Autowire一个 FactoryBean时,代码里要把变量的类型定义为FactoryBean.getObjectType()



  <bean id="myCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean"> 为例,

     //正确
     @Autowired
     private Ehcache myCache;  //因为 EhCacheFactoryBean.getObjectType() == Ehcache 

     //错误:
     @Autowired
     private Cache myCache;  //会报错,虽然Cache是Ehcache的子类。具体错误为“No unique bean of type [net.sf.ehcache.Cache] is defined”

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.