javabean的结构(Java开发框架5-2配置第三方bean-加载properties文件)
javabean的结构(Java开发框架5-2配置第三方bean-加载properties文件)jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://127.0.0.1:3306/spring_db jdbc.USERNAME=root jdbc.password=chenmo在 spring 的配置文件中开启 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.springf
#我在头条搞创作第二期#
5.2 加载 properties 文件上述对于 druid的配置使用到了一些固定的常量如数据库连接的四要素,将这些值写在 spring的配置文件中不利于后期维护
需要将这些值提取到一个外部的 properties配置文件中
5.2.1 第三方 bean 属性优化1. 实现思路需求:将数据库连接四要素提取到 properties配置文件,spring来加载配置信息并使用这些信息来完成属性注入。
在 resources下创建一个 jdbc.properties (文件的名称可以任意)
将数据库连接四要素配置到配置文件中
⭐️ 在 Spring的配置文件中加载properties文件
⭐️ 使用加载到的值实现属性注入
2. 实现步骤- 创建 properties 配置文件,并添加对应的属性键值对
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/spring_db
jdbc.USERNAME=root
jdbc.password=chenmo
- 在 spring 的配置文件中开启 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">
</beans>
- 为 spring 加载 porperties 配置文件
<context:property-placeholder location="jdbc.properties"/>
- 使用 ${key} 来读取 properties配置文件中的内容并完成属性注入
<?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">
<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>
5.2.2 读取单个属性1. 实现思路
需求:从properties配置文件中读取key为name的值,并将其注入到BookDao中并在save方法中进行打印。
在项目中添加BookDao和BookDaoImpl类
为BookDaoImpl添加一个name属性并提供setter方法
在jdbc.properties中添加数据注入到bookDao中打印方便查询结果
在applicationContext.xml添加配置完成配置文件加载、属性注入(${key})
2. 实现步骤- 添加类
public interface BookDao {
public void save();
}
public class BookDaoImpl implements BookDao {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void save() {
System.out.println("book dao save..." name);
}
}
- 完成配置文件的读取与注入
<?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">
<context:property-placeholder location="jdbc.properties"/>
<bean id="bookDao" class="dao.impl.BookDaoImpl">
<property name="name" value="${jdbc.driver}"/>
</bean>
</beans>
- 测试
@Test
public void test1(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
BookDao bookDao = (BookDao) ctx.getBean("bookDao");
bookDao.save();
}
测试结果如下:
- 键值对的 key为 username引发的问题
- 在 properties中配置 username=root
username=root
<?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">
<context:property-placeholder location="jdbc.properties"/>
<!-- 注入属性 -->
<bean id="bookDao" class="dao.impl.BookDaoImpl">
<property name="name" value="${username}"/>
</bean>
</beans>
- 测试结果:控制台打印的不是root而是电脑的用户名
- 问题原因:context:property-placeholder/> 标签会加载系统的环境变量,而且环境变量的值会被优先加载
系统环境变量的查看
Map<String String> env = System.getenv();
System.out.println(env);
打印结果中有一个 username=XXX(电脑的用户名称)
- 解决方案
- context:property-placeholder/>中的 system-properties-mode 属性设置为 NEVER,表示不加载系统属性
- 避免使用 username作为属性的 key
- 问题二:当有多个 properties 配置文件需要被加载时的配置方案
<?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">
<!-- 方式一 -->
<!-- 每个配置文件都需要配置 -->
<context:property-placeholder location="jdbc.properties jdbc1.properties"/>
<!-- 方式一 -->
<!-- 所有以 properties 结尾的文件都会被加载,不建议用 -->
<context:property-placeholder location="*.properties"/>
<!-- 方式一 -->
<!-- 标准写法, classP:代表从更目录下开始查找,只查询当前项目的更路径-->
<context:property-placeholder location="classpath:*.properties"/>
<!-- 方式一 -->
<!-- 不仅可以加载当前项目还可以加载当前项目所依赖的所有项目的根路径下的配置文件-->
<context:property-placeholder location="classpath*:*.properties"/>
</beans>