尽管创建了Bean,EJB注入仍然无法正常工作

时间:2017-06-22 09:46:37

标签: java ejb cdi jndi

我有以下两个EJB

    @Stateless
public class EmployeeBean extends AbstractFacade<Employee>
        implements BeanInterface<Employee>{


      public EmployeeBean() {
            super(Employee.class);
        }
    }

        @Stateless
    public class UserBean extends AbstractFacadeBean<User> 
implements BeanInterface<User> {
        public UserBean() {
            super(User.class);
        }

业务界面

@Local
public interface BeanInterface<T> {

    BeanResponse create(T entity);
}

和AbstractFacade

    public abstract class AbstractFacadeBean<T>{

    private Class<T> type;

    public AbstractFacade(Class<T> type) {
        this.type = type;
    }
    public BeanResponse create(T entity) {
        return null;
    }
}

这是我尝试注入EJB的地方

  @Path("/employee")
public class EmployeeAPI implements APIInterface<Employee> {


    @EJB
    private BeanInterface<Employee> bean;
}

现在从服务器日志中我可以看到bean已创建

    INFO    2017-06-22 12:15:01,904 [MSC service thread 1-7] org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor  - JNDI bindings for session bean named EmployeeBean in deployment unit deployment "malison.war" are as follows:

    java:global/malison/EmployeeBean!com.malison.co.ke.model.enterpriseBeans.BeanInterface
    java:app/malison/EmployeeBean!com.malison.co.ke.model.enterpriseBeans.BeanInterface
    java:module/EmployeeBean!com.malison.co.ke.model.enterpriseBeans.BeanInterface
    java:global/malison/EmployeeBean
    java:app/malison/EmployeeBean
    java:module/EmployeeBean

INFO    2017-06-22 12:15:01,913 [MSC service thread 1-7] org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor  - JNDI bindings for session bean named UserBean in deployment unit deployment "malison.war" are as follows:

    java:global/malison/UserBean!com.malison.co.ke.model.enterpriseBeans.BeanInterface
    java:app/malison/UserBean!com.malison.co.ke.model.enterpriseBeans.BeanInterface
    java:module/UserBean!com.malison.co.ke.model.enterpriseBeans.BeanInterface
    java:global/malison/UserBean
    java:app/malison/UserBean
    java:module/UserBean

但是每当尝试在运行时访问EJB时,我都会收到以下错误

 ERROR   2017-06-22 12:20:43,325 [default task-4] io.undertow.request  - UT005023: Exception handling request to /malison/api/employee/create
org.jboss.resteasy.spi.UnhandledException: java.lang.IllegalArgumentException: WFLYWELD0044: Error injecting resource into CDI managed bean. Can't find a resource named java:comp/env/com.malison.co.ke.persons.employee.restws.EmployeeAPI/bean
    at org.jboss.resteasy.core.ExceptionHandler.handleApplicationException(ExceptionHandler.java:76)
    at org.jboss.resteasy.core.ExceptionHandler.handleException(ExceptionHandler.java:212)
    at org.jboss.resteasy.core.SynchronousDispatcher.writeException(SynchronousDispatcher.java:149)
    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:372)
    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:179)
    at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:220)
    at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56)
    at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:51)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:86)
    at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
    at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
    at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)
    at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.security.handlers.AuthenticationConstraintHandler.handleRequest(AuthenticationConstraintHandler.java:51)
    at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
    at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
    at io.undertow.servlet.handlers.security.ServletSecurityConstraintHandler.handleRequest(ServletSecurityConstraintHandler.java:56)
    at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:58)
    at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:72)
    at io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
    at io.undertow.security.handlers.SecurityInitialHandler.handleRequest(SecurityInitialHandler.java:76)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:282)
    at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:261)
    at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:80)
    at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:172)
    at io.undertow.server.Connectors.executeRootHandler(Connectors.java:199)
    at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:774)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalArgumentException: WFLYWELD0044: Error injecting resource into CDI managed bean. Can't find a resource named java:comp/env/com.malison.co.ke.persons.employee.restws.EmployeeAPI/bean
    at org.jboss.as.weld.services.bootstrap.WeldEjbInjectionServices.doLookup(WeldEjbInjectionServices.java:240)
    at org.jboss.as.weld.services.bootstrap.WeldEjbInjectionServices$1.createResource(WeldEjbInjectionServices.java:113)
    at org.jboss.weld.injection.AbstractResourceInjection.getResourceReference(AbstractResourceInjection.java:44)
    at org.jboss.weld.injection.AbstractResourceInjection.injectResourceReference(AbstractResourceInjection.java:53)
    at org.jboss.weld.util.Beans.injectEEFields(Beans.java:348)
    at org.jboss.weld.injection.producer.ResourceInjector$1.proceed(ResourceInjector.java:69)
    at org.jboss.weld.injection.InjectionContextImpl.run(InjectionContextImpl.java:48)
    at org.jboss.weld.injection.producer.ResourceInjector.inject(ResourceInjector.java:72)
    at org.jboss.weld.injection.producer.BasicInjectionTarget.inject(BasicInjectionTarget.java:121)
    at org.jboss.resteasy.cdi.JaxrsInjectionTarget.inject(JaxrsInjectionTarget.java:44)
    at org.jboss.weld.bean.ManagedBean.create(ManagedBean.java:159)
    at org.jboss.weld.context.AbstractContext.get(AbstractContext.java:96)
    at org.jboss.weld.bean.ContextualInstanceStrategy$DefaultContextualInstanceStrategy.get(ContextualInstanceStrategy.java:101)
    at org.jboss.weld.bean.ContextualInstanceStrategy$CachingContextualInstanceStrategy.get(ContextualInstanceStrategy.java:178)
    at org.jboss.weld.bean.ContextualInstance.get(ContextualInstance.java:50)
    at org.jboss.weld.bean.proxy.ContextBeanInstance.getInstance(ContextBeanInstance.java:99)
    at org.jboss.weld.bean.proxy.ProxyMethodHandler.getInstance(ProxyMethodHandler.java:125)
    at com.malison.co.ke.persons.employee.restws.EmployeeAPI$Proxy$_$$_WeldClientProxy.create(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:137)
    at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTarget(ResourceMethodInvoker.java:296)
    at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:250)
    at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:237)
    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:356)
    ... 34 more
Caused by: javax.naming.NameNotFoundException: env/com.malison.co.ke.persons.employee.restws.EmployeeAPI/bean -- service jboss.naming.context.java.module.malison.malison.env."com.malison.co.ke.persons.employee.restws.EmployeeAPI".bean
    at org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:106)
    at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:207)
    at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:184)
    at org.jboss.as.naming.InitialContext$DefaultInitialContext.lookup(InitialContext.java:237)
    at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:193)
    at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:189)
    at javax.naming.InitialContext.lookup(InitialContext.java:417)
    at javax.naming.InitialContext.lookup(InitialContext.java:417)
    at org.jboss.as.weld.services.bootstrap.WeldEjbInjectionServices.doLookup(WeldEjbInjectionServices.java:238)

我认为这是我的应用程序或类似的JNDI配置问题。任何人都知道如何解决这个问题。什么是java:comp/env/的用途,因为它似乎是容器搜索bean的位置

1 个答案:

答案 0 :(得分:2)

在我们的案例中,当您使用@Inject而不是@EJB时会有帮助。 这是不同的机制