Spring框架——使用spring的特殊bean完成分散配置
2016-08-03 20:12
302 查看
使用spring的特殊bean完成分散配置:
使用spring的特殊bean完成分散配置有两种方式引入文件:
1、将配置文件分成几个分散的配置文件。如数据源。
<context:property-placeholder location="classpath:com/hsp/spring/dispatcher/db.properties"/>
beans.xml配置代码:
说明: 当通过 context:property-placeholder 引入多个属性文件的时候,使用逗号隔开.
<context:property-placeholder location="classpath:com/bean/dispatch/db.properties,com/bean/dispatch/db2.properties"/>
<!-- 配置一个DBUtil对象 -->
<bean id="dbutil" class="com.bean.dispatch.DBUtil">
<property name="drivername" value="${drivername}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="pwd" value="${pwd}"/>
</bean>
<!-- 配置一个DBUtil对象 -->
<bean id="dbutil2" class="com.bean.dispatch.DBUtil">
<property name="drivername" value="${db2.drivername}"/>
<property name="url" value="${db2.url}"/>
<property name="username" value="${db2.username}"/>
<property name="pwd" value="${db2.pwd}"/>
</bean>取值:
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("com/bean/dispatch/beans.xml");
DBUtil db=(DBUtil)ac.getBean("dbutil2");
System.out.println(db.getDrivername()+" : "+db.getUrl());
}
使用spring的特殊bean完成分散配置有两种方式引入文件:
1、将配置文件分成几个分散的配置文件。如数据源。
<bean class="...PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>xx/yy/db.properties</value> <value>xx/yy/db2.properties</value> </list> </property> </bean>2、名字空间配置
<context:property-placeholder location="classpath:com/hsp/spring/dispatcher/db.properties"/>
beans.xml配置代码:
说明: 当通过 context:property-placeholder 引入多个属性文件的时候,使用逗号隔开.
<context:property-placeholder location="classpath:com/bean/dispatch/db.properties,com/bean/dispatch/db2.properties"/>
<!-- 配置一个DBUtil对象 -->
<bean id="dbutil" class="com.bean.dispatch.DBUtil">
<property name="drivername" value="${drivername}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="pwd" value="${pwd}"/>
</bean>
<!-- 配置一个DBUtil对象 -->
<bean id="dbutil2" class="com.bean.dispatch.DBUtil">
<property name="drivername" value="${db2.drivername}"/>
<property name="url" value="${db2.url}"/>
<property name="username" value="${db2.username}"/>
<property name="pwd" value="${db2.pwd}"/>
</bean>取值:
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("com/bean/dispatch/beans.xml");
DBUtil db=(DBUtil)ac.getBean("dbutil2");
System.out.println(db.getDrivername()+" : "+db.getUrl());
}
相关文章推荐
- 使用spring的特殊bean --- 分散配置
- Spring-简单bean的配置(使用bean.xml手动完成)
- Spring根据XML配置文件注入属性 其实也是造bean,看看是使用constructor还是setter顺带完成属性赋值
- spring 特殊bean ,分散配置
- 关于在Spring配置特殊bean-分散配置的时候抛出的异常:无法找到元素 'context:component-scan' 的声明 的解决方法
- Spring框架使用properties配置数据源Bean【Spring入门】
- 使用spring的特殊bean --- 对bean进行后处理和对bean工厂进行后处理简单了解
- 使用dom4j:读取spring配置文件,实例化bean对象并注入依赖对象
- spring3 混合使用 XML 与注解(Annotation)进行 Bean 的配置
- 扩展Spring——使用 Annotation将配置资源注入到Bean中
- 使用Spring的特殊bean
- Spring学习笔记 使用annotation配置实现Bean的auto-wiring (自动绑定)
- 注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(五):使用@Component 来简化bean的配置
- spring 2.5 中配置bean 使用annotation
- Spring 使用配置完成从前台页面输入到查询数据库的例子
- Spring-Context之三:使用XML和Groovy DSL配置Bean
- Spring学习笔记 使用XML配置实现Bean的auto-wiring (自动绑定)
- 使用通配符完成struts2和spring多配置文件_悄悄俏俏
- 注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(五):使用@Component 来简化bean的配置
- 注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(五):使用@Component 来简化bean的配置