springboot集成cache与shiro时使用@Cacheable不起作用问题
问题描述:
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的自动注入下加@Lezy注解,问题解决
public class UserRealm extends AuthorizingRealm {
@Autowired
@Lezy
private AdminService adminService;
@Autowired
@Lezy
private CadreService cadreService;
@Autowired
@Lezy
private ParticipantService participantService;
@Autowired
@Lezy
private VoteService voteService;
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {……}
//认证
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken)
throws AuthenticationException {……}
3、@Cacheable 的一些注意点
1、因为@Cacheable 由AOP 实现,所以,如果该方法被其它注解切入,当缓存命中的时候,则其它注解不能正常切入并执行,@Before
也不行,当缓存没有命中的时候,其它注解可以正常工作
2、@Cacheable 方法不能进行内部调用,否则缓存无法创建
3、相关类没有实例化 serialVersionUID