@Value给属性赋值:比如配置文件里application.properties里aaa.bbb=100
那么
@Value"${aaa.bbb}"
private String ab;
这样ab就有值了
当然该类要由Spring管理
@PropertisSource该注解用在类上,专门读取.properties文件,比如新建个abc.properties文件在resources目录下 里面写着 ccc.ddd=200,这样@Value无法得到这个值,原因,SpringBoot默认读的是application.propertis,这个时候要获取这个值就需要用@PropertisSource这个注解:如下
@Controller
@PropertiesSource(value="classpath:abc.properties")
public class Abc{
@value("${ccc.ddd}")
private String cd;//这样cd就有值了
@Value("{aaa.bbb}")
private String ab;//如果你需要读取application.properties的内容也是可以的(默认)
}
关于@ConfigurationProperties(prefix="xxxxx")的用法,它的作用是给对象赋值
比如application.properties里有
stu.name=lisi
stu.age=23
用法如下
@Component
@ConfigurationProperties(prefix="stu")
public class Student{
private String name;
private Integer age;
}
那么我就可以在控制器
@Controller
public class TestController{
@Resources
private Student student;//该student有值
}
注意:@ConfigurationProperties(prefix="stu") 里的stu只是和properties里的
stu.name=lisi
stu.age=23 对应,并不会去改变默认Spring容器Bean池中的引用名