[英]@Autowired failes - java.lang.ClassCastException
I am using java config to configure my Beans: 我正在使用java config配置我的Bean:
@Configuration
public class SpringJavaConfig {
@Bean
public IBrandsApi getBrandsApi(){
return new BrandsApi();
}
}
I can see in debug mode while loading that spring aware to the SpringJavaConfig. 在将那个Spring感知加载到SpringJavaConfig时,我可以看到处于调试模式。
In my controller I am using the autowire: 在我的控制器中,我正在使用自动接线:
@Controller
@RequestMapping("/api/brands")
public class BrandsController extends BaseController {
BrandsApi brandsApi;
@Autowired
public void setBrandsApi(IBrandsApi brandsApi){
this.brandsApi = (BrandsApi)brandsApi;
}
}
this is the brands API decleration: 这是品牌的API定义:
@Transactional
public class BrandsApi extends BaseApplicationAPI<Object> implements IBrandsApi {
public BrandsApi(){
}
}
@Transactional
public interface IBrandsApi {}
this is the exception that i get while loading the tomcat: 这是我在加载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
See this question: Application context bean . 看到这个问题: Application context bean 。
Why are you downcasting your service to BrandsApi
? 为什么将您的服务下
BrandsApi
? Shouldn't you be working with interface only? 您不应该只使用界面吗?
Does your BrandsApi use @Transactional
or any other kind of AOP? 您的BrandsApi是否使用
@Transactional
或任何其他种类的AOP? Maybe the problem is you're using a concrete class and not a interface as reference and that could lead a problem when Spring creates some kind of AOP Proxy class. 也许问题是您使用的是具体类而不是接口作为引用,当Spring创建某种AOP Proxy类时,可能会导致问题。
Check this related question: Spring Autowiring class vs. interface? 检查以下相关问题: Spring Autowiring类与接口?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.