【问题标题】:spring PropertyPlaceholderConfigurer and context:property-placeholderspring PropertyPlaceholderConfigurer 和 context:property-placeholder
【发布时间】:2011-11-24 10:39:48
【问题描述】:

我有以下 bean 声明:

  <bean
     class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>WEB-INF/classes/config/properties/database.properties</value>
                <value>classpath:config/properties/database.properties</value>
            </list>
        </property>
        <property name="ignoreResourceNotFound" value="true"/>
    </bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
</bean>

现在我想将上面的 PropertyPlaceholderConfigurer 更改为以下格式:

<context:component-scan base-package="org.example.config"/>
<util:properties id="jdbcProperties" 
           location="classpath:config/properties/database.properties"/>
  1. ignoreResourceNotFound 将在运行时忽略该属性。例如: 当测试应用程序 WEB-INF/.. 路径将忽略(因为 maven 项目和属性文件位于 src/main/resources/..),而 启动 web 应用程序,其他属性将忽略路径,我需要 实现与上述格式相同。
  2. 应该可以添加多个属性文件,比如 database.properties、test.properties 等
  3. 在 Spring 3 中,我可以为 DB 使用注解而不是这些 xml 文件吗 加载中,我该怎么做?因为我只使用一个 xml 文件(给定 以上)加载数据库的东西。

我正在使用 Spring 3 框架。

【问题讨论】:

    标签: spring


    【解决方案1】:

    &lt;context:property-placeholder ... /&gt; 是等同于 PropertyPlaceholderConfigurer 的 XML。所以,更喜欢那个。 &lt;util:properties/&gt; 只是制造一个您可以注入的 java.util.Properties 实例。

    在 Spring 3.1(不是 3.0...)中,您可以执行以下操作:

    @Configuration
    @PropertySource("/foo/bar/services.properties")
    public class ServiceConfiguration { 
    
        @Autowired Environment environment; 
    
        @Bean public javax.sql.DataSource dataSource( ){ 
            String user = this.environment.getProperty("ds.user");
            ...
        } 
    }
    

    在 Spring 3.0 中,您可以使用 SpEl 注释“访问”使用 PropertyPlaceHolderConfigurer 机制定义的属性:

    @Value("${ds.user}") private String user;
    

    如果您想一起删除 XML,只需使用 Java 配置手动注册 PropertyPlaceholderConfigurer。我更喜欢 3.1 的方法。但是,如果您使用的是 Spring 3.0 方法(因为 3.1 还不是 GA...),您现在可以像这样定义上述 XML:

    @Configuration 
    public class MySpring3Configuration {     
            @Bean 
            public static PropertyPlaceholderConfigurer configurer() { 
                 PropertyPlaceholderConfigurer ppc = ...
                 ppc.setLocations(...);
                 return ppc; 
            } 
    
            @Bean 
            public class DataSource dataSource(
                    @Value("${ds.user}") String user, 
                    @Value("${ds.pw}") String pw, 
                    ...) { 
                DataSource ds = ...
                ds.setUser(user);
                ds.setPassword(pw);                        
                ...
                return ds;
            }
    }
    

    请注意,PPC 是使用static bean 定义方法定义的。这是确保 bean 尽早注册所必需的,因为 PPC 是一个BeanFactoryPostProcessor - 它可以影响 bean 本身在上下文中的注册,因此它必须在其他一切之前注册。

    【讨论】:

    • 但在 Spring 3.1 示例中您将再次需要 @Bean public static PropertySourcePlaceholderConfigurer pspc()
    • 这是最好的答案。一个快速说明:如果您使用 Spring >= 3.1,请使用 PropertySourcesPlaceholderConfigurer 而不是旧的 PropertyPlaceholderConfigurer。它更好地支持环境和 Spring 3.1 中引入的所有新东西。
    【解决方案2】:

    首先,您不需要同时定义这两个位置。只需使用classpath:config/properties/database.properties。在 WAR 中,WEB-INF/classes 是一个类路径条目,所以它可以正常工作。

    在那之后,我想你的意思是你想use Spring's schema-based configuration to create a configurer。会是这样的:

    <context:property-placeholder location="classpath:config/properties/database.properties"/>
    

    请注意,您不再需要“ignoreResourceNotFound”。如果需要使用util:properties单独定义属性:

    <context:property-placeholder properties-ref="jdbcProperties" ignore-resource-not-found="true"/>
    

    不过,通常没有任何理由单独定义它们。

    【讨论】:

    • 1) 如果我定义了
    • util:properties 只创建java.util.Properties 类型的bean。它不进行占位符替换。进行替换的最简单方法是在 XML 中使用 context:property-placeholder。如果您愿意,您可以在没有 XML 的情况下创建和配置您自己的 PropertyPlaceholderConfigurer 实例——例如使用 Java 配置——但我不确定是否有人会认为这是一种更好的方法。
    【解决方案3】:

    以下对我有用:
    &lt;context:property-placeholder location="file:src/resources/spring/AppController.properties"/&gt;

    不知何故“classpath:xxx”没有选择文件。

    【讨论】:

    • 您的 ide 配置包含 {src/resources} 作为类路径项之一。因此,将您的文件放置为 {src/resources/spring/AppController.properties},该文件的类路径引用将是“classpath:spring/AppController.properties”。
    猜你喜欢
    • 2012-11-23
    • 1970-01-01
    • 1970-01-01
    • 2018-03-20
    • 1970-01-01
    • 1970-01-01
    • 2013-06-22
    • 2014-05-01
    • 1970-01-01
    相关资源
    最近更新 更多