SpringBoot學習記錄:@Cacheable不起作用 -->原因:Shrio + @Cache整合
問題描述:
1、在使用shiro的項目中,整合Cache,@Cacheable失效
2、去掉ShiroConfig后,@Cacheable能夠最長使用,其他注解也OK
問題原因:
@Cacheable ,當使用ehcache時,autoconfig機制會根據配置文件自動去初始化bean
而shiroConfig在@Configuration構造時,也會去初始化ehcache ,項目啟動會產生如下異常
解決方法:
realm原始代碼(錯誤):
public class UserRealm extends AuthorizingRealm { @Autowired private AdminService adminService; @Autowired private CadreService cadreService; @Autowired private ParticipantService participantService; @Autowired private VoteService voteService; protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {……} //認證 @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {……} }
在realm的自動注入下加@Lazy注解,問題解決
public class UserRealm extends AuthorizingRealm { @Autowired
@Lazy private AdminService adminService; @Autowired
@Lazy private CadreService caadreService; @Autowired
@Lazy private ParticipantService participantService; @Autowired
@Lazy private VoteService voteService; protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {……} //認證 @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {……} }