SpringBoot升级全纪录之Maven打包:
https://blog.csdn.net/qq_38254635/article/details/140986084
1、问题
在项目升级版本过程中,@Autowired提示Field injection is not recommended
Field injection is not recommended
Inspection info:
Reports injected or autowired fields in Spring components.
The quick-fix suggests the recommended constructor-based dependency injection in beans and assertions for mandatory fields.
Example:
class MyComponent {
@Inject MyCollaborator collaborator; // injected field
public void myBusinessMethod() {
collaborator.doSomething(); // throws NullPointerException
}
}
After applying the quick-fix:
class MyComponent {
private final MyCollaborator collaborator;
@Inject
public MyComponent(MyCollaborator collaborator) {
Assert.notNull(collaborator, "MyCollaborator must not be null!");
this.collaborator = collaborator;
}
public void myBusinessMethod() {
collaborator.doSomething(); // now this call is safe
}
}
2、原因
重点应该在这一句
The quick-fix suggests the recommended constructor-based dependency injection in beans and assertions for mandatory fields.
其实就是要基于构造函数进行依赖注入,估计有可能会在未来某个版本会删除该用法吧。
3、解决
把 import org.springframework.beans.factory.annotation.Autowired;
替换成
import jakarta.annotation.Resource;
问题解决。
OK,就这些吧。
有什么不对的还望指正,书写不易,觉得有帮助就点个赞吧!☺☺☺