0

We have an issue where we have a superclass with an annotation (@Resource) and subclasses that extend that superclass are are stateless beans.

Very similar to this, if the superclass and subclass are in the same module then the resource is injected successfully. If they are in separate modules then the resource is not injected - it seems the annotation is not processed.

I've verified through reflection that the annotation is "seen" on the class, but when I step into the ResourceInjectionAnnotationParsingProcessor, the @Resource annotation does not show on the classes that inherit from other modules (although it does show on classes that are in the same module).

The common solution I've been seeing is to jandex the files and set annotations="true" but this seems to be for static modules, not other deployments such as in our case.

The other suggestion was that the modules could be missing a dependency on the annotations, but in my example all modules involved had a dependency on <module name="javax.annotation.api"/>.

Is there any other way to make these annotations "visible" from separate deployments?

As a minimal example, if you have a superclass

import javax.annotation.Resource;
import javax.ejb.SessionContext;

public class BaseResource {
  @Resource
  private SessionContext sessionContext;

  public String getContext() {
    return "Context is " + sessionContext;
  }
}

and a subclass of

import javax.ejb.Stateless;

@Stateless
public class ResourceBean extends BaseResource {
  public ResourceBean() {
    System.out.println(getClass().getName() + " created");
  }
}

If these are in the same module, the SessionContext is displayed. In separate modules, SessionContext is always null.

Evan Knowles
  • 7,426
  • 2
  • 37
  • 71
  • You probably need to wrap it all into an EAR for JBoss to recognise the superclass. – djb May 14 '15 at 12:31
  • In our case there is one main project, and around 70 projects using it, so wrapping it in an `EAR` isn't feasible. Plus, JBoss recognizes the superclass, but the annotations aren't processed. – Evan Knowles May 14 '15 at 13:15
  • Does the subclass have @Inherited? (http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/annotation/Inherited.html) – djb May 14 '15 at 13:44
  • This looks useful: https://java.net/projects/javaee-spec/pages/AnnotationRules – djb May 14 '15 at 13:46
  • `@Resource` annotations are explicitly inherited despite not having `@Inherited` according to the spec - it does work when they're in the same module after all, so I don't think it's an inheritance thing. – Evan Knowles May 15 '15 at 06:01
  • Well i know you can't inject beans from modules that aren't in your EAR. JNDI won't even find them. So I'm not surprised that @Resource injection, which is probably also JNDI based, has trouble across modules. Can you access any JNDI bindings from the module with the superclass? Also, please elaborate a bit on the structure of your app: how do your 70 subprojects use the main project? Can you access any JNDI resources in the main project from a subproject? – djb May 15 '15 at 10:01
  • The injection isn't really the problem - I'm injecting a SessionContext, which doesn't come from one of my modules. – Evan Knowles May 15 '15 at 10:43

1 Answers1

0

You could just look up the SessionContext as a workaround. This is not an answer to your issue but you can at least get access to the SessionContext.

  public SessionContext getSessionContext()
  {
    try
    {
      InitialContext ic = new InitialContext();
      return (SessionContext) ic.lookup("java:comp/EJBContext");
    }
    catch (NamingException ex)
    {
      // handle exception
    }
    return null;
  }

Excerpt from: http://javahowto.blogspot.com/2006/06/4-ways-to-get-ejbcontext-in-ejb-3.html

Kenneth Clark
  • 1,725
  • 2
  • 14
  • 26