0

我很难弄清楚为什么我的 spring bean 没有被注入到部署到 jboss 中的 CXF Web 服务中。这是场景:

我有多个实现接口的服务类

这是实施#1

@Service("retrieveService")
@Scope("prototype")
@Transactional(propagation=Propagation.REQUIRED)
public class RetrieveService extends BaseService implements IEuclidService 
{
    @Resource
    private EntrLoanRepository entrLoanRepository;


    @Override
    public RetrieveResponse serve(RequestMessage request) 
    {
                    entrLoanRepository.findByEntrLoanId();
    }
}

这是实施#2

@Service("disableService")
@Scope("prototype")
@Transactional(propagation=Propagation.REQUIRED)
public class DisableService extends BaseService implements IEuclidService 
{
    @Resource
    private EntrLoanRepository entrLoanRepository;

    @Override
    public RetrieveResponse serve(RequestMessage request) 
    {
                EntrLoan entrLoan = new EntrLoan();
                entrLoan.setEndDate( new Date());
                entrLoanRepository.save(entrLoan);
    }
}

我有我的 cxf Web 服务,我希望将一个 spring(服务)bean 注入其中。

@javax.jws.WebService
public class Euclid implements IEuclid {

    private IEuclidService euclidService

    public MyResponse myMethod(MyRequest req)
    {
    <!-- How do I inject an instance of RetrieveService here ???-->
    euclidService.serve();
    }
}

我试过这样做:

euclidService = ctx.getBean("retrieveService", RetrieveService.class);

但我收到错误消息:

     com.sgb.euclid.contracts.RetrieveEnterpriseLnIdRequest@31d908
     ***1 EuclidService is NULL
     *****Instantiating Spring Bean
     org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'retrieveService' must be of type [com.sgb.euclid.ws.service.RetrieveService], but was actually of type [$Proxy98]
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:360)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
        at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1102)
        at com.sgb.euclid.ws.web.Euclid.retrieve(Euclid.java:79)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.jboss.as.ee.component.ManagedReferenceMethodInterceptorFactory$ManagedReferenceMethodInterceptor.processInvocation(ManagedReferenceMethodInterceptorFactory.java:72)
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
        at org.jboss.invocation.WeavedInterceptor.processInvocation(WeavedInterceptor.java:53)
        at org.jboss.as.ee.component.interceptors.UserInterceptorFactory$1.processInvocation(UserInterceptorFactory.java:58)
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
        at org.jboss.invocation.WeavedInterceptor.processInvocation(WeavedInterceptor.java:53)
        at org.jboss.as.ee.component.interceptors.UserInterceptorFactory$1.processInvocation(UserInterceptorFactory.java:58)
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
        at org.jboss.invocation.InitialInterceptor.processInvocation(InitialInterceptor.java:21)
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
        at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61)
        at org.jboss.as.ee.component.interceptors.ComponentDispatcherInterceptor.processInvocation(ComponentDispatcherInterceptor.java:53)
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
        at org.jboss.as.webservices.deployers.WSComponentInstanceAssociationInterceptor.processInvocation(WSComponentInstanceAssociationInterceptor.java:49)
        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
        at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61)
        at org.jboss.as.ee.component.ViewService$View.invoke(ViewService.java:165)
        at org.jboss.as.webservices.invocation.AbstractInvocationHandler.invoke(AbstractInvocationHandler.java:111)
        at org.jboss.wsf.stack.cxf.JBossWSInvoker.performInvocation(JBossWSInvoker.java:149)
        at org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:96)
        at org.apache.cxf.jaxws.AbstractJAXWSMethodInvoker.invoke(AbstractJAXWSMethodInvoker.java:178)
        at org.apache.cxf.jaxws.JAXWSMethodInvoker.invoke(JAXWSMethodInvoker.java:64)
        at org.jboss.wsf.stack.cxf.JBossWSInvoker.invoke(JBossWSInvoker.java:129)
        at org.apache.cxf.interceptor.ServiceInvokerInterceptor$1.run(ServiceInvokerInterceptor.java:58)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
        at java.util.concurrent.FutureTask.run(FutureTask.java:138)
        at org.apache.cxf.workqueue.SynchronousExecutor.execute(SynchronousExecutor.java:37)
        at org.apache.cxf.interceptor.ServiceInvokerInterceptor.handleMessage(ServiceInvokerInterceptor.java:107)
        at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:262)
        at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121)
        at org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:236)
        at org.jboss.wsf.stack.cxf.RequestHandlerImpl.handleHttpRequest(RequestHandlerImpl.java:88)
        at org.jboss.wsf.stack.cxf.transport.ServletHelper.callRequestHandler(ServletHelper.java:156)
        at org.jboss.wsf.stack.cxf.CXFServletExt.invoke(CXFServletExt.java:87)
        at org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:221)
        at org.apache.cxf.transport.servlet.AbstractHTTPServlet.doPost(AbstractHTTPServlet.java:141)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:754)
        at org.jboss.wsf.stack.cxf.CXFServletExt.service(CXFServletExt.java:135)
        at org.jboss.wsf.spi.deployment.WSFServlet.service(WSFServlet.java:140)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:295)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:149)
        at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50)
        at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50)
        at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:169)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:145)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:97)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:102)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:336)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:652)
        at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:919)
        at java.lang.Thread.run(Thread.java:662)

我不太明白“类型 [$Proxy98]”是什么。我在日志中搜索,但看不到它来自哪里。我认为这与春天有关?不管怎样,我做错了什么,我无法实例化一个spring bean,记住bean不是在beans.xml中定义的,而是注释的。

我觉得我错过了一些东西,但不能完全把我的手指放在它上面。

当我在 jboss 上进行部署时,配置 xml 与我在网上看到的示例有点不同。这是我的 jboss-cxf.xml:

    <jaxws:endpoint id="POJOEndpoint" 
        implementor="com.sgb.euclid.ws.web.Euclid"
        wsdlLocation="WEB-INF/wsdl/EnterpriseLoanIdentifier.wsdl" 
        address="http://localhost:8080/euclid-ws">
        <jaxws:properties>
            <entry key="schema-validation-enabled" value="true" />
        </jaxws:properties>
        <jaxws:invoker>
            <bean class="org.jboss.wsf.stack.cxf.InvokerJSE" />
        </jaxws:invoker>
    </jaxws:endpoint>

这是 applicationContext-ws.xml:

    <import resource="applicationContext-core.xml" />
    <import resource="applicationContext-jpa.xml" />

    <!-- setting up packages to scan for components -->
    <context:component-scan base-package="com.sgb.euclid.ws.service" />

    <tx:annotation-driven 
    transaction-manager="transactionManager" />

这里是对应的web.xml

<!--  setting up spring configuration to instantiate the spring container  -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:META-INF/spring/applicationContext-ws.xml</param-value>
</context-param>

<servlet>
    <servlet-name>euclid</servlet-name>
    <servlet-class>com.sgb.euclid.ws.web.Euclid</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>euclid</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

我无法弄清楚我的错误是什么。任何帮助表示赞赏....

4

1 回答 1

0

解决方案似乎如下:

euclidService = ctx.getBean("retrieveService", IEuclidService .class);

或者

euclidService = (IEuclidService) ctx.getBean("retrieveService");

看来 getBean 只能转换为接口而不能转换为实现类。... 基于http://forum.springsource.org/showthread.php?71543-Proxy-Type-Conversion-Error

另一种解释是: Abstract DAO 模式和 Spring 的“Proxy cannot be cast to ...”问题!

于 2012-12-21T00:53:45.753 回答