当前位置:网站首页>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 .
边栏推荐
- Solve the problem that Anaconda environment cannot be accessed in PowerShell
- [set theory] relational closure (relational closure solution | relational graph closure | relational matrix closure | closure operation and relational properties | closure compound operation)
- Openresty best practices
- 1. 两数之和
- Project summary --2 (basic use of jsup)
- The programmer shell with a monthly salary of more than 10000 becomes a grammar skill for secondary school. Do you often use it!!!
- Leetcode solution - 02 Add Two Numbers
- Interesting research on mouse pointer interaction
- 使用conda创建自己的深度学习环境
- Apifix installation
猜你喜欢

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

Core principles and source code analysis of disruptor

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

Simple handwritten ORM framework

Jedis source code analysis (II): jediscluster module source code analysis

How to create and configure ZABBIX

Zhiniu stock project -- 05

Support vector machine for machine learning

Advanced technology management - do you know the whole picture of growth?

Migrate data from Mysql to tidb from a small amount of data
随机推荐
SQL实现将多行记录合并成一行
Method of converting GPS coordinates to Baidu map coordinates
MySQL带二进制的库表导出导入
Jedis source code analysis (I): jedis introduction, jedis module source code analysis
[set theory] equivalence relation (concept of equivalence relation | examples of equivalence relation | equivalence relation and closure)
【C#/VB.NET】 将PDF转为SVG/Image, SVG/Image转PDF
YOLOV1学习笔记
Simple solution of small up main lottery in station B
从小数据量分库分表 MySQL 合并迁移数据到 TiDB
JDBC connection database steps
Alibaba cloud OOS file upload
Skywalking8.7 source code analysis (II): Custom agent, service loading, witness component version identification, transform workflow
輕松上手Fluentd,結合 Rainbond 插件市場,日志收集更快捷
Detailed explanation of findloadedclass
ruoyi接口权限校验
Yum is too slow to bear? That's because you didn't do it
88. Merge two ordered arrays
Simple understanding of ThreadLocal
Kubernetes notes (IX) kubernetes application encapsulation and expansion
Mysql database table export and import with binary