1、Spring5-IOC操作Bean管理(外部属性文件)-《Java学习知识库》

admin 2025-11-02 01:25:36 编程 来源:ZONE.CI 全球网 0 阅读模式

    1、直接配置数据库信息 (1)配置德鲁伊连接池 (2)引入德鲁伊连接池依赖 jar 包

    1. <!--直接配置连接池-->
    2. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
    3. <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
    4. <property name="url" value="jdbc:mysql://localhost:3306/userDb"></property>
    5. <property name="username" value="root"></property>
    6. <property name="password" value="root"></property>
    7. </bean>

    2、引入外部属性文件配置数据库连接池 (1)创建外部属性文件,properties 格式文件,写数据库信息

    1. prop.url=jdbc:mysql://localhost:3306/userDb
    2. prop.username=root
    3. prop.password=root
    4. prop.driverClassName=com.mysql.jdbc.Driver

    (2)把外部 properties 属性文件引入到 spring 配置文件中 * 引入 context 名称空间

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xmlns:p="http://www.springframework.org/schema/p"
    5. xmlns:util="http://www.springframework.org/schema/util"
    6. xmlns:context="http://www.springframework.org/schema/context"
    7. xsi:schemaLocation="http://www.springframework.org/schema/beans
    8. http://www.springframework.org/schema/beans/spring-beans.xsd
    9. http://www.springframework.org/schema/util
    10. http://www.springframework.org/schema/util/spring-util.xsd
    11. http://www.springframework.org/schema/context
    12. http://www.springframework.org/schema/context/spring-context.xsd">

    在 spring 配置文件使用标签引入外部属性文件

    1. <!--引入外部属性文件-->
    2. <context:property-placeholder location="classpath:jdbc.properties"/>
    3. <!--配置连接池-->
    4. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
    5. <property name="driverClassName" value="${prop.driverClassName}"></property>
    6. <property name="url" value="${prop.url}"></property>
    7. <property name="username" value="${prop.username}"></property>
    8. <property name="password" value="${prop.password}"></property>
    9. </bean>
    以太坊cppgolang区别 编程

    以太坊cppgolang区别

    以太坊是一种去中心化的开源平台,它采用智能合约技术,旨在构建和运行不受干扰的分布式应用程序。作为目前最受欢迎的区块链平台之一,以太坊提供了多种编程语言的支持,其
    progolang 编程

    progolang

    Go语言(Golang)是由Google开发的一门静态类型编程语言。作为一名专业的Golang开发者,我深知这门语言的优势和特点。在本文中,我将介绍Golang
    golangn个发送者 编程

    golangn个发送者

    Golang是一种开源的编程语言,由Google团队开发,旨在提高程序的并发性和简化软件开发过程。在Go语言中,有时需要向多个接收者发送信息。本文将介绍如何在G
    golang技能图谱 编程

    golang技能图谱

    从互联网行业的快速发展到人工智能技术的日益成熟,各种编程语言也应运而生。而在这众多的编程语言中,Golang(即Go)作为一门强大且高效的开发语言备受关注。Go
    评论:0   参与:  5