Results 1 to 4 of 4

Thread: PropertyPlaceholder in Java Config HOWTO

  1. #1
    Join Date
    May 2005
    Location
    BEEK, The Netherlands
    Posts
    230

    Default 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. #2
    Join Date
    Jul 2010
    Location
    Venice, Italy
    Posts
    709

    Default

    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>

  3. #3
    Join Date
    May 2005
    Location
    BEEK, The Netherlands
    Posts
    230

    Default

    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.

  4. #4

    Lightbulb 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.
    javapda on forum

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •