当前位置:网站首页>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
边栏推荐
- C language: spoof games
- Record the ThreadPoolExecutor main thread waiting for sub threads
- Practice and exploration of knowledge map visualization technology in meituan
- Go reflection rule
- Com alibaba. Common methods of fastjson
- L2-011 play binary tree (build tree + BFS)
- C# List字段排序含有数字和字符
- Luogu p1858 [multi person knapsack] (knapsack seeking the top k optimal solution)
- Spark FAQ sorting - must see before interview
- Leetcode001 -- returns the subscript of the array element whose sum is target
猜你喜欢
Small volume Schottky diode compatible with nsr20f30nxt5g
Practice and exploration of knowledge map visualization technology in meituan
[timing] empirical evaluation of general convolution and cyclic networks for sequence modeling based on TCN
Innovation training (V) configuration information
拼了!两所A级大学,六所B级大学,纷纷撤销软件工程硕士点!
Simply drag objects to the item bar
Customize the navigation bar at the top of wechat applet (adaptive wechat capsule button, flex layout)
Use model load_ state_ Attributeerror appears when dict(): 'STR' object has no attribute 'copy‘
Recommended scheme for national production of electronic components of wireless keyboard
View analysis of scenic spots in ArcGIS
随机推荐
IEEE Transactions on systems, man, and Cybernetics: Notes for systems (TSMC)
[timing] empirical evaluation of general convolution and cyclic networks for sequence modeling based on TCN
MySQL - data read / write separation, multi instance
The object needs to add additional attributes. There is no need to add attributes in the entity. The required information is returned
List remove an element
PIP3 installation requests Library - the most complete pit sorting
泰克示波器DPO3054自校准SPC失败维修
Installation and deployment of Flink and wordcount test
Sword finger offer: the path with a certain value in the binary tree (backtracking)
redis数据类型有哪些
C list field sorting contains numbers and characters
leetcode002--将有符号整数的数字部分反转
Innovation training (VII) FBV view & CBV view
Key points of AWS eks deployment and differences between console and eksctl creation
Unity3d practical skills - theoretical knowledge base (I)
Leetcode003 -- judge whether an integer is a palindrome number
leetcode008--实现strStr()函数
做数据可视化应该避免的8个误区
Gets all dates between two times
Leetcode 1547: minimum cost of cutting sticks