spring boot @bean注解实现缓存化数据

8 篇文章 0 订阅

利用spring注解进行配置的方式进行注入 

package com.fht.base.enterprise.domain.model.EntCompanyCyIm;

import cn.hutool.core.util.ObjectUtil;
import com.fht.base.enterprise.application.vo.EntCompanyCyImVO;
import com.fht.base.enterprise.domain.model.entcomany.EntCompany;
import com.fht.base.enterprise.domain.model.entcomany.EntCompanyRepository;
import com.fht.base.enterprise.domain.model.entcompanyuser.EntCompanyUserRepository;
import com.fht.base.enterprise.dto.CompanyUserDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author baoli.zhang
 * @desc 通行记录配置类
 * @since 2021-08-26
 */
@Configuration
public class CyImConfig {

    @Autowired
    private ApplicationContext applicationContext;

    @Autowired
    private EntCompanyUserRepository entCompanyUserRepository;
    @Autowired
    private EntCompanyRepository entCompanyRepository;

    @Bean(name = "companyUserImCy")
    public Map<Integer,List<EntCompanyCyImVO>> companyUserImCyMap(){
        Map<Integer,List<EntCompanyCyImVO>> companyUserMap = new HashMap<>();
        try {
            //从数据源获取数据
            List<EntCompany> integers=entCompanyRepository.selectAll();
            if(ObjectUtil.isNotEmpty(integers)){
                List<EntCompanyCyImVO>  entCompanyImArrayList = entCompanyUserRepository.companyUserAll();
                integers.stream().forEach(ite->{
                    List<EntCompanyCyImVO> list=new ArrayList<>();
                    entCompanyImArrayList.stream().forEach(ent->{
                        if(ite.getCompanyId().equals(ent.getCompanyId())){
                            list.add(ent);
                        }
                    });
                    companyUserMap.put(ite.getCompanyId(),list);
                });
            }
        }catch (Exception ex){
            return companyUserMap;
        }
        return companyUserMap;
    }

    public void reRegister(String beanName, Object singletonObject) {
        DefaultListableBeanFactory defaultListableBeanFactory =
                (DefaultListableBeanFactory)applicationContext.getAutowireCapableBeanFactory();
        defaultListableBeanFactory.destroySingleton(beanName);
        defaultListableBeanFactory.registerSingleton(beanName,singletonObject);
    }
}

调用方式 

@Autowired
private CyImConfig config;      

Map<Integer,List<EntCompanyCyImVO>> listMap=config.companyUserImCyMap();

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值