我正在使用java配置来配置我的Beans:
@Configuration
public class SpringJavaConfig {
@Bean
public IBrandsApi getBrandsApi(){
return new BrandsApi();
}
}
我可以在调试模式下看到将Spring知道加载到SpringJavaConfig。
在我的控制器中,我正在使用autowire:
@Controller
@RequestMapping("/api/brands")
public class BrandsController extends BaseController {
BrandsApi brandsApi;
@Autowired
public void setBrandsApi(IBrandsApi brandsApi){
this.brandsApi = (BrandsApi)brandsApi;
}
}
这是品牌API的贬值:
@Transactional
public class BrandsApi extends BaseApplicationAPI<Object> implements IBrandsApi {
public BrandsApi(){
}
}
@Transactional
public interface IBrandsApi {}
这是我在加载tomcat时遇到的异常:
Mar 9, 2011 9:50:54 AM org.apache.catalina.core.StandardContext listenerStart
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 'brandsController': Injection of autowired dependencies failed; nested exception is java.lang.ClassCastException: $Proxy46 cannot be cast to com.affiliates.BrandsApi
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1074)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:276)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:197)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4521)
at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5004)
at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:4999)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:636)
Caused by: java.lang.ClassCastException: $Proxy46 cannot be cast to com.affiliates.BrandsApi
at com.affiliates.controllers.BrandsController.setBrandsApi(BrandsController.java:37)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:582)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:282)
... 21 more
答案 0 :(得分:2)
请参阅此问题:Application context bean。
为什么要将您的服务转发给BrandsApi
?你不应该只使用界面吗?
答案 1 :(得分:1)
您的BrandsApi是否使用@Transactional
或任何其他类型的AOP?也许问题是你正在使用一个具体的类而不是一个接口作为引用,当Spring创建某种AOP代理类时可能会导致问题。