关于@Value取值为NULL的解决方案
作者:zzmlake 时间:2021-07-15 06:38:42
@Value取值为NULL的问题
在spring mvc架构中,如果希望在程序中直接使用properties中定义的配置值,通常使用一下方式来获取:
@Value("${tag}")
private String tagValue;
但是取值时,有时这个tagvalue为NULL,可能原因有:
使用static或final修饰了tagValue,如下:
private static String tagValue; //错误
private final String tagValue; //错误
类没有加上@Component(或者@service等)
@Component //遗漏
class TestValue{
@Value("${tag}")
private String tagValue;
}
类被new新建了实例,而没有使用@Autowired
@Component
class TestValue{
@Value("${tag}")
private String tagValue;
}
class Test{
...
TestValue testValue = new TestValue()
}
这个testValue中肯定是取不到值的,必须使用@Autowired:
class Test{
@AutoWired
TestValue testValue
}
@Value取值为NULL原因分析
有两种方式:
@Value(“${}”)
用于获取配置文件中的属性值,通常用于获取写在application.properties中的内容;@Value(“#{}”)
其实是SpEL表达式的值,可以表示常量的值,或者获取bean中的属性
区别:
① ${ property : default_value }
//property对应外部配置文件,default_value,就是前面的值为空时的默认值。② #{ obj.property? :default_value }
//SpEL表达式,obj代表对象
一.@Value(“${}”)的使用
@Value("${inputDir}")
private String inputDir;
但有时候@Value(“${}”)取值为NULL,可能是由下面几个原因造成的:
1.类没有交给spring管理,即没有加上@Component等注解
@Service
public class TestValue{
@Value("${inputDir}")
private String inputDir;
……
}
2.使用 static或final修饰成员变量
@Value("${inputDir}")
private static String inputDir;//错误,不能使用@Value给static成员变量赋值
@Value("${inputDir}")
private final String inputDir;//错误,不能使用@Value给final成员变量赋值
3.自己new了一个对象实例,而没有使用@Autowired注解
class Test{
@AutoWired
TestValue testValue
//TestValue testValue = new TestValue()//错误,自己new的对象不能通过@Value注解获取配置值。
}
二.@Value{“#{}”}的使用
@RestController
@RequestMapping("/login")
@Component
public class LoginController {
@Value("#{1}")
private int number; //获取数字 1
@Value("#{'Spring Expression Language'}") //获取字符串常量
private String str;
@Value("#{dataSource.url}") //获取bean的属性,dataSource为spring管理的obj,不是配置文件中的配置项
private String jdbcUrl;
@Autowired
private DataSourceTransactionManager transactionManager;
@RequestMapping("login")
public String login(String name,String password) throws FileNotFoundException{
System.out.println(number);
System.out.println(str);
System.out.println(jdbcUrl);
return "login";
}
}
运行结果
来源:https://blog.csdn.net/zzmlake/article/details/54946346
标签:@Value,取值,NULL
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java数据结构-堆实现优先队列
2023-11-25 08:30:20
![](https://img.aspxhome.com/file/2023/5/59335_0s.jpg)
C#调用带结构体指针Dll的方法
2022-06-14 19:55:32
详解Java中的线程池
2023-11-10 16:33:27
![](https://img.aspxhome.com/file/2023/9/59369_0s.png)
Java读写Windows共享文件夹的方法实例
2022-10-02 02:25:45
Spring Boot中使用JDBC Templet的方法教程
2021-09-06 06:04:38
![](https://img.aspxhome.com/file/2023/3/84273_0s.png)
Unity实现俄罗斯方块(二)
2022-01-15 06:57:56
![](https://img.aspxhome.com/file/2023/0/69780_0s.jpg)
Java泛型变量如何添加约束
2021-06-02 23:12:06
java 实现线程同步的方式有哪些
2023-01-13 01:26:14
SpringMVC多个文件上传及上传后立即显示图片功能
2021-06-26 12:41:08
Apache Commons fileUpload实现文件上传之一
2022-12-06 12:36:48
Java中List常用操作比for循环更优雅的写法示例
2023-08-08 23:25:14
C# 开发step步骤条控件详解
2021-07-01 00:53:14
![](https://img.aspxhome.com/file/2023/0/80040_0s.png)
C#快速排序算法实例分析
2023-06-17 00:30:00
java读取word-excel-ppt文件代码
2022-07-06 14:20:46
Android开发仿映客送礼物效果
2023-03-24 01:11:12
![](https://img.aspxhome.com/file/2023/3/93293_0s.gif)
Volatile关键字的使用案例
2022-01-27 16:37:05
![](https://img.aspxhome.com/file/2023/4/86054_0s.png)
Java ArrayList中存放引用数据类型的方式
2023-11-16 15:23:46
![](https://img.aspxhome.com/file/2023/6/60556_0s.png)
一文带你学会Java事件机制
2022-07-22 05:05:11
springboot 防止重复请求防止重复点击的操作
2021-09-19 16:03:00
![](https://img.aspxhome.com/file/2023/6/71196_0s.png)
C#使用Redis的基本操作
2023-12-03 11:03:51
![](https://img.aspxhome.com/file/2023/7/79127_0s.png)