Unable to inject a dao in Custom login module

1.1k Views Asked by At

I have a web application running in Wildfly which is using Spring and JPA. Now I am moving the login module of the application as a custom module in JBoss.

Code snippet is as below.

MyLoginModule

public class MyLoginModule extends AbstractServerLoginModule
{

private Principal caller;
private char[] credential;
private String[] roleList;

@Inject
@DaoQualifier
private Dao dao;    


@Override
public void initialize(Subject subject, CallbackHandler callbackHandler,
          Map sharedState, Map options) {
    super.initialize(subject, callbackHandler, sharedState, options);

    super.principalClassName = "com.myapp.login.LoginPrincipal";

}

 @Override   
 public boolean login() throws LoginException
 {


  logger.info("inside login "+dao);
  if (super.login())
  {
    ................
  }
  else
  {
      ............
   }
}

}

DaoImpl class as given below.

 public class DaoImpl implements Dao {
     @Inject
     private EntityManager em;

     //implementation methods
  }

Pom.xml dependencies

  <dependencies>        
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.3.5</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-core</artifactId>
        <version>4.0.0.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.picketbox</groupId>
        <artifactId>picketbox</artifactId>
        <version>4.0.21.Beta1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.jboss.logging</groupId>
        <artifactId>jboss-logging</artifactId>
        <version>3.1.4.GA</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.enterprise</groupId>
        <artifactId>cdi-api</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.1-api</artifactId>
        <scope>provided</scope>
    </dependency>        

beans.xml

<beans
   xmlns="http://xmlns.jcp.org/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
                  http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
    bean-discovery-mode="all">
</beans>

When this jar is deployed in JBoss/modules and server started, the dao object is always coming as null. Is there something missing in my code?

2

There are 2 best solutions below

0
Harald Wellmann On

Login modules aren't managed beans, so injection does not work. You have to look up your dependencies manually from JNDI or other suitable registries.

By the way, the built-in solution for dependency injection in Java EE 7 is CDI, so what's the point in using Spring?

0
Veselin On

as hwellmann said, login modules aren't managed beans. He is right about the manual lookup also. I'd like just to add a example code for the lookup:

public class CustomLoginModule extends AbstractServerLoginModule {

@Inject
AuthService authService;

@Override
public boolean login() throws LoginException {
    if (authService == null) {
      CdiHelper.programmaticInjection(CustomLoginModule.class, this);
    }

   authService.authenticate();
}...

The helper:

import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.AnnotatedType;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.InjectionTarget;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class CdiHelper {
  // Nicked from: http://docs.jboss.org/weld/reference/1.1.0.Final/en-US/html_single/#d0e5286
  public static <T> void programmaticInjection(Class clazz, T injectionObject) throws NamingException {
    InitialContext initialContext = new InitialContext();
    Object lookup = initialContext.lookup("java:comp/BeanManager");
    BeanManager beanManager = (BeanManager) lookup;
    AnnotatedType annotatedType = beanManager.createAnnotatedType(clazz);
    InjectionTarget injectionTarget = beanManager.createInjectionTarget(annotatedType);
    CreationalContext creationalContext = beanManager.createCreationalContext(null);
    injectionTarget.inject(injectionObject, creationalContext);
    creationalContext.release();
  }
}

I've quoted this form https://developer.jboss.org/thread/196807 just in case the original post disappears.