当前位置:网站首页>Cannot get value with @value, null
Cannot get value with @value, null
2022-07-03 06:13:00 【Muyu】
In the use of @Value When , It can be downloaded from properties Get the value from the file , Such operation can enhance the decoupling of the program , When using this annotation today, even if the assignment , But I can only get it null
Major errors :
1. Not used on class @Component Annotations or other derived annotations , Make it a container ,spring You can't get it properties Value .
2. When calling this class , Not used @Autowired Make it inject automatically , It USES new The way , In this way, there is no value .
Here's the right way :
- Util class
...
@Component // Must be added
public class BmsUtil {
@Value("${file.uploadFolder}")
private String realBasePath;
...
}
- Service class
...
@Service // Must be added ,@Service yes @Component Derived notes for
public class BookServiceImpl implements BookService {
@Autowired
BookMapper bookMapper;
@Autowired // When calling a class , Need to use @Autowired Automatic injection
BmsUtil bmsUtil;
...
}
- properties
spring.thymeleaf.cache=false
# Set up sql The statement is output on the console
logging.level.com.muyu.bms.mapper=debug
#windows Upload image path
file.uploadFolder=E:/code/IDEA code/BMS/src/main/resources/static/upload
#linux Upload image path
#file.uploadFolder=/usr/local/tomcat/apache-tomcat/webapps/ROOT/WEB-INF/classes/static/upload
There is another case @Value You can't get the value , Is called in the constructor @Value Action variables , And assign a value to it .
================================
Look back at this problem after a long time , In fact, it has been well understood ,@Value yes Spring,IOC A way to inject parameters , So we have to add @Component Etc. derived annotation , Give it to Spring management ,@Autowired It's the same thing , Use this annotation Spring Will inject the value .
Call the constructor to create an object ,@Value The reason why the value cannot be obtained is , You didn't use Spring Method to get the object , I created an object myself , Naturally, you can't get it Spring The injected value .
边栏推荐
- 使用conda创建自己的深度学习环境
- Click cesium to obtain three-dimensional coordinates (longitude, latitude and elevation)
- SVN分支管理
- Get a screenshot of a uiscrollview, including off screen parts
- YOLOV3学习笔记
- Migrate data from Amazon aurora to tidb
- conda和pip的区别
- It is said that the operation and maintenance of shell scripts are paid tens of thousands of yuan a month!!!
- Leetcode solution - 01 Two Sum
- 技术管理进阶——你了解成长的全貌吗?
猜你喜欢

从小数据量 MySQL 迁移数据到 TiDB

Merge and migrate data from small data volume, sub database and sub table Mysql to tidb

项目总结--01(接口的增删改查;多线程的使用)

GPS坐标转百度地图坐标的方法

. Net program configuration file operation (INI, CFG, config)

arcgis创建postgre企业级数据库

伯努利分布,二项分布和泊松分布以及最大似然之间的关系(未完成)

Selenium ide installation recording and local project maintenance

Deep learning, thinking from one dimensional input to multi-dimensional feature input

Project summary --04
随机推荐
Kubernetes notes (10) kubernetes Monitoring & debugging
YOLOV2学习与总结
Kubernetes notes (I) kubernetes cluster architecture
Oauth2.0 - user defined mode authorization - SMS verification code login
认识弹性盒子flex
最大似然估计,散度,交叉熵
pytorch 搭建神经网络最简版
Why should there be a firewall? This time xiaowai has something to say!!!
Detailed explanation of findloadedclass
Creating postgre enterprise database by ArcGIS
Nacos service installation
Leetcode solution - 01 Two Sum
Mysql
【C#/VB.NET】 将PDF转为SVG/Image, SVG/Image转PDF
JDBC connection database steps
GPS坐标转百度地图坐标的方法
Characteristics and isolation level of database
The server data is all gone! Thinking caused by a RAID5 crash
Oauth2.0 - use database to store client information and authorization code
Kubernetes notes (II) pod usage notes