当前位置:网站首页>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 .
边栏推荐
- Redis cluster creation, capacity expansion and capacity reduction
- 从小数据量分库分表 MySQL 合并迁移数据到 TiDB
- Svn branch management
- Exportation et importation de tables de bibliothèque avec binaires MySQL
- 智牛股项目--05
- Skywalking8.7 source code analysis (II): Custom agent, service loading, witness component version identification, transform workflow
- Convolution operation in convolution neural network CNN
- Naive Bayes in machine learning
- Zhiniu stock project -- 05
- Oracle database synonym creation
猜你喜欢
Disruptor learning notes: basic use, core concepts and principles
Understand the first prediction stage of yolov1
Solve the 1251 client does not support authentication protocol error of Navicat for MySQL connection MySQL 8.0.11
Kubernetes notes (IX) kubernetes application encapsulation and expansion
Tabbar settings
Oauth2.0 - user defined mode authorization - SMS verification code login
YOLOV3学习笔记
从小数据量 MySQL 迁移数据到 TiDB
Cesium Click to obtain the longitude and latitude elevation coordinates (3D coordinates) of the model surface
Support vector machine for machine learning
随机推荐
Project summary --04
Oracle Database Introduction
Cesium 点击获三维坐标(经纬度高程)
深度学习,从一维特性输入到多维特征输入引发的思考
Understand expectations (mean / estimate) and variances
It is said that the operation and maintenance of shell scripts are paid tens of thousands of yuan a month!!!
Solve the 1251 client does not support authentication protocol error of Navicat for MySQL connection MySQL 8.0.11
冒泡排序的简单理解
YOLOV3学习笔记
The server data is all gone! Thinking caused by a RAID5 crash
从 Amazon Aurora 迁移数据到 TiDB
Understand the first prediction stage of yolov1
BeanDefinitionRegistryPostProcessor
PMP笔记记录
Printer related problem record
When PHP uses env to obtain file parameters, it gets strings
1. Sum of two numbers
【C#/VB.NET】 将PDF转为SVG/Image, SVG/Image转PDF
Merge and migrate data from small data volume, sub database and sub table Mysql to tidb
Some thoughts on machine learning