springboot中@PropertySource(value = {"classpath:FoundBean.properties"})读取不出内容

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_31279347/article/details/84344789

情景:因为不可能所有的属性都放在全局文件中,所以需要把一些跟springboot无关的东西放在其他文件,用 @PropertySource:加载指定的配置文件;

所以我在javabean中直接用了此注解

@PropertySource(value = {"classpath:bean.properties"}) 
@Component public class FoundBean {
    xxx 
}

在springboot单元测试中,发现并没有读取到内容,后来发现此注解只是加载进全局文件,所以还需要另一个注解(@ConfigurationProperties(prefix = “person”))读取内容

@ConfigurationProperties(prefix = "FoundBean")
@PropertySource(value = {"classpath:bean.properties"})
@Component
public class FoundBean {
    xxx
}

测试成功

猜你喜欢

转载自blog.csdn.net/qq_31279347/article/details/84344789