Ari*_*nen 14 autowired spring-boot spotbugs
在我的 Spring Boot 应用程序中,我使用com.github.spotbugs:spotbugs-maven-plugin插件。Spotbug 检查报告以下类没有问题:
@Service
public class FooService {
@Autowired
CocoComponent cocoComponent;
@PostConstruct
public void init() {
System.out.println(cocoComponent.getGreeting() + " world!");
}
}
Run Code Online (Sandbox Code Playgroud)
这很好用。然而,由于自动装配的字段并不意味着在注入后会发生变化,所以我更愿意将它们声明为最终的。像这样:
@Service
public class BarService {
final CocoComponent cocoComponent;
public BarService(CocoComponent cocoComponent) {
this.cocoComponent = cocoComponent;
}
@PostConstruct
public void init() {
System.out.println(cocoComponent.getGreeting() + " world!");
}
}
Run Code Online (Sandbox Code Playgroud)
问题是,spotbugs 检查BarService课堂上的报告问题:
[ERROR] Medium: new xxx.nnn.BarService(CocoComponent) may expose internal representation by storing an externally mutable object into BarService.cocoComponent [xxx.nnn.BarService] At BarService.java:[line 14] EI_EXPOSE_REP2
Run Code Online (Sandbox Code Playgroud)
当然,我可以:
FooService@SuppressFBWarnings("EI_EXPOSE_REP2")但是,恕我直言,两者都不理想。
我的主要问题:有没有一种方法可以配置 Spotbugs,使其不因EI_EXPOSE_REP2在另一个对象中存储带注释的类@Component(以及任何派生类,例如@Service、@Repository、 ..)的对象而引发?
或者(但不是那么理想):有没有一种方法可以配置 Spotbugs,使其不会因通过构造函数EI_EXPOSE_REP2在带注释的类@Component(以及任何派生类,例如@Service、@Repository、 ..)的实例上存储可变对象而引发?我想我可以使用过滤器文件,但是,据我所知,没有与注释匹配的过滤器,所以它将基于包或类名模式,这不太漂亮。
还有其他建议可以避免污染代码吗@SuppressFBWarnings?
小智 0
使用下面 URL 中的信息作为提示,我能够抑制构造函数注入期间发生的 EI_EXPOSE_REP2 警告。(可能是有点粗糙的解决方案)
https://spotbugs.readthedocs.io/ja/latest/filter.html
Spotbugs 过滤器设置文件
<FindBugsFilter ...>
...
...
<Match>
<Method name="<init>"/>
<Bug pattern="EI_EXPOSE_REP2" />
</Match>
...
...
</FindBugsFilter>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4020 次 |
| 最近记录: |