您的位置:首页 > 编程语言 > Java开发

Spring框架——使用spring的特殊bean完成分散配置

2016-08-03 20:12 302 查看
使用spring的特殊bean完成分散配置:

使用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());
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java Spring 特殊bean
相关文章推荐