SpringBoot學習記錄:@Cacheable不起作用 -->原因:Shiro + @Cache整合


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 {……} }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM