PropertyPlaceholderConfigurer可以实现:在单独的标准java Properties文件中配置属性,然后在spring的xml配置文件中,通过${key}获取配置属性的效果,这样做可以达到bean配置与properties配置分离的目的。
本章主要涉及的知识点有:
1. PropertyPlaceholderConfigurer
2. properties文件
3. 多properties文件加载
1.目录
project
\---src
\---main
\---java
| \---pers
| \---hanchao
| \---propertyplaceholderconfigurer
| \---App.java
| \---SolrUtils.java
\---resources
\---spring-config
\---spring-propertyplaceholderconfigurer.xml
\---spring-env.properties
\---spring-jdbc.properties
2.App.java
package pers.hanchao.propertyplaceholderconfigurer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import javax.sql.DataSource;
/**
* <p>对PropertyPlaceholderConfigurer的实例代码</p>
* @author hanchao 2018/1/11 22:41
**/
public class App {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-config/spring-propertyplaceholderconfigurer.xml");
SolrUtils solrUtils = (SolrUtils) context.getBean("solrUtils");
System.out.println(solrUtils.toString());
DataSource dataSource = (DataSource) context.getBean("dataSource");
System.out.println(dataSource.toString());
}
}
3.SolrUtils.java
package pers.hanchao.propertyplaceholderconfigurer;
public class SolrUtils {
private String host;
private String defaultCollection;
public void setHost(String host) {
this.host = host;
}
public void setDefaultCollection(String defaultCollection) {
this.defaultCollection = defaultCollection;
}
@Override
public String toString(){
return "solrUtils[host:" + host + ",defaultCollection:" +defaultCollection + "]";
}
}
4.spring-propertyplaceholderconfigurer.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<!--多properties文件配置-->
<property name="locations">
<list>
<value>spring-env.properties</value>
<value>spring-jdbc.properties</value>
</list>
</property>
<property name="fileEncoding" value="UTF-8"/>
</bean>
<bean id="solrUtils" class="pers.hanchao.propertyplaceholderconfigurer.SolrUtils">
<property name="host" value="${solr.host}"/>
<property name="defaultCollection" value="${solr.default.collection}"/>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${mysql.driverClassName}"/>
<property name="url" value="${mysql.url}"/>
<property name="username" value="${mysql.username}"/>
<property name="password" value="${mysql.password}"/>
</bean>
</beans>
5.spring-env.properties
solr.host=http://127.0.0.1:8983/solr
solr.default.collection=question
6.spring-jdbc.properties
mysql.driverClassName=com.mysql.jdbc.Driver
mysql.url=jdbc:mysql://localhost:3306/exam?useSSL=false
mysql.username=root
mysql.password=******
7.result
solrUtils[host:http://127.0.0.1:8983/solr,defaultCollection:question]
org.springframework.jdbc.datasource.DriverManagerDataSource@6737fd8f