10011---使用外部属性文件

来源:互联网 发布:昆明行知中学 编辑:程序博客网 时间:2024/05/22 11:58

-

配置文件里配置 Bean ,有时需要在Bean 的配置里混入系统部署的细节信息(例如:文件路径,数据源配置信息等).
  而这些部署细节实际上需要和Bean配置相分离
Spring 提供了一个 PropertyPlaceholderConfigurerBeanFactory后置处理器,这个处理器允许用户将Bean配置的部分内容外移属性文件.
  可以在 Bean 配置文件里使用形式为${var}的变量,PropertyPlaceholderConfigurer从属性文件里加载属性,并使用这些属性来替换变量.
  Spring 还允许在属性文件中使用${propName},以实现属性之间的相互引用。
注册 PropertyPlaceholderConfigurer
可通过 <context:property-placeholder>元素简化:
  –<beans> 中添加 context Schema 定义
  –在配置文件中加入如下配置:

-------------

----

ApplicationContext cxt = new ClassPathXmlApplicationContext("beans-properties.xml");javax.sql.DataSource ds = (javax.sql.DataSource) cxt.getBean("dataSource");System.out.println(ds.getConnection());


 

0 0
原创粉丝点击