PropertyPlaceholderConfigurer

PropertyPlaceholderConfigurer

本文介绍了春天PropertyPlaceholderConfigurer和context:property-placeholder的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下bean声明:

I have following bean declaration:

  <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更改为以下格式:

Now I want to change above PropertyPlaceholderConfigurer to following format:

<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文件(给定上方)来加载数据库内容.
  1. ignoreResourceNotFound will ignore the property while running. e.g:When testing application WEB-INF/.. path will ignore( since mavenproject and property file is under src/main/resources/..), whilelaunching web application, other property will ignore path, I needto implement same with above format.
  2. should be able to add multiple property file likedatabase.properties, test.properties etc.
  3. in Spring 3, can I use annotation instead of these xml files for DBloading, how can I do it? since I am using only one xml file(givenabove) to load db stuff.

我正在使用Spring 3框架.

I am using Spring 3 framework.

推荐答案

<context:property-placeholder ... />是与PropertyPlaceholderConfigurer等效的XML.所以,更喜欢那个. <util:properties/>只是工厂化一个您可以注入的java.util.Properties实例.

<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.

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

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

@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机制定义的属性:

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

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

如果要一起删除XML,只需使用Java配置手动注册PropertyPlaceholderConfigurer.我更喜欢3.1方法.但是,如果您使用的是Spring 3.0方法(因为3.1尚不支持GA ...),您现在可以按以下方式定义上述XML:

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:

@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定义方法定义的.这是确保豆类尽早注册所必需的,因为PPC是BeanFactoryPostProcessor-它会在上下文中影响豆类本身的注册,因此必须先进行其他所有注册.

Note that the PPC is defined using a static bean 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.

这篇关于春天PropertyPlaceholderConfigurer和context:property-placeholder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 07:03