Spring Boot @Autowired -CheckStyle / PMD / Spotbugs规则在非接口使用情况下警告
最近被 Spring 咬住了Spring Boot 应用程序中的 CGLib 与 JDK 动态代理问题(在 Mac 上运行时似乎使用 CGLib,而在 Linux 上运行时则使用 JDK 动态代理)。
我正在寻找一个 linter 工具配置/插件(PMD、Checkstyle、Spotbugs 等),它可以识别使用 @Autowired 注释字段/参数的位置,并且自动装配的类是使用类实例而不是接口定义的。
例如:
public interface MyService {
}
public class MyServiceImpl implements MyService {
}
@RestController
public class MyController {
@Autowired
private MyServiceImpl serviceImpl; // this should raise a warning in a linter as
// class is being used for injection type
@Autowired
private MyService service; // this should not raise a warning, as interface
// is used for type
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为这是不可能与PMD,CheckStyle,Spotbugs等进行检查。但是有一个解决方案可以检查此问题。
Archunit 提供编写自定义规则,可以为此编写自定义规则。假设您使用Junit5或Junit4进行测试,则可以做类似的事情 -
可以将其纳入您的测试中,您必须阅读ARCH单元的文档并在工作环境中设置,它非常容易集成。您可以为设定器或方法创建类似的规则 -
将其集成到代码库中时,可以为自己创建自定义规则。将Archunit用于春季项目通常是一个好主意,它们还具有可以在项目中使用的预配置模板。希望这会有所帮助。
I don't think this will be possible to check with PMD, Checkstyle, Spotbugs, etc. But there is a solution to check this.
ArchUnit offers writing custom rules, a custom rule can be written for this. Assuming you are using JUnit5 or JUnit4 for testing, you could do something like-
This can be incorporated within your tests, you would have to read the documentation of arch unit and set up in your work environment, it super easy to integrate. You can create similar rules for setters or methods-
As you integrate this into your code base, you can create custom rules for yourself. Using ArchUnit for spring projects is generally a great idea, they also have pre configured templates that you can use in your project. Hope this helps.
除了上面的 @vyomydv的答案外,我还偶然发现了春季的这个问题:
本质上是用
@validated
和@configurationproperties
和实现接口将是在运行时成为JDK代理实例的候选者,并且您不能自动自动化类实例,您必须自动作为接口。因此,修改Yyomydv的规则如下:
In addition to @VyomYdv's answer above, I stumbled on this github issue for spring: https://github.com/spring-projects/spring-boot/issues/8277
Essentially a bean that is annotated with
@Validated
and@ConfigurationProperties
and also implements an interface will be a candidate for being a JDK proxy instance at runtime, and you can't autowire the class instance, you have to autowire as the interface.So amend YyomYdv's rule as follows: