Spring 使用Properties配置文件(转载)

来源:百度文库 编辑:神马文学网 时间:2024/07/03 08:01:04
1. jdbc.properties

database.url=jdbc:mysql://localhost/smaple
database.driver=org.gjt.mm.mysql.Driver
database.user=root
database.password=star1xing

2.conf.xml





            class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
       
            com/starxing/test/jdbc.properties
       

   

            class="org.springframework.jdbc.datasource.DriverManagerDataSource">
       
            ${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配置如下:

 class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

/WEB-INF/jdbc.properties



class="org.springframework.jdbc.datasource.DriverManagerDataSource">

${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获得更多信息。