普通字符串
@Value("fengzai")
String str;
@Value("fengzai") 直接把字符串赋值给当前字段
占位符
@Value("${name}")
String name;
先进行占位符的替换,然后将替换后的字符串赋值给当前字段
根据操作系统环境变量,jvm环境变量,properties文件,作为替换的来源
SpringEL
@Value("#{name}")
User user;
先解析Spring表达式,将结果赋值给当前字段,解析的结果可能是字符串,也可能是一个Bean对象
基于@Value做扩展
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Value("$(name)")
public @interface Name {
}
自定义一个注解,在使用的字段上使用该注解可以达赋值的效果.
@Name
String name;