cheng~cheng 2015-04-25 14:33 采纳率: 0%
浏览 1839

Ehcache的Spring配置依赖注入问题

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方面的高手帮忙看看是怎么回事?小弟感激不尽!
  • 写回答

3条回答

  • devmiao 2015-04-25 14:41
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式