简体   繁体   English

如何教findbugs正确理解IoC字段?

[英]How to teach findbugs to understand IoC fields properly?

This is my class (JAX-RS annotated): 这是我的班级(JAX-RS注释):

@Path("/")
public class Foo {
  @Context
  private UriInfo uriInfo;
  // ...
}

This is what findbugs says: 这就是findbugs所说的:

Unwritten field: com.XXX.Foo.uriInfo

It's true, the field is unwritten, but it is injected by JAX-RS servlet. 确实,该字段是不成文的,但它是由JAX-RS servlet注入的。 I think that I'm doing something wrong here, but how to solve the problem? 我认为我在这里做错了什么,但如何解决这个问题呢?

What I've understand so far is that findbugs is right. 到目前为止我所了解的是,findbugs是对的。 It tells me that this variable is not accessible from outside of the class, and my annotation is not valid in terms of OOP. 它告诉我这个变量不能从类的外部访问,并且我的注释在OOP方面无效。 The JAX-RS servlet will have to break field access restrictions in order to inject UriInfo . JAX-RS servlet必须打破字段访问限制才能注入UriInfo I have to give him a legal way to this field: 我必须给他这个领域的合法途径:

@Path("/")
public class Foo {
  private UriInfo uriInfo;
  @Context
  public void setUriInfo(UriInfo info) {
    this.uriInfo = info;
  }
  // ...
}

Now it's correct for findbugs and for OOP design paradigm :) 现在它对于findbugs和OOP设计范例是正确的:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM