欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

關(guān)于shiro中部分SpringCache失效問題的解決方法

 更新時間:2018年07月31日 08:38:54   作者:夏日的雪花  
這篇文章主要給大家介紹了關(guān)于shiro中部分SpringCache失效問題的解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

1、問題拋出

今天在做Springboot和shiro集成時,發(fā)現(xiàn)一個嚴(yán)重的問題。部分service的緩存和事務(wù)失效,debug代碼時,發(fā)現(xiàn)這些有問題的service實(shí)例都不是代理生成的,所以事務(wù)和緩存就失效了(事務(wù)和緩存依賴代理類實(shí)現(xiàn))。繼續(xù)查問題,發(fā)現(xiàn)這些有問題的service全部被shiro的realm所依賴,所以懷疑是shiro影響了

所以做一下測試:

shiro中用到的ResourceService

public class LocalRealmService extends RealmService {
 @Autowired
 private ResourceService resourceService;
 ...
}

controller也調(diào)用ResourceService

@RestController
@RequestMapping(value = "/test")
public class CacheController {
 private Logger logger = LoggerFactory.getLogger(this.getClass());

 @Autowired
 private ResourceService resourceService;
}

結(jié)果發(fā)現(xiàn)resourceService的實(shí)例如圖:


發(fā)現(xiàn)問題:resourceService的實(shí)例不是代理即緩存注解和事務(wù)全部生效(緩存和事務(wù)都是代理完成的)

當(dāng)我把resourceService從realm依賴中刪除時,在controller引用時resourceService的實(shí)例就是“代理”即緩存和事務(wù)生效

結(jié)論:只要被shiro的realm所依賴的service,代理會全部失效(暫時沒擼源碼,還不知原理,知道的童鞋可以說下,謝了)

2、解決的方案

常用的解決方式有三種:

第一種:這是網(wǎng)上比較多的

就是realm中不要依賴service,依賴dao

第二種:在依賴的service上添加@Lazy注解

延遲加載,就是在實(shí)例化shiro的realm時,不去實(shí)例化service的bean,等到用的時候再從spring容器中去取對應(yīng)的Bean

public class LocalRealmService extends RealmService {
 
 @Lazy
 @Autowired
 private ResourceService resourceService;
 ...
}

這種解決方案讓我感覺到:這里是不是存在多個上下文,或者不是spring?這里有待后續(xù)考證。。。

第三種:shiro在實(shí)例化securityManager時,先不設(shè)置realm,等到容器加載完再設(shè)置

這種方式與第二種類似,只不過無需在每個service屬性上增加@Lazy注解

SecurityMangaerd的實(shí)例化

/**
* 注釋掉realm
*/
@Bean("securityManager")
public DefaultWebSecurityManager securityManager(/*@Qualifier("realm") BootRealm BootRealm,*/
             SessionManager sessionManager,
             CacheManager shiroCacheManager) {
 DefaultWebSecurityManager manager = new DefaultWebSecurityManager();
 //注釋掉
 //manager.setRealm(hyBootRealm);
}

容器加載完設(shè)置realm:這里有多重方案,主要列舉兩種

I、利用spring監(jiān)聽

import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;

@Component
public class ShiroRealmListener implements ApplicationListener {
 @Autowired
 private DefaultWebSecurityManager securityManager;
 @Autowired
 private HyBootRealm realm;
 
 @Override
 public void onApplicationEvent(ApplicationEvent event) {
  securityManager.setRealm(realm);
 }
}

II、利用springboot的ApplicationRunner

import com.chyjr.hyboot.security.shiro.realm.HyBootRealm;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;

public class ShiroRealmRunner implements ApplicationRunner {

 @Autowired
 private DefaultWebSecurityManager securityManager;
 @Autowired
 private HyBootRealm realm;
 
 @Override
 public void run(ApplicationArguments args) throws Exception {
  securityManager.setRealm(realm);
 }
}

注意:ShiroRealmRunner必須是spring的Bean,所以在配置管理類中要添加:

@Bean
public ShiroRealmRunner shiroRealmRunner(){
 return new ShiroRealmRunner();
}

總結(jié):

上述就是常用的解決方案,至于原理后續(xù)會抽時間研究....

好了,以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關(guān)文章

  • SpringBoot中的靜態(tài)資源訪問的實(shí)現(xiàn)

    SpringBoot中的靜態(tài)資源訪問的實(shí)現(xiàn)

    這篇文章主要介紹了SpringBoot中的靜態(tài)資源訪問的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • Spring入門實(shí)戰(zhàn)之Profile詳解

    Spring入門實(shí)戰(zhàn)之Profile詳解

    什么是spring profile?簡單講profile就是一組配置,不同profile提供不同組合的配置,程序運(yùn)行時可以選擇使用哪些profile來適應(yīng)環(huán)境。下面這篇文章主要介紹了Spring中Profile實(shí)戰(zhàn)的相關(guān)資料,需要的朋友可以參考借鑒。
    2017-02-02
  • SpringBoot整合minio服務(wù)的示例代碼

    SpringBoot整合minio服務(wù)的示例代碼

    本文主要介紹了SpringBoot整合minio服務(wù)的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • SpringBoot中@RestControllerAdvice @ExceptionHandler異常統(tǒng)一處理類失效原因分析

    SpringBoot中@RestControllerAdvice @ExceptionHandler異常統(tǒng)一處

    這篇文章主要介紹了SpringBoot中@RestControllerAdvice @ExceptionHandler異常統(tǒng)一處理類失效原因,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • Mybatis各種查詢接口使用詳解

    Mybatis各種查詢接口使用詳解

    這篇文章主要介紹了Mybatis各種查詢接口使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2022-11-11
  • Java算法之遞歸算法計(jì)算階乘

    Java算法之遞歸算法計(jì)算階乘

    這篇文章主要為大家詳細(xì)介紹了Java遞歸算法計(jì)算階乘,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2015-08-08
  • IE8+SpringMVC文件上傳防止JSON下載

    IE8+SpringMVC文件上傳防止JSON下載

    這篇文章主要介紹了IE8+SpringMVC文件上傳防止JSON下載的相關(guān)資料,需要的朋友可以參考下
    2017-07-07
  • 基于Java實(shí)現(xiàn)雙向鏈表

    基于Java實(shí)現(xiàn)雙向鏈表

    這篇文章主要為大家詳細(xì)介紹了基于Java實(shí)現(xiàn)雙向鏈表,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • Java如何將字符串轉(zhuǎn)為數(shù)字int的三種方式詳析

    Java如何將字符串轉(zhuǎn)為數(shù)字int的三種方式詳析

    這篇文章主要給大家介紹了關(guān)于Java如何將字符串轉(zhuǎn)為數(shù)字int的三種方式,在編程中我們經(jīng)常需要進(jìn)行各種數(shù)據(jù)類型之間的轉(zhuǎn)換操作,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-10-10
  • 利用Java+MySQL實(shí)現(xiàn)附近功能實(shí)例

    利用Java+MySQL實(shí)現(xiàn)附近功能實(shí)例

    現(xiàn)在很多手機(jī)軟件都用附近搜索功能,但具體是怎么實(shí)現(xiàn)的呢?下面這篇文章就來給大家介紹關(guān)于利用Java+MySQL實(shí)現(xiàn)附近功能的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-12-12

最新評論