当前位置:网站首页>Autowired注解警告的解决办法
Autowired注解警告的解决办法
2022-06-30 04:45:00 【轩辕龙儿】
@AutoWired 在idea报警告
近期,发现@AutoWired注解在idea中总是报警告
java代码
如下:
@Controller
public class UserController {
@Autowired
private UserService userService;
}
警告内容
如下:

解决办法
于是乎,关联性的在网上找了找资料,用以下的写法不会报警告,同时这种写法也是spring官方推荐的写法,代码如下:
@Controller
public class UserController {
private final UserService userService;
public UserController(UserService userService){
this.userService = userService;
}
}
Lombok优雅写法
@Controller
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public clas UserController {
//这里必须是final,若不使用final,用@NotNull注解也是可以的
private final UserService userService;
}
拓展学习
由此,我这边拓展到了spring的三种依赖注入方式:
Field Injection
Constructor Injection
Setter Injection
Field Injection
@Autowired注解的一大使用场景就是Field Injection。
具体形式如下:
@Controller
public class UserController {
@Autowired
private UserService userService;
}
这种注入方式通过Java的反射机制实现,所以private的成员也可以被注入具体的对象。
Constructor Injection
Constructor Injection是构造器注入,是我们日常最为推荐的一种使用方式。
具体形式如下:
@Controller
public class UserController {
private final UserService userService;
public UserController(UserService userService){
this.userService = userService;
}
}
这种注入方式很直接,通过对象构建的时候建立关系,所以这种方式对对象创建的顺序会有要求,当然Spring会为你搞定这样的先后顺序,除非你出现循环依赖,然后就会抛出异常。
Setter Injection
Setter Injection也会用到@Autowired注解,但使用方式与Field Injection有所不同,Field Injection是用在成员变量上,而Setter Injection的时候,是用在成员变量的Setter函数上。
具体形式如下:
@Controller
public class UserController {
private UserService userService;
@Autowired
public void setUserService(UserService userService){
this.userService = userService;
}
}
这种注入方式也很好理解,就是通过调用成员变量的set方法来注入想要使用的依赖对象。
三种依赖注入方式比较
| 注入方式 | 可靠性 | 可维护性 | 可测试性 | 灵活性 | 循环关系的检测 | 性能影响 |
|---|---|---|---|---|---|---|
| Field | 不可靠 | 低 | 差 | 很灵活 | 不检测 | 启动快 |
| Constructor | 可靠 | 高 | 好 | 不灵活 | 自动检测 | 启动慢 |
| Setter | 不可靠 | 低 | 好 | 很灵活 | 不检测 | 启动快 |
参考:
https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-constructor-injection
https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-setter-injection
利用Lombok编写优雅的spring依赖注入代码,去掉繁人的@Autowired_路遥知码农的博客-CSDN博客_lombok 依赖注入
https://segmentfault.com/a/1190000040914633
边栏推荐
- IO stream, byte stream read / write copy
- Five methods to clear floating and their advantages and disadvantages
- Learning about signals
- Threejs realizes the simulation of river, surface flow, pipe flow and sea surface
- Redis实现短信登入功能(一)传统的Session登入
- Redis实现短信登入功能(二)Redis实现登入功能
- Threejs实现模拟河流,水面水流,水管水流,海面
- What is SQL injection and how to avoid it?
- [从零开始学习FPGA编程-52]:高阶篇 - 基于IP核的FPGA开发 - IP核使用的基本框架(以锁相环PLL为例)
- 基于servlet+jsp+mysql实现的工资管理系统【源码+数据库】
猜你喜欢

Use of thread pool

Connect to the database and run node JS running database shows that the database is missing

Pourquoi l'ordinateur n'a - t - il pas de réseau après l'ouverture du Hotspot win10?

破局存量客群营销,试一下客户分群管理(含聚类模型等实操效果评估)

【Paper】2021_ Analysis of the Consensus Protocol of Heterogeneous Agents with Time-Delays

IIS request SSL certificate

Redis实现短信登入功能(一)传统的Session登入

Software digital signature certificate

【Paper】2021_ Observer-Based Controllers for Incrementally Quadratic Nonlinear Systems With Disturbanc

Process architecture and process management
随机推荐
【Paper】2006_ Time-Optimal Control of a Hovering Quad-Rotor Helicopter
Is the Flink connector JDBC open source? Where can I download it
【Paper】2013_ An efficient model predictive control scheme for an unmanned quadrotor helicopter
My idea configuration
Pourquoi l'ordinateur n'a - t - il pas de réseau après l'ouverture du Hotspot win10?
Internship: interface case implementation
PBR material: basic principle and simple fabrication
IO stream, buffer stream, automatic resource management
【Paper】2015_ Active fault-tolerant control system design with trajectory re-planning against actuator
Create a simple battle game with photon pun
The role of break
Redis implements SMS login function (I) traditional session login
【Paper】2015_ Coordinated cruise control for high-speed train movements based on a multi-agent model
[UGV] schematic diagram of UGV version 32
Differences between beanfactory and factorybean
One interview question every day to talk about the process of TCP connection and disconnection
Servlet lifecycle
How to apply for SSL certificate from the manufacturer
Winter vacation parent-child tour, these new york attractions are not only fun but also knowledge
Redis实现短信登入功能(二)Redis实现登入功能