使用EJB注释的依赖注入

时间:2012-04-09 08:27:49

标签: java java-ee jboss ejb

Simple WebSevice

@WebService
@Stateless
public class HistoryFormatterWebService implements IHistoryFormatterWebService
{
   @EJB
   private IHistoryFormatter historyFormatter;

   public byte[] formatHistory(final byte[] data)
   {
      System.err.println("hello from service");

      return null;
   }
}  

IHistoryFormatter

@Local
public interface IHistoryFormatter
{  
// ..  
}  

IHistoryFormatter是来自另一个项目的bean(query-ear.ear),但他也部署在jBoss上。
我的WebService正常部署在jBoss上。而jBoss写道

11:17:09,506 INFO  [JmxKernelAbstraction] installing MBean: jboss.j2ee:ear=wsformatter-ear-1.0-SNAPSHOT.ear,jar=wsformatter-ejb-1.0-SNAPSHOT.jar,name=HistoryFormatterWebService,service=EJB3 with dependencies:
11:17:09,568 INFO  [JmxKernelAbstraction]   jboss.j2ee:ear=query-ear.ear,jar=query-services.jar,name=HistoryFormatter,service=EJB3
11:17:09,568 INFO  [EJBContainer] STARTED EJB: com.wsformatter.HistoryFormatterWebService ejbName: HistoryFormatterWebService

但是当我向它发送请求时(使用SoapUI)我得到了异常

java.lang.RuntimeException: Non matching type for inject of field: private com.softcomputer.softlab.lquery.service.IHistoryFormatter com.softcomputer.wsformatter.HistoryFormatterWebService.historyFormatter for type: $Proxy250 of jndiName env/com.softcomputer.wsformatter.HistoryFormatterWebService/historyFormatter
intfs: , com.softcomputer.softlab.lquery.service.IHistoryFormatter, org.jboss.ejb3.JBossProxy
    at org.jboss.injection.JndiFieldInjector.inject(JndiFieldInjector.java:128)
    at org.jboss.injection.JndiFieldInjector.inject(JndiFieldInjector.java:106)
    at org.jboss.injection.JndiFieldInjector.inject(JndiFieldInjector.java:63)
    at org.jboss.ejb3.AbstractPool.create(AbstractPool.java:111)
    at org.jboss.ejb3.InfinitePool.get(InfinitePool.java:49)
    at org.jboss.ejb3.ThreadlocalPool.create(ThreadlocalPool.java:50)
    at org.jboss.ejb3.ThreadlocalPool.get(ThreadlocalPool.java:90)
    at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:54)
    at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
    at org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:77)
    at org.jboss.ejb3.security.Ejb3AuthenticationInterceptor.invoke(Ejb3AuthenticationInterceptor.java:110)
    at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
    at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:46)
    at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
    at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:106)
    at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
    at org.jboss.wsf.container.jboss42.InvocationHandlerEJB3.invoke(InvocationHandlerEJB3.java:103)
    at org.jboss.ws.core.server.ServiceEndpointInvoker.invoke(ServiceEndpointInvoker.java:221)
    at org.jboss.wsf.stack.jbws.RequestHandlerImpl.processRequest(RequestHandlerImpl.java:466)
    at org.jboss.wsf.stack.jbws.RequestHandlerImpl.handleRequest(RequestHandlerImpl.java:284)
    at org.jboss.wsf.stack.jbws.RequestHandlerImpl.doPost(RequestHandlerImpl.java:201)
    at org.jboss.wsf.stack.jbws.RequestHandlerImpl.handleHttpRequest(RequestHandlerImpl.java:134)
    at org.jboss.wsf.stack.jbws.EndpointServlet.service(EndpointServlet.java:84)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:182)
    at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:262)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:446)
    at java.lang.Thread.run(Thread.java:595)
Caused by: java.lang.IllegalArgumentException
    at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:63)
    at java.lang.reflect.Field.set(Field.java:656)
    at org.jboss.injection.JndiFieldInjector.inject(JndiFieldInjector.java:116)
    ... 41 more 

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:3)

只是猜测:

java.lang.RuntimeException: Non matching type for inject of field: 
[...] com.softcomputer.softlab.lquery.service.IHistoryFormatter [...] for type: 
[...] of [...] com.softcomputer.softlab.lquery.service.IHistoryFormatter[...]

对我来说,有两个类加载器加载了两个IHistoryFormatter。即使这些类具有相同的名称,如果它们已被不同的类加载器加载,它们也会被视为不同。

因此,请检查接口类的包装 - 理想情况下,它们应该只在JBoss的一个位置提供。