Spring IOC/DI 管理第三方Bean + 加载properties配置文件详解(Spring系列2)
在日常Spring开发中我们通常用IOC容器管理自己编写的业务类但实际项目中经常需要管理第三方Jar包中的类比如数据库连接池Druid、C3P0等。同时硬编码配置信息如数据库四要素不利于后期维护本文将通过实战案例详细讲解第三方Bean的管理方式以及如何加载外部properties配置文件实现配置解耦并梳理常见坑点与解决方案。一、IOC/DI配置管理第三方BeanDruid数据源实战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 groupIdorg.springframework/groupId artifactIdspring-context/artifactId version5.2.10.RELEASE/version /dependency /dependencies3. 编写Spring配置文件与运行类?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beans xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xsi:schemaLocation http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd /beanspublic class App { public static void main(String[] args) { ApplicationContext ctx new ClassPathXmlApplicationContext(applicationContext.xml); } }1.3 实现Druid数据源管理步骤1导入Druid依赖dependency groupIdcom.alibaba/groupId artifactIddruid/artifactId version1.1.16/version /dependency步骤2配置第三方Beanbean iddataSource classcom.alibaba.druid.pool.DruidDataSource property namedriverClassName valuecom.mysql.jdbc.Driver/ property nameurl valuejdbc:mysql://localhost:3306/spring_db/ property nameusername valueroot/ property namepassword valueroot/ /bean步骤3获取并验证Beanpublic 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.drivercom.mysql.jdbc.Driver jdbc.urljdbc:mysql://127.0.0.1:3306/spring_db jdbc.usernameroot jdbc.passwordroot步骤2开启context命名空间并加载配置文件?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beans xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xmlns:contexthttp://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 locationjdbc.properties/ !-- 注入配置 -- bean iddataSource classcom.alibaba.druid.pool.DruidDataSource property namedriverClassName value${jdbc.driver}/ property nameurl value${jdbc.url}/ property nameusername value${jdbc.username}/ property namepassword value${jdbc.password}/ /bean /beans2.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-modeNEVER或避免使用该关键字多配置文件加载加载properties文件不加载系统属性context:property-placeholder locationjdbc.properties system-properties-modeNEVER/加载多个properties文件context:property-placeholder locationjdbc.properties,msg.properties/加载所有properties文件context:property-placeholder location*.properties/加载properties文件标准格式context:property-placeholder locationclasspath:*.properties/从类路径或jar包中搜索并加载properties文件context:property-placeholder locationclasspath:*.*.properties/四、总结第三方Bean通过bean标签配置setter方式注入属性交由IOC容器管理使用context:property-placeholder加载properties文件${key}读取配置核心应用场景数据库连接池、第三方工具类的配置与管理