简体   繁体   English

注册 ResourceBundleMessageSource bean 时,Spring 4 @PropertySource 不起作用

[英]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. ApplicationConfig 类中的 bean 之前的属性加载不起作用。 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:

@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:您 messageSource 是错误的,假设您的 authentication.properties 在 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假设 config.properties 在 src/resources

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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