spring4.27 数据源对象管理和properties文件引入

1.Druid

<bean id="druid" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/spring_db"></property>
        <property name="username" value="root"></property>
        <property name="password" value="p1234"></property>
    </bean>

2.c3p0

<bean id="c3p0" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring_db"></property>
        <property name="user" value="root"></property>
        <property name="password" value="p1234"></property>
    </bean>

3.properties文件引入

1.需要命名空间

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

2.导入context
单个导入

<context:property-placeholder location="jdbc.properties.properties" />

多个导入,在后面加个逗号即可

<context:property-placeholder location="jdbc.properties.properties,jdbc2.properties" />

全部导入,加一个*即可

<context:property-placeholder location="*.properties" />

全部导入标准格式,加一个classpath即可

 <context:property-placeholder location="classpath:*.properties" />

从类路径或者jar包中搜索文件,在classpath后面加个*号

 <context:property-placeholder location="classpath*:*.properties" />

3.使用转义符进行低耦合度的开发
使用${xxx} 其中xxx为proper文件中写的数据名driverClassName=com.mysql.jdbc.Driver
在spring配置文件中,value需要与其对应

 <property name="driverClassName" value="${driverClassName}"></property>

总结

1.如果在proper文件中书写username可能会拿不到想要的数据,因为系统名的优先级高于pro,会输出自己的系统名称,可以在context进行修改,修改system为never。

 <context:property-placeholder location="classpath:*.properties" system-properties-mode="NEVER"/>

你可能感兴趣的:(笔记,spring,java)