使用外部属性文件
•在配置文件里配置 Bean 时, 有时需要在 Bean 的配置里混入系统部署的细节信息(例如: 文件路径, 数据源配置信息等). 而这些部署细节实际上需要和 Bean 配置相分离
•Spring 提供了一个 PropertyPlaceholderConfigurer 的 BeanFactory 后置处理器, 这个处理器允许用户将 Bean 配置的部分内容外移到属性文件中. 可以在 Bean 配置文件里使用形式为 ${var} 的变量, PropertyPlaceholderConfigurer 从属性文件里加载属性, 并使用这些属性来替换变量.
•Spring 还允许在属性文件中使用 ${propName},以实现属性之间的相互引用。
以连接数据库为例,我们还需要导入c3p0、mysql-connector-java两个jar包,选定他们右键---build path---add to build path
Spring连接数据库
新建一个xml配置文件如:beans-properties.xml,代码如下
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="root"></property>
<property name="password" value="11111111"></property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///test"></property>
</bean>
</beans>
连接数据库代码,这里写在主类里,检测其能否连接上
package com.yorkmass.spring.beans.properties;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) throws SQLException {
// TODO Auto-generated method stub
ApplicationContext ctx=new ClassPathXmlApplicationContext("beans-properties.xml");
DataSource datasource=(DataSource)ctx.getBean("dataSource");
System.out.println(datasource.getConnection());
}
}
运行没有出错,即数据库连接成功。
使用外部属性文件配置bean
一般不把数据库连接等基本信息写在bean里面,因为不方便修改。
我们可以新建一个数据库连接属性文件如:db.properties
然后我们从bean中引用这个文件,后期修改、更换数据只需要修改db.properties文件里面的基本信息
1、在属性文件写入内容,基本连接信息
user=root
password=11111111
driverclass=com.mysql.jdbc.Driver
jdbcurl=jdbc:mysql:///test
2、我们首先打开beans-properties.xml文件,在Namespaces中勾选context命名空间
3、 导入属性文件即刚才新建的db.properties文件,然后value里面的值用${var}引入,var相当于db.properties中的“键”, beans-properties.xml代码如下
<?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:p="http://www.springframework.org/schema/p"
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-4.0.xsd">
<!-- 导入属性文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 使用外部化属性文件的属性 -->
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
<property name="driverClass" value="${driverclass}"></property>
<property name="jdbcUrl" value="${jdbcurl}"></property>
</bean>
</beans>
在属性文件中实现属性之间的相互引用
user=root
endpassword=1111
password=1111${endpassword}
##属性之间相互引用
driverclass=com.mysql.jdbc.Driver
jdbcurl=jdbc:mysql:///test