Springboot多个类中引用配置文件中属性(@Value(
"
x
x
x
"
)
导
致
的
报
错
:
C
o
u
l
d
n
o
t
r
e
s
o
l
v
e
p
l
a
c
e
h
o
l
d
e
r
′
∗
′
i
n
v
a
l
u
e
"
{"xxx"})导致的报错:Could not resolve placeholder '*' in value "
"xxx")导致的报错:Couldnotresolveplaceholder′∗′invalue"{xxx}"
解决:在对应类上加注解 @PropertySource(“classpath:xxx.properties”)
springboot可以直接通过在类中使用@Value获取配置文件*.properties中的字段属性。
@Value("${url}")
private String url;
但在多个类中使用时,会获取不到,并导致几种形式的报错和异常:
1.Could not resolve placeholder ‘*’ in value “${xxx}”
2.配置了swagger,没有异常,但swagger-ui.html页面无法访问(缺少部分初始化值导致页面不正常)。
根源:估计原因是spring的加载机制:Spring容器采用反射扫描的发现机制,在探测到Spring容器中有一个org.springframework.beans.factory.config.PropertyPlaceholderConfigurer的Bean就会停止对剩余PropertyPlaceholderConfigurer的扫描,这里两个类没有调用到同一个PropertyPlaceholderConfigurer类,导致第二个类中的@Value解析获取值的操作终止,从而报错。