首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >SpringBoot设置@Value注解获取不到值

SpringBoot设置@Value注解获取不到值

作者头像
tongyao
发布于 2022-06-09 15:00:28
发布于 2022-06-09 15:00:28
3.6K0
举报
文章被收录于专栏:tongyaotongyao

1.使用static或final修饰了tagValue,如下:

代码语言:javascript
复制
private static String tagValue; //错误
private final String tagValue; //错误

2.类没有加上@Component(或者@service等)

代码语言:javascript
复制
@Component //遗漏
class TestValue{
	@Value("${tag}")
	private String tagValue;
}

3.类被new新建了实例,而没有使用@Autowired

代码语言:javascript
复制
@Component
class TestValue{
	@Value("${tag}")
	private String tagValue;
}

class Test{
	...
	TestValue testValue = new TestValue()
}

正确方式: 上面这个testValue中肯定是取不到值的,必须使用@Autowired:

代码语言:javascript
复制
class Test{
	@AutoWired
	TestValue testValue;
	public void aa(){
		testValue.test();
	}
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021年3月10日,如有侵权请联系 cloudcommunity@tencent.com 删除
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档