PropertyPlaceholderConfigurer vs Filters - Spring Beans

I have a question regarding the difference between PropertyPlaceholderConfigurer (org.springframework.beans.factory.config.PropertyPlaceholderConfigurer) and the normal filters defined in my pom.xml.

I've looked at examples and it seems that even though filters are defined and marked as active by default in the pom.xml, they still use the PropertyPlaceholderConfigurer in Spring's applicationContext.xml.

This means that pom.xml refers to filter-LOCAL.properties and applicationContext.xml refers to application.properties, and both contain the same settings.

Why? Is this how it should be done? I can run the mvn jetty target: run without application.properties present, but if I add settings to the application.properties properties that are different from filter-LOCAL.properties they don't seem to override.

Here's an example of what I mean:

pom.xml

    <profiles>  
        <profile>  
            <id> LOCAL  
            <activation>  
                <activeByDefault> true  
            </activation>   
            <properties>  
                <env> LOCAL  
            </properties>  
        </profile>  
    </profiles>

applicationContext.xml

    <bean id = "propertyConfigurer" class = "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name = "locations">
            <list>
                <value> classpath: application.properties
            </list>
        </property>
        <property name = "ignoreResourceNotFound" value = "true" />
    </bean>

    <bean id = "dataSource" class = "org.apache.commons.dbcp.BasicDataSource" destroy-method = "close">
        <property name = "driverClassName" value = "$ {jdbc.driver}" />
        <property name = "url" value = "$ {jdbc.url}" />
        <property name = "username" value = "$ {jdbc.username}" />
        <property name = "password" value = "$ {jdbc.password}" />
    </bean>

sample content of application.properties and filters-LOCAL.properties

jdbc.driver = org.postgresql.Driver
jdbc.url = jdbc: postgresql: // localhost / shoutbox_dev
jdbc.username = tester
jdbc.password = tester

Is it possible to remove the Configurer property from the applicationContext, create a PROD filter and ignore the application.properties file, or will this give me problems when deploying to a production server?

+2


a source to share


2 answers


You should use Maven to choose which Spring properties file to use depending on the environment you are building for.



When you are testing in your IDE, you should simply start the Spring container from the test without using Maven for anything other than dependency management.

+2


a source


For the record, here's what the author of the blog series the OP is talking about wrote in this comment :

I've been a big fan of Spring s PropertyPlaceholderConfigurer

, but ever since I started using maven I don't find it useful as mavens filters using a filter file as explained here, or by multiple profiles in a pump for different deployment levels with each profile specifying properties.

The biggest problem I am facing PropertyPlaceholderConfigurer

is that you can only have one PropertyPlaceholderConfigurer

bean. And it's not well documented.

With mavens filter files, you can do whatever you want.

Another reason I prefer mavens filters is that with them you can do an 'Mvn package and then pop into the target directory and eyeball of the filtered config files and see what it does. With Spring s PropertyPlaceholderConfigurer

you won't know what has been replaced until the application is running.



I will remember this opinion and prefer the filter approach using PropertyPlaceholderConfigurer

and the Antrun plugin to copy the say test.properties

to application.properties

when running my tests. And using filtered resources is well supported by all major IDEs (Eclipse, IntelliJ, NetBeans), so I don't understand why I shouldn't use it.

+2


a source







All Articles