分散配置
在应用上下文中,使用PropertyPlaceholderConfigurer从外部属性文件装载的配置信息:
应用上下文中从当个外部外部属性文件装载配置信息:
<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="cn/csdn/service/jdbc.properties"/>
</bean>
其中,location属性允许使用单个配置文件,可试用其locations属性设置配置文件列表:
<bean id="propertyPlaceholderConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>cn/csdn/service/jdbc.properties</value>
<value>cn/csdn/service/security.properties</value>
<value>cn/csdn/service/application.properties</value>
</list>
</property>
</bean>
这样,就可用占位符变量代替Bean配置文件中硬编码配置中。语法上,占位符变量采用${variacle}的形式,如下:
ConnServiceImpl.java
package cn.csdn.service;
public class ConnServiceImpl {
private String driverClass;
private String url;
private String user;
private String password;
public void setDriverClass(String driverClass) {
this.driverClass = driverClass;
}
public void setUrl(String url) {
this.url = url;
}
public void setUser(String user) {
this.user = user;
}
public void setPassword(String password) {
this.password = password;
}
public String getDriverClass() {
return driverClass;
}
public String getUrl() {
return url;
}
public String getUser() {
return user;
}
public String getPassword() {
return password;
}
}
<bean id="connServiceImpl" class="cn.csdn.service.ConnServiceImpl"
scope="singleton">
<property name="driverClass">
<value>${jdbc:driverClass}</value>
</property>
<property name="url">
<value>${jdbc:url}</value>
</property>
<property name="user">
<value>${jdbc:user}</value>
</property>
<property name="password">
<value>${jdbc:password}</value>
</property>
</bean>
jdbc.properties
jdbc\:driverClass=com.mysql.jdbc.Driver
jdbc\:url=jdbc\:mysql\://localhost\:3306/db
jdbc\:user=z_xiaofei168
jdbc\:password=root
分享到:
相关推荐
spring-framework-2.5-rc2-with-dependencies\spring-framework-2.5-rc2\spring-framework-2.5-rc2docs
Spring2.5-中文参考手册 chm .zip
Spring2.5-中文参考手册.chm
Spring2.5-中文参考手册(这是个chm格式的文档),阅读起来比较方便!!!
spring-framework-2.5-rc2-with-dependencies\spring-framework-2.5-rc2\spring-framework-2.5-rc2源代码
spring-framework-2.5-rc2-with-dependencies\spring-framework-2.5-rc2\spring-framework-2.5-rc2-with-dependenciesspring-framework-2.5-rc2spring-framework-2.5-rc2官方自带案例
Spring2.5-Reference_zh_CN.txt 非常适合手机等学习研究
spring2.5-中文API spring2.5-中文API
Spring2.5-中文参考手册,CHM版本的Spring2.5-中文参考手册
Spring2.5-中文参考手册 spring的全面参考手册chm阅读方便
共三个文档,spring2.5.2开发参考手册pdf,spring2.5.ppt,Spring2.5-中文参考手册.chm
spring2.5 -3.0 hibernate3.3 jar包说明 自己总结,希望对你有帮助
spring2.5-中文参考手册.pdf spring2.5-中文参考手册.pdf
dubbo2.5-spring4-mybastis3.2-springmvc4-mongodb-redis, dubbo2.5-spring4-mybastis3.2-springmvc4-mongodb-redis整合
Spring2.5-中文参考手册chm,Spring 详细说明解读。Spring2.5-中文参考手册chm,Spring 详细说明解读。
spring2.5-api手册chm格式
spring2.5-英文文档1