按照网上的教程做shiro和ehcache的整合,applicationContext-shiro.xml中配置片断如下:
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="userRealm" />
<!-- 注入缓存管理器-->
<property name="cacheManager" ref="cacheManager"/>
<!-- 注入session管理器 -->
<property name="sessionManager" ref="sessionManager" />
</bean>
<!-- 缓存管理器开始 -->
<bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
<property name="cacheManagerConfigFile" value="classpath:pvf-shiro-ehcache.xml"/>
</bean>
<!-- 缓存管理器结束 -->
和网上的教程一模一样,但是启动项目后就报错:
Caused by: java.lang.IllegalStateException: Cannot convert value of type [org.apache.shiro.cache.ehcache.EhCacheManager] to required type [net.sf.ehcache.CacheManager] for property 'cacheManager': no matching editors or conversion strategy found
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:231)
at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:447)
... 55 more
初看这个错误,感觉是没法将[org.apache.shiro.cache.ehcache.EhCacheManager]转换成[net.sf.ehcache.CacheManager] ,也就是说securityManager中的cacheManager属性应该是[net.sf.ehcache.CacheManager]类型的,无法转换报错,找了半天源码,一层套一层找了半天也没确认,度娘上基本上无法找到这种情况。突然灵光一闪,既然你说无法转换,那我不注入了行了吧。把securityManager中的cacheManager属性注入给注释掉,如下:
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="userRealm" />
<!-- 注入缓存管理器
<property name="cacheManager" ref="cacheManager"/> -->
<!-- 注入session管理器 -->
<property name="sessionManager" ref="sessionManager" />
</bean>
<!-- 缓存管理器开始 -->
<bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
<property name="cacheManagerConfigFile" value="classpath:pvf-shiro-ehcache.xml"/>
</bean>
<!-- 缓存管理器结束 -->
结果依旧报同样的错,靠,我都不引你了。。你还转换个毛线啊。。等等。。莫非有别人在引cacheManager?通过id引的还是通过class引的?
索性直接把cacheManager拿到securityManager内部来再看看呢,如下:
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="userRealm" />
<!-- 注入缓存管理器 -->
<property name="cacheManager">
<!-- 缓存管理器开始 -->
<bean class="org.apache.shiro.cache.ehcache.EhCacheManager">
<property name="cacheManagerConfigFile" value="classpath:pvf-shiro-ehcache.xml"/>
</bean>
<!-- 缓存管理器结束 -->
</property>
<!-- 注入session管理器 -->
<property name="sessionManager" ref="sessionManager" />
</bean>
。。还是报错。。但是仔细看看。。报的错已经不一样了。。
Caused by: net.sf.ehcache.CacheException: Another unnamed CacheManager already exists in the same VM. Please provide unique names for each CacheManager in the config or do one of following:
1. Use one of the CacheManager.create() static factory methods to reuse same CacheManager with same name or create one if necessary
2. Shutdown the earlier cacheManager before creating new one with same name.
The source of the existing CacheManager is: InputStreamConfigurationSource [stream=java.io.BufferedInputStream@eb847ae]
at net.sf.ehcache.CacheManager.assertNoCacheManagerExistsWithSameName(CacheManager.java:460)
at net.sf.ehcache.CacheManager.init(CacheManager.java:349)
at net.sf.ehcache.CacheManager.<init>(CacheManager.java:317)
at org.apache.shiro.cache.ehcache.EhCacheManager.ensureCacheManager(EhCacheManager.java:213)
... 55 more
拿着这个错再问问度娘呢。。这下容易了,找到了。继续修改配置文件,如下:
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="userRealm" />
<!-- 注入缓存管理器-->
<property name="cacheManager" ref="cacheManager" />
<!-- 注入session管理器 -->
<property name="sessionManager" ref="sessionManager" />
</bean>
<!-- 缓存管理器开始 -->
<bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
<property name="cacheManager" ref="ehCacheManager"/>
</bean>
<bean id="ehCacheManager" class ="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:pvf-shiro-ehcache.xml" />
<property name="shared" value="true"></property>
</bean>
再次启动,问题解决,经过断点测试,缓存已生效。
虽然还不明白具体原理,但是经过这次排错得到的经验还是遇到错要冷静分析,切记盲目和浮躁。