Spring启动:无法使用@Value()注释读取资源包.properties文件

时间:2016-05-16 13:22:49

标签: spring-boot

我用spring MVC编写了项目代码。我切换到弹簧靴。 我使用下面的代码来设置资源包属性文件之一:

@Bean(name = "appConfig")
public ReloadableResourceBundleMessageSource appConfig() {
    ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
    messageSource.setBasename("classpath:/appConfig");
    messageSource.setCacheSeconds(10); 
    return messageSource;
}

我运行代码,我得到以下错误:

Indexing into type 'org.springframework.context.support.ReloadableResourceBundleMessageSource' is not supported

On guy推荐这段代码:

 @Bean(name = "appconfig")
public PropertiesFactoryBean appconfig() {
    PropertiesFactoryBean bean = new PropertiesFactoryBean();
    bean.setLocation(new ClassPathResource("appConfig.properties"));
    return bean;
}

我收到此错误:

Property or field 'appConfig' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe not public?

我做错了什么?

1 个答案:

答案 0 :(得分:0)

我已经忘记/了 在  bean.setLocation(new ClassPathResource("appConfig.properties"));

这段代码对我有用。

@Bean(name = "appConfig")
public PropertiesFactoryBean appconfig() {
    PropertiesFactoryBean bean = new PropertiesFactoryBean();
    bean.setLocation(new ClassPathResource("/appConfig.properties"));
    return bean;
}

:)