
- 配置文件
- 处理方法
当项目的配置文件外置了(比如放入nacos统一管理了),就不要再在类上面使用此注解,不然会报错:class path resource [XXX.properties] cannot be opened because it does not exist
处理方法去掉此注解,如果该类下面需要获取配置文件的内容,直接使用@Value("${XXX}")就可以。
注意
在配置类中使用此注解可能会获取不到值,如下图
或多个@PropertySource注释(Java 8)。如下图
或者
@Configuration
@ComponentScan
class ApplicationConfig {
@Bean
public static PropertySourcesPlaceholderConfigurer placeholderConfigurer() {
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
configurer.setLocation(new ClassPathResource("application_dev.properties"));
return configurer;
}
}
注意:下面针对spring+shiro情况讲解(其他情况可能也可以以类似的方法解决)
在spring整shiro时会创建LifecycleBeanPostProcessor的bean,LifecycleBeanPostProcessor是Bean的后置处理器,它的初始化比一般的bean初始化要早,所以如果LifecycleBeanPostProcessor bean放在@Configuration配置文件中,并且还和其他的普通bean放在一个目录下就会导致bean加载的一些问题。
解决方法有两个:
1、使用static方法注册后置处理器
2、LifecycleBeanPostProcessor单独在一个@Configuration配置文件中
我使用的是第一种
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)