一、加载properties文件
新建一个properties配置文件
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/bookshop
jdbc.username=root
jdbc.password=12345
在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:util="http://www.springframework.org/schema/util"
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
https://www.springframework.org/schema/context/spring-context.xsd">
</beans>
加载properties配置文件
<context:property-placeholder location="jdbc.properties"/>
使用${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>
添加对应的类 BookDao和BookDaoImpl类,并在BookDaoImpl类中添加name属性与setter方法
public interface BookDao {
public 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);
}
}
配置文件
<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>
<bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl">
<property name="name" value="${jdbc.driver}"/>
</bean>
运行文件
public class AppForLifeCycle {
public static void main( String[] args ) {
ApplicationContext ctx = new
ClassPathXmlApplicationContext("applicationContext.xml");
DataSource dataSource = (DataSource) ctx.getBean("dataSource");
System.out.println(dataSource);
BookDao bookDao = (BookDao) ctx.getBean("bookDao");
bookDao.save();
}
}
标签会加载系统的环境变量,而且环境变量的值会被优先加载
配置文件
<bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl"> <property name="name" value="${username}"/> </bean>
properties文件新加一行配置
username=9999
添加对应的类 BookDao和BookDaoImpl类,并在BookDaoImpl类中添加name属性与setter方法
public interface BookDao { public 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); } }
运行文件
public class AppForLifeCycle { public static void main( String[] args ) { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); BookDao bookDao = (BookDao) ctx.getBean("bookDao"); bookDao.save(); } }
重新运行
这时看到输出的名称不是设置的9999
就是因为标签会加载系统的环境变量,而且环境 变量的值会被优先加载
system-properties-mode:设置为NEVER,表示不加载系统属性,就可以解决上述问题。
<context:property-placeholder location="jdbc.properties" system-properties-mode="NEVER"/>
![]()
当有多个properties配置文件需要被加载
jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://127.0.0.1:3306/bookshop jdbc.username=root jdbc.password=12345
jdbc1.properties
username=9999
<!--方式一 --> <context:property-placeholder location="jdbc.properties,jdbc2.properties" system-propertiesmode="NEVER"/> <!--方式二--> <context:property-placeholder location="*.properties" systemproperties-mode="NEVER"/> <!--方式三 --> <context:property-placeholder location="classpath:*.properties" system-properties-mode="NEVER"/> <!--方式四--> <context:property-placeholder location="classpath*:*.properties" system-properties-mode="NEVER"/> </beans>