Search code examples
javahibernatespringmaven-2

PropertyPlaceholderConfigurer vs Filters -- Spring Beans


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

I've been looking at examples, and it seems that even though filters are defined and marked to be active by default in the pom.xml they still make use of PropertyPlaceholderConfigurer in Spring's applicationContext.xml.

This means that the pom.xml has a reference to a filter-LOCAL.properties while applicationContext.xml has a reference to application.properties and they both contain the same settings.

Why is that? Is that how it is supposed to be done? I'm able to run the goal mvn jetty:run without the application.properties present, but if I add settings to the application.properties that differ from the 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>

an example of the 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

Can I remove the propertyConfigurer from the applicationContext, create a PROD filter and disregard the application.properties file, or will that give me issues when deploying to the production server?


Solution

  • You should rather use Maven to select which Spring properties file to use depending on which environment you're building for.

    When you're testing in your IDE, you should just start the Spring container from the test without using Maven for anything else than managing your dependencies.