java - 我可以将 SessionBean 注入(inject) Java EE AroundInvoke-Interceptor 吗?

标签 java jboss dependency-injection jakarta-ee interceptor

我有一个带有模块的 EAR:

  • foo-api.jar
  • foo-impl.jar
  • 拦截器.jar

在 foo-api 中有:

@Local
FooService // (interface of a local stateless session bean)

在 foo-impl 中有:

@Stateless
FooServiceImpl implements FooService //(implementation of the foo service)

在我想要的 interceptor.jar 中

public class BazInterceptor {

  @EJB
  private FooService foo;

  @AroundInvoke
  public Object intercept( final InvocationContext i) throws Exception {
    // do someting with foo service
    return i.proceed();
  }

问题是:

Java EE 5 兼容应用程序服务器(例如 JBoss 5)是否会注入(inject)拦截器? 如果不是,访问 session bean 的好的策略是什么?

考虑:

  • 部署顺序/竞争条件

最佳答案

是的,注入(inject)应该发生在拦截器中,例如 An Introduction to the Java EE 5 Platform 中提到的文章(粗体是我的):

Easier Access to Resources Through Dependency Injection

Dependency injection is a pattern in which an object's dependencies are supplied automatically by an entity external to that object. The object is not required to request these resources explicitly, for example, by looking them up in a naming service. In the Java EE 5 platform, dependency injection can be applied to all resources that a component needs, effectively hiding the creation and lookup of resources from application code. Dependency injection can be applied throughout Java EE 5 technology -- in EJB software containers, web containers, and clients.

To request injection of a resource, a component uses the @Resource annotation or, in the case of some specialized resources, the @EJB and @WebServiceRef annotations. Following are some of the many resources that can be injected:

  • SessionContext object
  • DataSources object
  • UserTransaction
  • EntityManager interface
  • TimerService interface
  • Other enterprise beans
  • Web services
  • Message queues and topics
  • Connection factories for resource adapters
  • Environment entries (for example, strings, integers, and so on)

Resource injection can be requested by any component class, that is, any class whose life cycle is managed by the container. In the EJB software container, components that support injection include the following:

  • EJB technology components
  • Interceptors
  • Message handlers for Java API for XML Web Services (JAX-WS) and Java API for XML-based RPC (JAX-RPC)

In web containers, components that support injection are the following:

  • Servlets, servlet filters, event listeners
  • Tag handlers, tag library event listeners
  • Managed beans

In the client container, the main class and the login callback handler components support injection.

另见 EJB Interceptors JBoss EJB 3.0 教程部分:

Just like a bean class, an interceptor can be the target of Dependency injection. The format for how this works is the same, and the injection works off the same ENC as the bean to which the interceptor is bound.

...

Remember that interceptors follow the same lifecycle as the bean they are bound to. The interceptors are created at the same time as the bean instance is created, and dependency injection occurs before the first business method is called.

资源

关于java - 我可以将 SessionBean 注入(inject) Java EE AroundInvoke-Interceptor 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/906786/

相关文章:

c# - asp.net core DI - 我可以使服务依赖于其他服务吗

java - Spring Autowiring 在正在测试的类中不起作用

dependency-injection - 跨多个测试套件的 Scalatest 共享服务实例

Java:如何将文件副本从 vps 机器(可通过远程桌面访问)移动到我的本地电脑

java - Preffix_Suffix 设置为 int 数组 [编程]

java - JAVA Web 应用程序中基于表单的身份验证重试

java - 使用 JBoss 部署 REST 服务时出现的问题

java - 想要将 ArrayList<HashMap<String, Object>> 转换为字符串

java - 打开 Activity 期间应用程序崩溃

java - Spring 监听器未拾取 JMS 消息