Spring IOC/DI配置管理第三方bean
1 实现Druid管理
(1) 导入druid 的依赖
//pom.xml中添加依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
(2) 配置第三方bean
//在applicationContext.xml配置文件中添加 DruidDataSource的配置
<!--管理DruidDataSource对象-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<!--driverClassName:数据库驱动-->
<property name="url" value="jdbc:mysql://localhost:3306/spring"/>
<!--url:数据库连接地址-->
<property name="username" value="root"/>
<!--username:数据库连接用户名-->
<property name="password" value="ADcb1234"/>
<!--password:数据库连接密码-->
</bean>
(3) 从IOC容器中获取对应的bean对象
public class App {
public static void main(String[] args) {
ClassPathXmlApplicationContext cpxac = new ClassPathXmlApplicationContext("applicationContext.xml");
DataSource dataSource = (DataSource) cpxac.getBean("dataSource");
//获取DruidDataSource对象
System.out.println(dataSource);
}
}
2 实现C3P0管理
(1) 导入C3P0的依赖
//pom.xml中添加依赖
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
(2) 配置第三方bean
//在applicationContext.xml配置文件中添加配置
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!--ComboPooledDataSource的属性是通过setter方式进行注入。-->
<!--若ComboPooledDataSource的属性是通过构造方法方式进行注入,则使用constructor-arg标签。-->
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring"/>
<property name="user" value="root"/>
<property name="password" value="ADcb1234"/>
<property name="maxPoolSize" value="1000"/>
</bean>
(3) 从IOC容器中获取对应的bean对象
public class App {
public static void main(String[] args) {
ClassPathXmlApplicationContext cpxac = new ClassPathXmlApplicationContext("applicationContext.xml");
DataSource dataSource = (DataSource) cpxac.getBean("dataSource");
//获取DruidDataSource对象
System.out.println(dataSource);
}
}
3 加载properties文件
(1) 准备properties配置文件
//resources下创建一个jdbc.properties文件,并添加对应的属性键值对。
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/spring
jdbc.username=root
jdbc.password=ADcb1234
(2) 开启context命名空间
//在applicationContext.xml中开启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为xmlnamespace。复制第一行,在smlns后加":context",再将最后的beans改为context。-->
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
<!--复制以上两行,并将以上两行中的beans全部改为context。-->
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
</beans>
(3) 加载properties配置文件
//在配置文件中使用context命名空间下的标签来加载properties配置文件。<context:property-placeholder location="jdbc.properties"/>
//location会自动在resource文件夹下搜索文件。
(4) 完成属性注入
//使用${key}来读取properties配置文件中的内容并完成属性注入。
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
(5) 注意事项
<1> 键值对的key为username引发的问题
<1.1> 原因<context:property-placeholder/>标签会加载系统的环境变量,而且环境变量的值会被优先加载。
<1.2> 解决方案<context:property-placeholder location="jdbc.properties" system-properties-mode="NEVER"/>
//system-properties-mode:设置为NEVER,表示不加载系统属性,就可以解决上述问题。
//当然还有一个解决方案就是避免使用username作为属性的key。
<2> 当有多个properties配置文件需要被加载
<2.1> 方式一
//如果配置文件多的话,每个都需要配置。<context:property-placeholder location="jdbc1.properties,jdbc2.properties" system-properties-mode="NEVER"/>
<2.2> 方式二
//*.properties代表所有以properties结尾的文件都会被加载,可以解决方式一的问题,但是不标准。<context:property-placeholder location="*.properties" system-properties-mode="NEVER"/>
<2.3> 方式三(标准方式)
//标准的写法,classpath:代表的是从根路径下开始查找,但是只能查询当前项目的根路径。<context:property-placeholder location="classpath:*.properties" system-properties-mode="NEVER"/>
<2.4> 方式四
//不仅可以加载当前项目还可以加载当前项目所依赖的所有项目的根路径下的properties配置文件。<context:property-placeholder location="classpath*:*.properties" system-properties-mode="NEVER"/>

浙公网安备 33010602011771号