2

我有庞大的复杂 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);
    }
}

}

版本:
Spring 3.2.0
apache-tomcat-6.0.36

有任何想法吗?谢谢。

4

3 回答 3

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 配置中强制执行。请注意,您的类路径上需要 CGLIB(自 Spring 3.2.0 以来并非如此)。

这是CGLIB 代理和 JDK 动态代理的有趣比较

于 2013-05-20T14:26:40.650 回答
0

在我的课堂上很好地实现接口解决了这个问题。

public interface UserInterface  {
    public void saveDetails();
}

在你要使用的类中实现上面的接口,

public class UserInterfaceImpl implements UserInterface{
  //your implementation stuff goes here
} 
于 2013-10-15T13:47:02.887 回答
0

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

@EnableAspectJAutoProxy(proxyTargetClass = true) 

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

<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.7.2</version>
</dependency> 
于 2013-09-09T14:51:29.413 回答