我有一个庞大而复杂的Spring项目。当我添加新课程时:

@Service("zoneManagerDelegate")
@Cached(keyGeneratorClass=ServiceGlobalKeyGenerator.class,dataWrapperClass=ServiceDelegateDataWrapper.class, tags = {"service", "sapi"})
public class ZoneManagerDelegate extends SapiDelegate<ZoneManagerWS> {

    public ZoneManagerDelegate(){
        super(ZoneManagerWS.class);
    }

    @Cached
    public Object[] getZones(SalesUserContext userContext) throws SalesAPIException {
        return getWs().getZones(userContext);
    }
}


并运行项目,我遇到一个错误输出:

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'conversionService' defined in ServletContext resource [/WEB-INF/context/common.xml]: Cannot resolve reference to bean 'rtChargeEntryDetailsToBookedInsertionInfo' while setting bean property 'converters' with key [14]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'rtChargeEntryDetailsToBookedInsertionInfo': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.ms.adsp.objectFinder.ObjectFinderService com.ms.adsp.bean.payment.converter.RtChargeEntryDetailsToBookedInsertionInfo.objectFinderService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'objectFinderService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void com.ms.adsp.objectFinder.ObjectFinderService.setObjectFinderExts(java.util.Collection); nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'objectFinderExtFactory': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'zoneManagerDelegate' must be of type [com.ms.adsp.delegate.sapi.ZoneManagerDelegate], but was actually of type [$Proxy20]
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:329)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:107)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveManagedSet(BeanDefinitionValueResolver.java:366)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:158)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1391)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1132)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:198)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:915)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:383)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4206)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4705)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:799)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:779)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:601)
at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:943)
at org.apache.catalina.startup.HostConfig.deployWARs(HostConfig.java:778)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:504)
at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1317)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:324)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:142)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1065)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:840)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463)
at org.apache.catalina.core.StandardService.start(StandardService.java:525)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:754)
at org.apache.catalina.startup.Catalina.start(Catalina.java:595)
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.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)


用法看起来像这样:


@Component
public class ObjectFinderExtFactory {

    @Resource(name="zoneManagerDelegate")
    private ZoneManagerDelegate zoneManagerDelegate;

...


}

当我在ZoneManagerDelegate中删除@Cached批注时-工作正常。扩展SapiDelegate的所有其他委托-也可以正常工作。

附加信息:

public abstract class SapiDelegate<T> implements ApplicationListener<EnvironmentReloaded> {

private static final Logger logger = Logger.getLogger(SapiDelegate.class);

private T wsInstance;

private final Class<T> wsClass;

public SapiDelegate(Class<T> wsClass){
    this.wsClass = wsClass;
}

@Override
public synchronized void onApplicationEvent(EnvironmentReloaded appReloaded) {
    wsInstance = null;
    logger.debug("Sales API client invalidated: " + wsClass.getName());
}

/**
 * Returns webservice client object
 */
protected synchronized  T getWs() {
    try {
        if (wsInstance == null) {
            wsInstance = ClientFactory.createClient(wsClass, "adsalespro");
            logger.debug("Sales API client created: " + wsClass.getName());
        }
        return (T) wsInstance;
    } catch (Exception e) {
        throw new SapiException(e);
    }
}


}

版:
春季3.2.0
apache-tomcat-6.0.36

有任何想法吗?谢谢。

最佳答案

对我来说,问题似乎在于您没有针对接口编程。

尝试像这样定义接口:

public interface ZoneManagerDelegateInterface  {

    public Object[] getZones(SalesUserContext userContext) throws SalesAPIException
}


并用您的ZoneManagerDelegate实施它。
然后使用它来引用它的接口:

@Component
public class ObjectFinderExtFactory {

    @Resource(name="zoneManagerDelegate")
    private ZoneManagerDelegateInterface zoneManagerDelegate;

...


更新

另一个解决方案是使用CGLIB代理而不是JDK动态代理。这可以在Spring configuration中强制执行。请注意,您需要在类路径上使用CGLIB(自Spring 3.2.0起不适用)。

这是有趣的comparison of CGLIB proxies and JDK Dynamic proxies

关于java - Spring 3.x-名为“zoneManagerDelegate”的Bean必须为[com.ms.adsp.delegate.sapi.ZoneManagerDelegate]类型,但实际上为[$ Proxy20]类型,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16651465/

10-10 12:49