Michael Locher
Michael Locher

Reputation: 165

Can I inject a SessionBean into a Java EE AroundInvoke-Interceptor?

I have an EAR with modules:

In foo-api there is:

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

In foo-impl there is:

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

In interceptor.jar I want

public class BazInterceptor {

  @EJB
  private FooService foo;

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

The question is:

Will a Java EE 5 compliant application server (e.g. JBoss 5) inject into the interceptor? If no, what is good strategy for accessing the session bean?

To consider:

Upvotes: 6

Views: 6952

Answers (2)

Pascal Thivent
Pascal Thivent

Reputation: 570385

Yes, injection should occur in an interceptor, as mentioned for example in An Introduction to the Java EE 5 Platform article (bold is mine):

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.

See also the EJB Interceptors section of the JBoss EJB 3.0 Tutorial:

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.

Resources

Upvotes: 7

Andrew
Andrew

Reputation:

My experience has indicated the injection can only occur in managed classes: EJB (Entity, Session or Message) and Servlets.

Upvotes: -1

Related Questions