[Spring IOC container] Import external property files



Import external properties file

In many cases, there may be more than one attribute in our class. Then we need to use the property tag multiple times to inject the attribute when we configure it in xml mode. Once there are more attributes, it will be very troublesome to use. Then we can configure some fixed values ​​in advance and simply quote them when using them. Common is the connection for database operations! We can configure the database name, user name, password, url, etc. in advance, and then directly import it when using it.

① Configure database information directly

Ⅰ. Configure the Druid connection pool

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--  直接配置数据库连接池  -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <!--   配置数据库信息     -->
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306:/test01"></property>
        <property name="username" value="root"></property>
        <property name="password" value="123456"></property>
    </bean>
</beans>

Ⅱ. Introduce the druid connection pool dependency jar package

Insert picture description here

Back to top


② Configure the database connection pool through an external file

Ⅰ. Create an external properties format file

  • 文件中写数据库信息
prop.driverClass=com.mysql.jdbc.Driver
prop.url=jdbc:mysql://localhost:3306/test01
prop.userName=root
prop.password=123456

Ⅱ. Import external files into spring configuration files

  • Introduce context 名称空间
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
  • Use tags to introduce external property files in spring configuration files
    <!--  通过引入外部属性文件  -->
    <context:property-placeholder location="classpath:外部属性文件/jdbc.properties"></context:property-placeholder>
    <!--  直接配置数据库连接池  -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <!--   配置数据库信息     -->
        <property name="driverClassName" value="${prop.driverClass}"></property>
        <property name="url" value="${prop.url}"></property>
        <property name="username" value="${prop.userName}"></property>
        <property name="password" value="${prop.password}"></property>
    </bean>
</beans>

Back to top


Guess you like

Origin blog.csdn.net/qq_45797116/article/details/113573258