spring PropertyPlaceholderConfigurer 和 context:property-placeholder

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7542506/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 04:40:06  来源:igfitidea点击:

spring PropertyPlaceholderConfigurer and context:property-placeholder

spring

提问by TechFind

I have following bean declaration:

我有以下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>

Now I want to change above PropertyPlaceholderConfigurer to following format:

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

<context:component-scan base-package="org.example.config"/>
<util:properties id="jdbcProperties" 
           location="classpath:config/properties/database.properties"/>
  1. ignoreResourceNotFound will ignore the property while running. e.g: When testing application WEB-INF/.. path will ignore( since maven project and property file is under src/main/resources/..), while launching web application, other property will ignore path, I need to implement same with above format.
  2. should be able to add multiple property file like database.properties, test.properties etc.
  3. in Spring 3, can I use annotation instead of these xml files for DB loading, how can I do it? since I am using only one xml file(given above) to load db stuff.
  1. ignoreResourceNotFound 将在运行时忽略该属性。例如:当测试应用程序 WEB-INF/.. 路径将忽略(因为 maven 项目和属性文件在 src/main/resources/.. 下),而在启动 web 应用程序时,其他属性将忽略路径,我需要实现与以上格式。
  2. 应该能够添加多个属性文件,如 database.properties、test.properties 等。
  3. 在 Spring 3 中,我可以使用注释而不是这些 xml 文件来加载数据库,我该怎么做?因为我只使用一个 xml 文件(上面给出)来加载 db 的东西。

I am using Spring 3 framework.

我正在使用 Spring 3 框架。

回答by Josh Long

<context:property-placeholder ... />is the XML equivalent to the PropertyPlaceholderConfigurer. So, prefer that. The <util:properties/>simply factories a java.util.Properties instance that you can inject.

<context:property-placeholder ... />是等同于 PropertyPlaceholderConfigurer 的 XML。所以,更喜欢那个。在<util:properties/>简单的工厂一个java.util.Properties实例可以注入。

In Spring 3.1 (not 3.0...) you can do something like this:

在 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");
        ...
    } 
}

In Spring 3.0, you can "access" properties defined using the PropertyPlaceHolderConfigurer mechanism using the SpEl annotations:

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

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

If you want to remove the XML all together, simply register the PropertyPlaceholderConfigurer manually using Java configuration. I prefer the 3.1 approach. But, if youre using the Spring 3.0 approach (since 3.1's not GA yet...), you can now define the above XML like this:

如果您想一起删除 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;
        }
}

Note that the PPC is defined using a staticbean definition method. This is required to make sure the bean is registered early, because the PPC is a BeanFactoryPostProcessor- it can influence the registration of the beans themselves in the context, so it necessarily has to be registered before everything else.

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

回答by Ryan Stewart

First, you don't need to define both of those locations. Just use classpath:config/properties/database.properties. In a WAR, WEB-INF/classesis a classpath entry, so it will work just fine.

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

After that, I think what you mean is you want to use Spring's schema-based configuration to create a configurer. That would go like this:

在那之后,我想你的意思是你想使用Spring的基于模式的配置来创建一个配置器。那会是这样的:

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

Note that you don't need to "ignoreResourceNotFound" anymore. If you need to define the properties separately using util:properties:

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

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

There's usually not any reason to define them separately, though.

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

回答by Vibha

Following worked for me:
<context:property-placeholder location="file:src/resources/spring/AppController.properties"/>

Somehow "classpath:xxx" is not picking the file.

以下对我
<context:property-placeholder location="file:src/resources/spring/AppController.properties"/>

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