当前位置:网站首页>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 .
边栏推荐
- Openresty best practices
- Cesium entity(entities) 实体删除方法
- Characteristics and isolation level of database
- 从 Amazon Aurora 迁移数据到 TiDB
- Es remote cluster configuration and cross cluster search
- 有意思的鼠标指针交互探究
- Fluentd is easy to use. Combined with the rainbow plug-in market, log collection is faster
- 智牛股项目--05
- CKA certification notes - CKA certification experience post
- MySQL带二进制的库表导出导入
猜你喜欢
![[system design] proximity service](/img/4a/2e68536cbe385af1d1a591e674fbf0.png)
[system design] proximity service

Disruptor learning notes: basic use, core concepts and principles

Tabbar settings

Kubernetes notes (III) controller

Clickhouse learning notes (I): Clickhouse installation, data type, table engine, SQL operation

Une exploration intéressante de l'interaction souris - pointeur

Code generator - single table query crud - generator

SVN分支管理

Creating postgre enterprise database by ArcGIS

Migrate data from Mysql to tidb from a small amount of data
随机推荐
Cesium entity (entities) entity deletion method
智牛股项目--04
Get a screenshot of a uiscrollview, including off screen parts
Detailed explanation of contextclassloader
arcgis创建postgre企业级数据库
Oauth2.0 - using JWT to replace token and JWT content enhancement
项目总结--04
Bio, NiO, AIO details
The server data is all gone! Thinking caused by a RAID5 crash
多线程与高并发(7)——从ReentrantLock到AQS源码(两万字大章,一篇理解AQS)
致即将毕业大学生的一封信
Kubernetes notes (IV) kubernetes network
Decision tree of machine learning
Mysql database table export and import with binary
Naive Bayes in machine learning
Clickhouse learning notes (I): Clickhouse installation, data type, table engine, SQL operation
How to create and configure ZABBIX
YOLOV1学习笔记
Migrate data from Amazon aurora to tidb
Oracle Database Introduction