spring InitializingBean与DisposableBean的使用

本文介绍了如何在Spring中使用InitializingBean和DisposableBean接口进行自定义Bean的初始化和销毁操作。示例展示了如何在afterPropertiesSet方法中初始化本地缓存,并在destroy方法中清理缓存。
摘要由CSDN通过智能技术生成

参考链接:https://www.jianshu.com/p/c2e33c451ee0
在项目中,我们会使用到自定义bean,做一些特定义的功能方法,示例如下所示:
@Service
public class ProductService implements InitializingBean, DisposableBean {
  @Autowired
  private ProductSvcRemoteService productRemoteService;
   // 创建本地缓存1:缓存产品信息
   private Cache<String, ProductInfoGson> localeProductInfoCache;
   // 创建本地缓存2:缓存产品id
   private Cache<String, Long> productCodeToIdCache;
   private static final String SEPARATOR = “|”;
   // 程序停止,销毁

   @Override
   public void destroy() throws Exception {
      localeProductInfoCache.cleanUp();
      productCodeToIdCache.cleanUp();
    }

  @Override
   public void afterPropertiesSet() throws Exception {
     // 初始化缓存实例:缓存5秒没有更新,失效
     localeProductInfoCache = CacheBuilder.newBuilder().expireAfterWrite(5, TimeUnit.SECONDS).maximumSize(100).build();
      // 初始化缓存实例:缓存10秒没有更新,失效
     productCodeToIdCache = CacheBuilder.newBuilder().expireAfterWrite(10, TimeUnit.MINUTES).maximumSize(100).build();
    }

   private String getCacheKey(Long productId, String locale) {
    return “productId:” + productId + SEPARATOR + locale;
   }

   private ProductInfoGson loadCachedProductInfo(final Long productId, final String locale) throws Exception {
     String cacheKey = getCacheKey(productId, StringUtils.isEmpty(locale) ? Constants.DEFAULT_LOCALE : locale);
     return localeProductInfoCache.get(cacheKey, new Callable() {
      @Override
      public ProductInfoGson call() throws Exception {
        ProductInfoGson productInfo = productRemoteService.getProductDetailById(productId);
        if (productInfo != null) {
           mergeLocaleProductEntity(productId, locale, productInfo);
          }
         return productInfo;
        }
       });
     }
  private List loadCachedProductInfoByCodeList(List productCodeList, String locale) throws Exception {
     List productInfos = new ArrayList();
     String cacheLocale = StringUtils.isEmpty(locale) ? Constants.DEFAULT_LOCALE : locale;
     // 需要做缓存的产品编码信息
     List cacheProductCodeList = new ArrayList();
     // 从本地缓存中获多语言产品信息
    for (String productCode : productCodeList) {
      Long productId = productCodeToIdCache.getIfPresent(productCode);
      if (productId != null) {
        ProductInfoGson productInfo = localeProductInfoCache.getIfPresent(getCacheKey(productId, cacheLocale));
        if (productInfo != null) {
          productInfos.add(productInfo);
          continue;
          }
         }
      cacheProductCodeList.add(productCode);
       }
  if (CollectionUtils.isEmpty(cacheProductCodeList)) {
    return productInfos;
     }
   // 缓存中不存在的, 查询远程服务
   List qProductInfos = productRemoteService.getProductDetailListByCodes(cacheProductCodeList);
   if (CollectionUtils.isNotEmpty(qProductInfos)) {
   Map<String, ProductInfoGson> cacheProductInfoMap = new HashMap<String, ProductInfoGson>();
   Map<String, Long> cacheProductIdMap = new HashMap<String, Long>();
   for (ProductInfoGson qProductInfo : qProductInfos) {
      productInfos.add(qProductInfo);
      cacheProductInfoMap.put(getCacheKey(qProductInfo.getId(), cacheLocale), qProductInfo);
      cacheProductIdMap.put(qProductInfo.getCode(), qProductInfo.getId());
     }
   // 产品信息–>批量存入缓存
   localeProductInfoCache.putAll(cacheProductInfoMap);
   // 产品id—>批量存入缓存
   productCodeToIdCache.putAll(cacheProductIdMap);
      }
   return productInfos;
   }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值