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

发布于 2024-10-03 02:45:35 字数 336 浏览 8 评论 0原文

这是我的类(JAX-RS注释):

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

这就是 findbugs 所说的:

Unwritten field: com.XXX.Foo.uriInfo

这是真的,该字段是未写入的,但它是注入的通过 JAX-RS servlet。我认为我在这里做错了什么,但是如何解决这个问题呢?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北音执念 2024-10-10 02:45:35

到目前为止我所了解的是 findbugs 是正确的。它告诉我这个变量无法从类外部访问,并且我的注释在 OOP 方面无效。 JAX-RS servlet 必须打破字段访问限制才能注入 UriInfo。我必须给他一个进入这个领域的合法途径:

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

现在对于 findbugs 和 OOP 设计范式来说这是正确的:)

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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文