利用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();