Search code examples
nullpointerexceptionejbwildflyfinalwildfly-10

Method declared as final causes @EJB to be null, why?


I have the following method. When the method is declared final the @EJB is null. Why is this so? It works, when I omit the final keyword.

@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
public class TokenService2
{
    @EJB
    private Configuration configuration;

    public final void processAuthentication(String authCode) throws FileNotFoundException, IOException
    {
        //here configuration is NULL.
    }
}

(The configuration itself is a singleton)


Solution

  • Ejb beans do not like any other mehod modifier apart from public for business methods(methods invocable by clients). Although this particular case looks like a bug in Wildfly. Wildfly 10 is JavaEE7 compliant, therefore its EJB container should conform the EJB 3.2 spec that says the following about session beans:

    Only public methods of the bean class and of any superclasses except java.lang.Object may be invoked through the no-interface view. Attempted invocations of methods with any other access modifi- ers via the no-interface view reference must result in the javax.ejb.EJBException

    So your business method cant be final anyhow, but your server should inform you about that. .