在配置文件中配置Bean时,有时需要在Bean的配置中混入系统部署的细节信息(例如文件路径,数据源配置信息等).而这些部署细节实际上需要和Bean配置相分离
Spring提供了一个PropertyPlaceholderConfigurer的BeanFactory后置处理器,这个处理器允许用户将Bean配置的部分内容外移到属性文件中.可以在Bean配置文件中使用形式为${var}
的变量,PropertyPlaceholderConfigurer从属性文件中嘉爱属性,并使用这些属性来替换变量
Spring还允许在属性文件中使用${propName}
,以实现属性之间的相互引用
正常获取数据库连接的配置如下:
pom.xml引入c3p0和MySQL驱动:
1 2 3 4 5 6 7 8 9 10 11
| <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency>
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.16</version> </dependency>
|
applicationContext.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <?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"> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="user" value="root"/> <property name="password" value="@SWZDL231100"/> <property name="driverClass" value="com.mysql.cj.jdbc.Driver"/> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/wechat?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC"/> </bean> </beans>
|
Main.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| package relation;
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
import javax.sql.DataSource; import java.sql.SQLException;
public class Main { public static void main(String[] args) throws SQLException { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); DataSource dataSource = (DataSource) applicationContext.getBean("dataSource"); System.out.println(dataSource.getConnection()); } }
|
运行结果为
1
| com.mchange.v2.c3p0.impl.NewProxyConnection@27c6e487 [wrapping: com.mysql.cj.jdbc.ConnectionImpl@49070868]
|
表示连接成功
但是此时数据库密码等都在applicationContext.xml中,不方便修改,可以将这些配置放在外部的配置文件中
新建配置文件名为DataSource.properties
1 2 3 4
| user=root password=@SWZDL231100 driverClass=com.mysql.cj.jdbc.Driver jdbcUrl=jdbc:mysql://localhost:3306/wechat?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
|
修改applicationContext.xml
为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| <?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="relation/DataSource.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="user" value="${user}"/> <property name="password" value="${password}"/> <property name="driverClass" value="${driverClass}"/> <property name="jdbcUrl" value="${jdbcUrl}"/> </bean>
</beans>
|
运行结果相同