首先,@Value需要参数,这里参数可以是两种形式:
- @Value("#{configProperties['t1.msgname']}")
- @Value("${t1.msgname}");
我项目中使用@Value注解读取yml文件中的配置
使用方式:
@Value("${tag}")
private String value
value 值为空的原因:
- 使用static或final修饰了tagValue,如下:
private static String value; //错误
private final String value; //错误
- 类没有加上@Component(或者@service等)
@Component //遗漏
class TestValue{
@Value("${tag}")
private String value;
}
- 使用@Autowired而不能通过new创建实例
class Test{
@AutoWired
TestValue testValue //正确
......
TestValue test = new TestValue();//错误
}
网友评论