免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 867 | 回复: 0
打印 上一主题 下一主题

Spring 使用Properties配置文件 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-08-13 09:30 |只看该作者 |倒序浏览
1. jdbc.properties
database.url=jdbc:mysql://localhost/smapledatabase.driver=org.gjt.mm.mysql.Driverdatabase.user=rootdatabase.password=star1xing
2.conf.xml
   
        
            com/starxing/test/jdbc.properties
        
   
   
        
            ${database.url}
        
        
            ${database.driver}
        
        
            ${database.user}
        
        
            ${database.password}
        
   
3.Config.java
package com.starxing.test;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.FileSystemResource;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
public class Config {
    public static void main(String[] args) {
        XmlBeanFactory factory = new XmlBeanFactory(new FileSystemResource(
                "com/starxing/test/conf.xml"));
        // 如果要在BeanFactory中使用,bean factory post-processor必须手动运行:
        PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();
        cfg.setLocation(new FileSystemResource(
                "com/starxing/test/jdbc.properties"));
        cfg.postProcessBeanFactory(factory);
        DriverManagerDataSource dataSource = (DriverManagerDataSource) factory
                .getBean("dataSource");
        System.out.println(dataSource.getDriverClassName());
        // 注意,ApplicationContext能够自动辨认和应用在其上部署的实现了BeanFactoryPostProcessor的bean。这就意味着,当使用ApplicationContext的时候应用PropertyPlaceholderConfigurer会非常的方便。由于这个原因,建议想要使用这个或者其他bean
        // factory postprocessor的用户使用ApplicationContext代替BeanFactroy。
        ApplicationContext context = new ClassPathXmlApplicationContext(
                "com/starxing/test/conf.xml");
        DriverManagerDataSource dataSource2 = (DriverManagerDataSource) context
                .getBean("dataSource");
        System.out.println(dataSource2.getDriverClassName());
    }
}
相关文档:
 使用这一解决方案,我们可以生成如下的属性文件(/WEB-INF/jdbc.properties): jdbc.driver=org.postgresql.Driver
jdbc.url=jdbc:postgresql://localhost/test
jdbc.user=postgres
jdbc.password=
  我们的Bean配置如下:
   
       /WEB-INF/jdbc.properties
   
   
         ${jdbc.driver}
   
   
         ${jdbc.url}
   
   
         ${jdbc.user}
   
   
         ${jdbc.password}
   
  如上所述,我们定义了一个PropertyPlaceholderConfigurer类的实例,并将其位置属性设置为我们的属性文件。该类被实现为Bean工厂的后处理器,并将使用定义在文件中的属性来代替所有的占位符(${...}value)。
  利用这种技术,我们可以从applicationContext.xml中移除所有特定于主机的配置属性。通过这种方式,我们可以自由地为该文件添加新的Bean,而不必担心特定于主机属性的同步性。这样可以简化生产部署和维护。
PropertyPlaceholderConfigurer作为一个bean factory post-processor实现,可以用来将BeanFactory定义中的属性值放置到另一个单独的Java Properties格式的文件中。这使得用户不用对BeanFactory的主XML定义文件进行复杂和危险的修改,就可以定制一些基本的属性(比如说数据库的urls,用户名和密码)。
考虑一个BeanFactory定义的片断,里面用占位符定义了DataSource:
在下面这个例子中,定义了一个datasource,并且我们会在一个外部Porperties文件中配置一些相关属性。 在运行时,我们为BeanFactory提供一个PropertyPlaceholderConfigurer,它将用Properties文件中的值替换掉这个datasource的属性值:
    ${jdbc.driverClassName}
    ${jdbc.url}
    ${jdbc.username}
    ${jdbc.password}
真正的值来自于另一个Properties格式的文件:
jdbc.driverClassName=org.hsqldb.jdbcDriver
jdbc.url=jdbc:hsqldb:hsql://production:9002
jdbc.username=sa
jdbc.password=root
如果要在BeanFactory中使用,bean factory post-processor必须手动运行:
XmlBeanFactory factory = new XmlBeanFactory(new FileSystemResource("beans.xml"));
PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();
cfg.setLocation(new FileSystemResource("jdbc.properties"));
cfg.postProcessBeanFactory(factory);
注意,ApplicationContext能够自动辨认和应用在其上部署的实现了BeanFactoryPostProcessor的bean。这就意味着,当使用ApplicationContext的时候应用PropertyPlaceholderConfigurer会非常的方便。由于这个原因,建议想要使用这个或者其他bean factory postprocessor的用户使用ApplicationContext代替BeanFactroy。
PropertyPlaceHolderConfigurer不仅仅在你指定的Porperties文件中查找属性, 如果它在其中没有找到你想使用的属性,它还会在Java的系统properties中查找。 这个行为能够通过设置配置中的systemPropertiesMode 属性来定制。这个属性有三个值, 一个让配置总是覆盖,一个让它永不覆盖,一个让它仅在properties文件中找不到的时候覆盖。 请参考 PropertiesPlaceholderConfigurer的JavaDoc获得更多信息。


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/45562/showart_358724.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP