Spring注解驱动开发系列:
属性赋值
@Value
可以使用@Value标在属性上,给属性赋值,当然也可以标在方法、参数、注解上。
可以直接写值、SpEL(Spring Expression Language)、或者${}取值
示例
public class People {
@Value("${people.name}") // 从配置文件中取
private String name;
@Value("#{1+2}") // SpEL
private String id;
@Value("32") // 直接赋值
private int age;
}
需要注意的是,如果我们从配置文件中获取值,需要配合使用@PropertySource注解
@PropertySource
@PropertySource标注在(配置)类上,将配置文件导入,例如:
@PropertySource({ "classpath:people.properties" })
随后就可以从配置文件中读取值