简体   繁体   中英

How to teach findbugs to understand IoC fields properly?

This is my class (JAX-RS annotated):

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

This is what findbugs says:

Unwritten field: com.XXX.Foo.uriInfo

It's true, the field is unwritten, but it is injected by 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. It tells me that this variable is not accessible from outside of the class, and my annotation is not valid in terms of OOP. The JAX-RS servlet will have to break field access restrictions in order to inject 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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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