1

How can I read the values of a properties file while using javabased configuration of a Spring 3.2 MVC application? My configuration class extends WebMvcConfigurationAdapter...

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.foo.bara" , excludeFilters = { @Filter( Configuration.class ) } )
@PropertySource( {"classpath:abc.properties", "classpath:persistence.properties" } )
public class MokaWebAppContext extends WebMvcConfigurerAdapter { 
    @Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer();
    Resource[] resources = new ClassPathResource[ ] {
            new ClassPathResource( "persistence.properties" ),
            new ClassPathResource( "abc.properties" )
    };
    pspc.setLocations( resources );
    pspc.setIgnoreUnresolvablePlaceholders( true );
    return pspc;
  }
    ...
}

When trying to access this resource whith

@Value('${persistence.db.driverClass}') private String driverClassName;

${persistence.db.driverClass} is not recognized.

What do I have to do to read values from properties files in a @Configuration class? I think I cannot use an Environment instance in this place, can I?

1 Answer 1

3

Do it the right way...and it works! You have to use double quotes rather than single quotes, of course:

@Value("${persistence.db.driverClass}") private String driverClassName;

rather than

@Value('${persistence.db.driverClass}') private String driverClassName;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.