Spring IOC/DI 管理第三方Bean + 加载properties配置文件详解(Spring系列2)
在日常Spring开发中,我们通常用IOC容器管理自己编写的业务类,但实际项目中经常需要管理第三方Jar包中的类(比如数据库连接池Druid、C3P0等)。同时,硬编码配置信息(如数据库四要素)不利于后期维护,本文将通过实战案例,详细讲解第三方Bean的管理方式,以及如何加载外部properties配置文件实现配置解耦,并梳理常见坑点与解决方案。
一、IOC/DI配置管理第三方Bean(Druid数据源实战)
1.1 案例背景
Druid(德鲁伊)和C3P0是Java中常用的数据库连接池技术,核心作用是管理数据库连接,避免频繁创建/关闭连接带来的性能损耗,提升数据库操作效率。本次以Druid为例,演示如何用Spring IOC容器管理第三方数据源对象。
1.2 环境准备
1. 创建Maven项目
spring_09_datasource ├── src │ └── main │ ├── java │ └── resources └── pom.xml2. pom.xml添加Spring核心依赖
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.10.RELEASE</version> </dependency> </dependencies>3. 编写Spring配置文件与运行类
<?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"> </beans>public class App { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); } }1.3 实现Druid数据源管理
步骤1:导入Druid依赖
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.16</version> </dependency>步骤2:配置第三方Bean
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/spring_db"/> <property name="username" value="root"/> <property name="password" value="root"/> </bean>步骤3:获取并验证Bean
public class App { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); DataSource dataSource = (DataSource) ctx.getBean("dataSource"); System.out.println(dataSource); } }二、加载properties配置文件(配置解耦优化)
将数据库配置硬编码在XML中不利于维护,我们需要将配置提取到外部properties文件,实现解耦。
2.1 实现步骤
步骤1:创建jdbc.properties配置文件
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://127.0.0.1:3306/spring_db jdbc.username=root jdbc.password=root步骤2:开启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"> <!-- 加载properties配置文件 --> <context:property-placeholder location="jdbc.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> </beans>2.2 读取单个属性实战
// 接口 public interface BookDao { void save(); } // 实现类 public class BookDaoImpl implements BookDao { private String name; public void setName(String name) { this.name = name; } public void save() { System.out.println("book dao save ..." + name); } }三、避坑指南(核心注意事项)
- username关键字问题:properties中key为username会读取系统环境变量,解决方案:添加system-properties-mode="NEVER"或避免使用该关键字
- 多配置文件加载
加载properties文件
不加载系统属性
<context:property-placeholder location="jdbc.properties" system-properties-mode="NEVER"/>加载多个properties文件
<context:property-placeholder location="jdbc.properties,msg.properties"/>加载所有properties文件
<context:property-placeholder location="*.properties"/>加载properties文件标准格式
<context:property-placeholder location="classpath:*.properties"/>从类路径或jar包中搜索并加载properties文件
<context:property-placeholder location="classpath:*.*.properties"/>
四、总结
- 第三方Bean通过<bean>标签配置,setter方式注入属性,交由IOC容器管理
- 使用context:property-placeholder加载properties文件,${key}读取配置
- 核心应用场景:数据库连接池、第三方工具类的配置与管理
