java - 无法在自定义登录模块中注入(inject) dao

标签 java maven jboss wildfly weld2

我有一个在 Wildfly 中运行的 Web 应用程序,它使用 Spring 和 JPA。现在我将应用程序的登录模块移动为 JBoss 中的自定义模块。

代码片段如下。

我的登录模块

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 类如下所示。

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

     //implementation methods
  }

Pom.xml 依赖项

  <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>

当此 jar 部署在 JBoss/modules 中并启动服务器时,dao 对象始终为 null。我的代码中是否缺少某些内容?

最佳答案

正如 hwellmann 所说,登录模块不是托管 bean。他对手动查找的看法也是正确的。我只想添加一个用于查找的示例代码:

public class CustomLoginModule extends AbstractServerLoginModule {

@Inject
AuthService authService;

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

   authService.authenticate();
}...

助手:

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();
  }
}

我引用了此表格https://developer.jboss.org/thread/196807以防万一原始帖子消失。

关于java - 无法在自定义登录模块中注入(inject) dao,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29845689/

相关文章:

java - 我应该使用什么作为集合的内存屏障?

java - 在 Maven 构建的项目中启用断言

multithreading - 如何指定SBT在任何命令期间使用的线程数?

java - 带 https 的 jboss eap jax-ws 客户端失败

java - 编码差异 UTF-8 Android 4.2.2 <=> Windows 7

java - LongSummaryStatistics 为什么要实现 IntConsumer?

java - 使用 ajax primefaces 渲染 panelgrid

eclipse - m2e:m2e 不支持复制依赖

java - 这是 JBOSS 网络服务吗? Tomcat ?如何创建 server.xml 文件?

java - @FormParameter在ContainerRequestContext实体流中读取并设置相同数据后数据变为null