简体   繁体   中英

Spring 4 @PropertySource not working when ResourceBundleMessageSource bean is registered

@Configuration
@PropertySource("classpath:service.properties")
public class ApplicationConfig{
    @Value("PROPERTY_MYSQL_JNDI_NAME")
    private String jndiName;
}

this was working fine. But when I register

@Bean
public MessageSource messageSource() {
    ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
    messageSource.setBasenames("resources/authentication");
    messageSource.setDefaultEncoding("UTF-8");
    return messageSource;
}

bean in the ApplicationConfig class previous property loading not working. is there any thing i can do to test it

I was facing the same issue. In my case, I had missed adding the following bean:

@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    return new PropertySourcesPlaceholderConfigurer();
}

which made the property loading work correctly.

You messageSource is wrong it should be as below assuming your authentication.properties is in src/resources:

@Bean
public MessageSource messageSource() {
    ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
    messageSource.setBasenames("authentication");
    messageSource.setDefaultEncoding("UTF-8");
    return messageSource;
}

You can still use

@PropertySource("classpath:config.properties")

Assuming config.properties is in src/resources

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM