分散配置
在应用上下文中,使用PropertyPlaceholderConfigurer从外部文件装载配置信息。其中location属性允许使用单个配置文件,可使用其locations属性设置配置文件列表。
案例:
Service包中ConnServiceImpl.java
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;
}
}
Jdbc.properties文件
jdbc\:driverClass=com.mysql.jdbc.Driver
jdbc\:url=jdbc\:mysql\://localhost\:3306/db
jdbc\:user=dxl
jdbc\:password=root
applicationContext.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-2.5.xsd">
<!-- 分散配置解析 引入一个外部文件即使用location属性
<bean id="propertyPlaceholderConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>cn/csdn/service/jdbc.properties</value>
</property>
</bean>
-->
<!-- 其中location属性允许使用单个配置文件,可使用其locations属性设置配置文件列表。classPath使用反斜杠
使用分散配置就可用占位符变量替代Bean配置文件中硬编码配置了,语法上,占位符变量采用${variable}形式
-->
<!-- 引入多个外部文件即使用locations属性 -->
<bean id="propertyPlaceholderConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>cn/csdn/service/jdbc.properties</value>
</property>
</bean>
<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>
<!-- 硬编码 的使用
<bean id="connServiceImpl" class="cn.csdn.service.ConnServiceImpl"
scope="singleton">
<property name="driverClass">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/db</value>
</property>
<property name="user">
<value>dxl</value>
</property>
<property name="password">
<value>root</value>
</property>
</bean>
-->
</beans>
此案例中主要介绍了分散配置的使用,首先介绍了硬编码的使用,然后是引入单个分散配置的使用,再就是引入多个分散配置的使用,并将三者做了具体的比较。读者主要观察xml文件中的代码,xml文件中的代码简单明了,易于理解。
分享到:
相关推荐
spring-aop-1.1.1.jar spring-aop-1.2.6.jar spring-aop-1.2.9.jar spring-aop-2.0.2.jar spring-aop-2.0.6.jar spring-aop-2.0.7.jar spring-aop-2.0.8.jar spring-aop-2.0.jar spring-aop-2.5.1.jar spring-aop-...
spring源码缺失jar。spring-cglib-repack-3.2.5.jar,spring-objenesis-repack-2.6.jar
spring-cloud-config + spring-cloud-bus-amqp实现分布式集群配置动态更新,可更新实例对象,包含源码包+Rabbitmq安装包和安装说明
spring源码导入SpringObjenesis类报错解决,缺少jar包spring-cglib-repack-3.2.0.jar和spring-objenesis-repack-2.1.jar
最新版spring-web-5.2.3.RELEASE和spring-webmvc-5.2.3.RELEASE
spring-cglib-repack-3.2.5.jar和spring-objenesis-repack-2.6.jar
spring-cloud-config-eureka-bus:配置中心和消息总线示例(配置中心终结版) gateway-service-zuul:Spring Cloud Zuul使用初级篇 网关 均衡负载 spring-cloud-zuul:Spring Cloud Zuul使用高级篇 Filter 鉴权 熔断...
spring-context-1.2.8.jar, spring-context-1.2.9.jar, spring-context-2.0-m2.jar, spring-context-2.0.1.jar, spring-context-2.0.2.jar, spring-context-2.0.4.jar, spring-context-2.0.6.jar, spring-context-...
spring-boot项目pom.xm中parent依赖文件spring-boot-starter-parent-1.5.13.RELEASE.pom
spring-context-3.2.4.RELEASE.jar spring-core-3.2.4.RELEASE.jar spring-beans-3.2.4.RELEASE.jar spring-test-3.2.4.RELEASE.jar spring-web-3.2.4.RELEASE.jar spring-aop-3.2.4.RELEASE.jar spring-webmvc-...
spring-boot-starter-dubbo是 dubbo 的 spring boot starter,它可以无缝地对接 spring boot和dubbo ,方便大家使用 dubbo 组件。 spring-boot-starter-dubbo 支持的 jdk 版本为 1.6 或者 1.6 。 spring-boot-...
spring.jar spring-aop.jar spring-aop.jar spring-beans.jar spring-hibernate3.jar spring-jdbc.jar spring-struts.jar spring-web.jar
spring-aop-4.1.6.RELEASE.jar spring-aspects-4.1.6.RELEASE.jar spring-beans-4.1.6.RELEASE.jar spring-context-4.1.6.RELEASE.jar spring-context-support-4.1.6.RELEASE.jar spring-core-4.1.6.RELEASE.jar ...
spring-web-4.1.3.RELEASE.jarspring-web-4.1.3.RELEASE.jarspring-web-4.1.3.RELEASE.jarspring-web-4.1.3.RELEASE.jarspring-web-4.1.3.RELEASE.jarspring-web-4.1.3.RELEASE.jarspring-web-4.1.3.RELEASE.jar
spring-core-1.2.8.jar, spring-core-1.2.9.jar, spring-core-2.0.1.jar, spring-core-2.0.2.jar, spring-core-2.0.4.jar, spring-core-2.0.6.jar, spring-core-2.0.7.jar, spring-core-2.0.jar, spring-core-2.5.1....
activiti-spring-boot-starter-basic-6.0.0适配springboot2.1.2
spring-data-redis-1.8.1.RELEASE-sources.jar(spring-data-redis-1.8.1.RELEASE-sources.jar()
spring-web-5.0.4.RELEASE.jar; spring-web-5.0.4.RELEASE.jar
commons-pool2-2.3.jar,jedis-2.8.0.jar,spring-data-redis-1.6.0.RELEASE.jar,spring-session-1.1.1.RELEASE.jar,Spring-data-redis(Version 1.6.0.RC1)中文版.pdf
赠送jar包:spring-websocket-5.0.8.RELEASE.jar; 赠送原API文档:spring-websocket-5.0.8.RELEASE-javadoc.jar; 赠送源代码:spring-websocket-5.0.8.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-...