Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have an EAR with modules:

  • foo-api.jar
  • foo-impl.jar
  • interceptor.jar

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:

  • Deployment ordering / race conditions
like image 688
Michael Locher Avatar asked May 25 '09 13:05

Michael Locher


People also ask

What is @inject in Java EE?

It allows us to define injection points in the client classes. In this case, @Inject instructs CDI to inject an ImageFileEditor implementation in the constructor. Furthermore, it's also possible to inject a service by using the @Inject annotation in fields (field injection) and setters (setter injection).

What is use of interceptor in Java?

Interceptors are used in conjunction with Java EE managed classes to allow developers to invoke interceptor methods on an associated target class, in conjunction with method invocations or lifecycle events. Common uses of interceptors are logging, auditing, and profiling.

How do you call an interceptor in Java?

Use the @AroundInvoke annotation to designate interceptor methods for managed object methods. Only one around-invoke interceptor method per class is allowed. Around-invoke interceptor methods have the following form: @AroundInvoke visibility Object method-name(InvocationContext) throws Exception { ... }

What is CDI interceptor?

An interceptor is a class used to interpose in method invocations or lifecycle events that occur in an associated target class. The interceptor performs tasks, such as logging or auditing, that are separate from the business logic of the application and are repeated often within an application.


1 Answers

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

  • An Introduction to the Java EE 5 Platform
  • EJB 3.0 Tutorial
    • EJB Interceptors (see the Injection section)
like image 112
Pascal Thivent Avatar answered Sep 30 '22 18:09

Pascal Thivent