Spring在依赖注入配置时,注入的对象的类型需要同接收的参数类型相同才能注入成功的。
但是在Ehcache的注入时,我发现类型不匹配,却能注入成功。
小弟愚昧,还请多多指教。具体配置如下:
Spring的bean.xml配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 配置eh缓存管理器 -->
<bean id="cacheManager"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" />
<!-- 配置一个简单的缓存工厂bean对象 -->
<bean id="simpleCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
<property name="cacheManager" ref="cacheManager" />
<property name="cacheName" value="MyEhCache" />
</bean>
<!--- 缓存测试Bean对象 -->
<bean id="ehcacheTest" class="spring.ehcache.EhcacheTest">
<property name="cache" ref="simpleCache"></property>
</bean>
</beans>
疑问说明:
org.springframework.cache.ehcache.EhCacheFactoryBean类的setter如下:
public void setCacheManager(CacheManager cacheManager)
{
this.cacheManager = cacheManager;
}
而bean.xml配置文件中配置为:
<property name="cacheManager" ref="cacheManager" />
这里cacheManager为org.springframework.cache.ehcache.EhCacheManagerFactoryBean类型,并非实际期望的:net.sf.ehcache.CacheManager类型?
请问Spring是怎么完成注入的?
同理:
测试类EhcacheTest中的属性cache为net.sf.ehcache.Cache类型,而非org.springframework.cache.ehcache.EhCacheFactoryBean类型,为什么同样能够注入成功?
还请Spring和Ehcache方面的高手帮忙看看是怎么回事?小弟感激不尽!