当前位置:网站首页>Field injection is not recommended using @ Autowired
Field injection is not recommended using @ Autowired
2022-04-23 04:49:00 【wyplj_ sir】
List of articles
Use @Autowired There is a warning in the code :

as a result of Spring Field based dependency injection is not recommended
It can be changed to dependency injection based on constructor ,idea Can automatically generate , Too many injected classes will lead to too many parameters of the constructor ( It's a signal , Indicates that there are too many dependencies )
You can also use @Resource, Just no warning
Make a distinction @Autowired and @Resource The difference between the two notes :
(1)、@Autowired The default in accordance with the byType In a way that bean matching ,@Resource The default in accordance with the byName In a way that bean matching
(2)、@Autowired yes Spring Annotations ,@Resource yes J2EE Annotations
Field injection is not recommended(Spring IOC Attribute injection is no longer recommended )
Use @Autowired Note warning Field injection is not recommended
@Autowired Note tips Field injection Warning
@Autowire The injected object may be null
Example :
@Component
@Transactional
public class XXService {
private Logger logger = LoggerFactory.getLogger(getClass());
@Autowired
private XXDao xxDao;
public void add(XX xxl) {
logger.debug(" Get into XXService.add(XX xx) Method ");
xxDao.save(xx); // Null pointer is reported here , as a result of xxDao by null
}
Tracing to the source :
@RestController
@RequestMapping(value = "/xxx/xx")
public class XXController {
Logger logger = LoggerFactory.getLogger(getClass());
/* Here through new To get an instance that leads to service Injected dao by null; Must be used in all dao The place of , Including calling it servcie To carry out @Autowired Inject , Otherwise, the subsequent injection will fail */
private XXService xxService = new XXService();
.....
}
reason :
@Autowired When injecting, the class is handed over to Springboot management , and new The examples are separated from Springboot Management of , Two things are not managed by one manager , So it can't be connected ,@Autowired Injection will be null.
@Autowire The object of injection is null
ps:Mapper No need to comment @Component It can also work ,spring Will be able to @MapperSacn Scan all configured packages ;@EnableFeignClients By scanning the path @FeignClient Annotated interfaces are injected IOC Containers .
版权声明
本文为[wyplj_ sir]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220555178520.html
边栏推荐
- [pytoch foundation] torch Split() usage
- leetcode004--罗马数字转整数
- Summary of MySQL de duplication methods
- leetcode001--返回和为target的数组元素的下标
- Windows remote connection to redis
- Teach you how to build the ruoyi system by Tencent cloud
- Spark FAQ sorting - must see before interview
- Flink's important basics
- leetcode009--用二分查找在数组中搜索目标值
- No such file or directory problem while executing shell
猜你喜欢

What is a data island? Why is there still a data island in 2022?

/etc/bash_ completion. D directory function (the user logs in and executes the script under the directory immediately)

Supplement 14: cmake practice project notes (to be continued 4 / 22)

Simply drag objects to the item bar

New terminal play method: script guidance independent of technology stack

【数据库】MySQL多表查询(一)

Learning Android from scratch -- Introduction

QML advanced (V) - realize all kinds of cool special effects through particle simulation system

Experience summary and sharing of the first prize of 2021 National Mathematical Modeling Competition

MySQL queries users logged in for at least N consecutive days
随机推荐
[timing] empirical evaluation of general convolution and cyclic networks for sequence modeling based on TCN
Create VPC in AWS console (no plate)
POI export message list (including pictures)
Programmers complain: I really can't live with a salary of 12000. Netizen: how can I say 3000
Spell it! Two A-level universities and six B-level universities have abolished master's degree programs in software engineering!
JS generates a specified number of characters according to some words
Customize the navigation bar at the top of wechat applet (adaptive wechat capsule button, flex layout)
Leetcode002 -- inverts the numeric portion of a signed integer
MySQL time function query
Learning Android from scratch -- Introduction
Unity摄像头跟随鼠标旋转
/etc/bash_ completion. D directory function (the user logs in and executes the script under the directory immediately)
Graduation project
leetcode006--查找字符串数组中的最长公共前缀
Unity rawimage background seamlessly connected mobile
Eight misunderstandings that should be avoided in data visualization
Agile practice | agile indicators to improve group predictability
IEEE Transactions on industrial information (TII)
The unity camera rotates with the mouse
Recommended scheme of national manufactured electronic components