情景:因為不可能所有的屬性都放在全局文件(SpringBoot項目使用一個全局的配置文件application.properties或者是application.yml)中
所以需要把一些跟springboot無關的東西放在其他文件,用 @PropertySource:加載指定的配置文件;
直接用@PropertySource讀取不到值,原因在於@PropertySource需要和@propertyConfiguration一起使用
@Component
@ConfigurationProperties(prefix = "person")
最終要注解內容有:
@Component
@ConfigurationProperties(prefix = "person")
@PropertySource(value={"classpath:/person.properties"})