Spring 3.x - 名为'zoneManagerDelegate'的Bean必须是[com.ms.adsp.delegate.sapi.ZoneManagerDelegate]类型,但实际上是[$ Proxy20]类型

时间:2013-05-20 14:12:51

标签: java spring

我有一个庞大复杂的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

有什么想法吗?感谢。

3 个答案:

答案 0 :(得分:4)

对我而言,似乎问题在于您没有针对 INTERFACES 进行编程。

尝试定义这样的界面:

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

答案 1 :(得分:0)

如果使用注释驱动的应用程序上下文,请使用

注释@Configuration类
@EnableAspectJAutoProxy(proxyTargetClass = true) 

您还必须添加以下依赖项:

<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.7.2</version>
</dependency> 

答案 2 :(得分:0)

在我的课程中实现接口解决了这个问题。

public interface UserInterface  {
    public void saveDetails();
}

在您要使用它的类中实现上述接口,

public class UserInterfaceImpl implements UserInterface{
  //your implementation stuff goes here
}