当前位置:网站首页>Field injection is not recommended
Field injection is not recommended
2022-07-22 03:53:00 【The carnival of the lonely】
Reference documents :https://www.cnblogs.com/wang-yaz/p/9340156.html
Reference documents :http://www.pianshen.com/article/5023380349/
First of all, we need to understand the use @Autowired Three ways to inject :
@Service("accountEmailService")
2 public class AccountEmailServiceImpl implements AccountEmailService{
3
4
5 /** Injection by constructor ---begin **/
6 private JavaMailSender javaMailSender;
7
8 @Autowired
9 public AccountEmailServiceImpl(JavaMailSender javaMailSender){
10 this.javaMailSender = javaMailSender;
11 }
12
13
14 /** adopt set Methods to inject ---begin **/
15 private JavaMailSender javaMailSender;
16 @Autowired
17 public void setJavaMailSender(JavaMailSender javaMailSender){
18 this.javaMailSender = javaMailSender;
19 }
20
21
22 /** adopt field Inject **/
23 @Autowired
24 private JavaMailSender javaMailSender;
25
Spring It is recommended to use constructor injection :
Spring Team recommends: “Always use constructor based dependency injection in your beans. Always use assertions for mandatory dependencies”.
because field Injection may occur NullPointerException
class MyComponent {
@Inject MyCollaborator collaborator;
public void myBusinessMethod() {
collaborator.doSomething(); // -> NullPointerException
}
}
However, constructor injection is not abandoned because the code is not concise enough . In fact, we have been using attribute injection without error . So there are two solutions :
The first one is : take “@Autowired” Replace notes with “@Resouce” annotation .
@Resource yes JSR-250 Notes in the specification , at present Spring The specification is supported [email protected] It's based on Bean To match the name of Bean, If you can't get it, match it according to the type ; and @Autowired Match according to type , Passing the name requires Spring Of @Qualifier Annotations to cooperate .
The second kind : modify setting In the face field Inspection of injection :setting->editor->code style->inspection-> spring->spring core-> code, Remove the hook :
边栏推荐
- 数据模型子类化参考
- eBPF验证器
- “F5G+EIoT“构建能源物联网,助力电力物联网数据服务
- OneManager与CloudFlare Workers部署安装-绑定域名和使用CloudFlare CDN加速
- seaborn绘制箱线图和折线图
- 数据中心末端直流配电系统产品的应用
- Sequences, Time Series and Prediction in Tessorflow quizs on Coursera (一)
- Comptroller in compound, part 105
- Browser cache mechanism resolution
- Fiddler oS. Invalid problem of utilsetresponsebody
猜你喜欢
How to make the full-color LED display screen energy-saving and environmental protection?
IDEA 2020.1 取消参数名称显示
PAM4科普
Yii2 render custom template file
Code management (novice)
Learning notes of Stanford CV course (I)
企业进行知识管理有哪些好处?
Alibaba programmer, working for 6 years, real salary exposure
百度副总裁李硕:AI能深入场景创造真价值,从传感器到大屏仅是数字化开始...
NPM and the development history of yarn
随机推荐
System
NFTScan 与 Port3 在 NFT 数据领域达成战略合作
清理systemd日志
How to build a good knowledge base management system?
Su Chunyuan, founder of science and technology · CEO of Guanyuan data: making business use is the key to the Bi industry to push down the wall of penetration
数据中心末端直流配电系统产品的应用
ANSVC无功补偿装置助力江苏某环保能源项目
Luogu - changing classrooms - (probability expectation +dp)
Abstract class name as formal parameter and return value
Design and application of prepaid platform on power reform policy
Arrays
asp.net core、c#关于路径的总结
从0开始实现一个代理池
抽象类名作为形参和返回值
获取模块句柄/基址
Talking about NPM construction process and package JSON and package lock. Workflow of JSON
PCI Express物理层 - 电气部分
After reading "what to read in college?"
【2022年中总结】我走得很慢,但我从不后退
He has been in charge of the British Society of engineering and technology for 13 years, and van nugget officially retired