当前位置:网站首页>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
边栏推荐
- Code007 -- determine whether the string in parentheses matches
- Leetcode008 -- implement strstr() function
- Go reflection - go language Bible learning notes
- QML advanced (V) - realize all kinds of cool special effects through particle simulation system
- Progress of innovation training (IV)
- Spell it! Two A-level universities and six B-level universities have abolished master's degree programs in software engineering!
- KVM error: Failed to connect socket to ‘/var/run/libvirt/libvirt-sock‘
- Basic operation of sequence table
- Innovation training (V) configuration information
- Recommended scheme for national production of electronic components of wireless keyboard
猜你喜欢
Eight misunderstandings that should be avoided in data visualization
New terminal play method: script guidance independent of technology stack
[paper reading] [3D target detection] point transformer
Leetcode 1547: minimum cost of cutting sticks
Innovation training (IX) integration
用LCR表完美测试无线充电系统中的线圈
Excel uses the functions of replacement, sorting and filling to comprehensively sort out financial data
Use model load_ state_ Attributeerror appears when dict(): 'STR' object has no attribute 'copy‘
Recommended scheme for national production of electronic components for wireless charging
C language: Advanced pointer
随机推荐
Innovation training (10)
Unity camera rotation with sliding effect (rotation)
Unity攝像頭跟隨鼠標旋轉
Introduction to raspberry pie 3B - system installation
Recommended scheme of national manufactured electronic components
Innovation training (IX) integration
Go reflection rule
Innovation training (XI) airline ticket crawling company information
PIP3 installation requests Library - the most complete pit sorting
C language: spoof games
Excel protects worksheets and workbooks from damage
Leetcode004 -- Roman numeral to integer
Learning Android from scratch -- Introduction
Spark optimization
IEEE Transactions on industrial information (TII)
解决ValueError: Argument must be a dense tensor: 0 - got shape [198602], but wanted [198602, 16].
Small volume Schottky diode compatible with nsr20f30nxt5g
Innovation training (IV) preliminary preparation - server
Solve valueerror: argument must be a deny tensor: 0 - got shape [198602], but wanted [198602, 16]
Better way to read configuration files than properties