当前位置:网站首页>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
边栏推荐
- Detailed explanation of cookies and sessions
- Free travel recommendation in Bangkok: introduction to the Mekong River in Bangkok
- Method of applying for code signing certificate by enterprise
- Issue SSL certificate with IP address
- National Museum of Singapore - give you spiritual and physical satisfaction
- 【Paper】2021_ Observer-Based Controllers for Incrementally Quadratic Nonlinear Systems With Disturbanc
- Transport layer protocol tcp/udp
- [FPGA] IIC读写EEPROM 的实现
- Array of small C
- SSL universal domain name certificate
猜你喜欢
Redis implements SMS login function (II) redis implements login function
Transport layer protocol tcp/udp
Geotrustov wildcard
This connection is not a private connection this website may be pretending to steal your personal or financial information
Efficiency test of adding and querying ArrayList and LinkedList
Tea mall system based on SSM framework [project source code + database script + report]
Process architecture and process management
Have a heart beating Valentine's day in Singapore
FortiGate firewall and Aruze cloud tunnel interruption
National Museum of Singapore - give you spiritual and physical satisfaction
随机推荐
為什麼win10開熱點後電腦沒有網絡?
Serializable and Deserialize
【Paper】2021_ Observer-Based Controllers for Incrementally Quadratic Nonlinear Systems With Disturbanc
Software digital signature certificate
Unit screenshot saved on the phone
PBR material: basic principle and simple fabrication
The golden deer, a scenic spot in London -- a sailing museum that tells vivid sailing stories
Tea mall system based on SSM framework [project source code + database script + report]
The role of break
How to renew an SSL certificate
Why does the computer have no network after win10 is turned on?
Pourquoi l'ordinateur n'a - t - il pas de réseau après l'ouverture du Hotspot win10?
Free travel recommendation in Bangkok: introduction to the Mekong River in Bangkok
Summary of the reasons why transactional doesn't work
Method of applying for code signing certificate by enterprise
【Paper】2015_ Coordinated cruise control for high-speed train movements based on a multi-agent model
Qos(Quality of Service)
Implementation of one interview question one distributed lock every day
One interview question a day the difference between B tree and b+ tree
Internship: interface case implementation