Announcement Announcement Module
Collapse
No announcement yet.
PropertyPlaceholder in Java Config HOWTO Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • PropertyPlaceholder in Java Config HOWTO

    I'm investigating moving from Spring xml config to a Java config.

    Currently we use PropertyPlaceholderConfigurer like

    <contextroperty-placeholder location="classpath*:META-INF/config.properties,classpath*:META-INF/config-${com.google.appengine.runtime.environment}.proper ties" />

    Where ${com.google.appengine.runtime.environment} comes from system property.
    So in fact we have a default config.properties which properties might be overridden with properties from e.g. config-Development.properties.

    We use this e.g. to set the properties like:

    {code}
    <bean class="org.springframework.web.context.support.Ser vletContextAttributeExporter">
    <property name="attributes">
    <map>
    <entry key="google.analytics.enabled" value="${google.analytics.enabled}" />
    <entry key="google.analytics.webPropertyID" value="${google.analytics.webPropertyID}" />
    {code}

    Now I'm wondering how we should do this with the new Java Config features?

  • #2
    From the Spring reference documentation, page 109 and following:

    @Configuration class-centric use of XML with @ImportResource

    In applications where @Configuration classes are the primary mechanism for configuring the container, it will still likely be necessary to use at least some XML. In these scenarios, simply use @ImportResource and define only as much XML as is needed. Doing so achieves a "Java-centric" approach to configuring the container and keeps XML to a bare minimum.
    Code:
    @Configuration
    @ImportResource("classpath:/com/acme/properties-config.xml")
    public class AppConfig {
    private @Value("${jdbc.url}") String url;
    private @Value("${jdbc.username}") String username;
    private @Value("${jdbc.password}") String password;
    
    public @Bean DataSource dataSource() {
        return new DriverManagerDataSource(url, username, password);
    }
    }
    Code:
    properties-config.xml
    <beans>
    <context:property-placeholder location="classpath:/com/acme/jdbc.properties"/>
    </beans>

    Comment


    • #3
      Thanks Enrico,

      I found this page as well but was wondering if I can get rid of XML completely...
      As Spring is advertising for Spring 3.1 I believe.

      Comment


      • #4
        Property placeholder configuration without xml - all in JavaConfig

        Try the following in 3.1.0 using the new PropertySourcesPlaceholderConfigurer. Notice that the bean is declared static.

        Code:
            static @Bean public PropertySourcesPlaceholderConfigurer myPropertySourcesPlaceholderConfigurer() {
                PropertySourcesPlaceholderConfigurer p = new PropertySourcesPlaceholderConfigurer();
                Resource[] resourceLocations = new Resource[] {
                        new ClassPathResource("my.custom.properties"),
                        new ClassPathResource("my.other.properties"),
                };
                p.setLocations(resourceLocations);
                return p;
            }
        Example: my.custom.properties:
        Code:
        country.name=Netherlands
        city.name=Amsterdam
        Then in your spring configuration you can reference ${country.name} and ${city.name} which will subsequently be replaced with 'Netherlands' and 'Amsterdam', respectively.

        Comment

        Working...
        X