0、Spring加载classpath路径下的propertities配置文件
<!-- 使用spring自带的占位符替换功能 -->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<!-- 允许JVM参数覆盖 -->
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<!-- 忽略没有找到的资源文件 -->
<property name="ignoreResourceNotFound" value="true" />
<!-- 配置资源文件 -->
<property name="locations">
<list>
<value>classpath:baseInfo.properties</value>
<value>classpath:db.properties</value>
</list>
</property>
</bean>
db.properties的配置
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/student
jdbc.username=root
jdbc.password=1234
baseInfo.properties的配置
my.jspPath=/jsp
my.htmlPath=/html
1、用于数据库连接池的配置
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="30" />
<property name="maxIdle" value="5" />
</bean>
</beans>
2、在Java程序中使用propertities中的配置
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@Service
public class ProService {
@Value("${my.jspPath}")
private String jspPath;
@Value("${my.htmlPath}")
private String htmlPath;
public void test() {
System.out.println("jspPath:" + this.jspPath + ", htmlPath:"
+ this.htmlPath);
}
}
注意baseInfo.properties和ProService.java的代码实现
3、自定义实现propertities配置文件的加载
使用上面的方法,但是这有一个问题,我每用一次配置文件中的值,就要声明一个局部变量。有没有用代码的方式,直接读取配置文件中的值。答案就是重写PropertyPlaceholderConfigurer。
3.1 重写PropertyPlaceholderConfigurer
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
public class PropertyPlaceholder extends PropertyPlaceholderConfigurer {
private static Map<String, String> propertyMap;
@Override
protected void processProperties(
ConfigurableListableBeanFactory beanFactoryToProcess,
Properties props) throws BeansException {
super.processProperties(beanFactoryToProcess, props);
propertyMap = new HashMap<String, String>();
for (Object key : props.keySet()) {
String keyStr = key.toString();
String value = props.getProperty(keyStr);
propertyMap.put(keyStr, value);
}
}
// static method for accessing context properties
public static Object getProperty(String name) {
return propertyMap.get(name);
}
}
3.2 在Spring的配置文件里面,
用上面的类,代替PropertyPlaceholderConfigurer
<!-- 使用spring自带的占位符替换功能 -->
<bean class="com.utils.PropertyPlaceholder">
<!-- 允许JVM参数覆盖 -->
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<!-- 忽略没有找到的资源文件 -->
<property name="ignoreResourceNotFound" value="true" />
<!-- 配置资源文件 -->
<property name="locations">
<list>
<value>classpath:baseInfo.properties</value>
<value>classpath:db.properties</value>
</list>
</property>
</bean>
3.2 Java程序中的用法
import org.springframework.stereotype.Service;
import com.utils.PropertyPlaceholder;
@Service
public class MyProService {
public void test() {
System.out.println("jspPath:"
+ PropertyPlaceholder.getProperty("my.jspPath") + ", htmlPath:"
+ PropertyPlaceholder.getProperty("my.htmlPath"));
}
}
具体的代码实现,就是PropertyPlaceholder.getProperty("my.jspPath").
4、测试代码
public class App {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"classpath:spring/bean-*.xml");
System.out.println(context);
// 获取并测试
ProService proService = context.getBean("proService", ProService.class);
System.out.println(proService);
proService.test();
// 获取并测试
MyProService myproService = context.getBean("myProService",
MyProService.class);
System.out.println(myproService);
myproService.test();
// 获取并测试
DataSource dataSource = context.getBean("dataSource", DataSource.class);
System.out.println(dataSource);
}
}
5、源码下载