spring启动容器加载成功后执行调用方法

Posted lisongyu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring启动容器加载成功后执行调用方法相关的知识,希望对你有一定的参考价值。

  • 需求: 由于在微服务架构中各服务之间都是通过接口调用来进行交互的,像很多的基础服务,类似字典信息其实并不需每次需要的时候再去请求接口.所以我的想法是每次启动项目的时候,容器初始化完成,就去调用一下基础服务的接口.通过一个本地map来缓存需要的数据.当我需要使用的时候直接从本地缓存中取.下面是具体的代码实现.
  • 在启动类中实现ApplicationListener 接口,重写onApplicationEvent(ApplicationReadyEvent applicationReadyEvent)方法.
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.ComponentScan;

import java.util.Map;

import static com.ykc.flowside.constants.Constants.DISCOUNT_TYPE;
import static com.ykc.flowside.constants.Constants.OPERATOR_INFO;
import static com.ykc.flowside.utils.FeignStaticUtils.getDictInfo;
import static com.ykc.flowside.utils.FeignStaticUtils.getOperatorInfo;



/**
 * @author lisongyu
 * @ClassName com.ykc.flow.FlowSideApplication
 * @description
 * @create 2019年03月25日 16:31
 */
@SpringBootApplication(exclude = {KafkaAutoConfiguration.class})
@ComponentScan(basePackages = "com.ykc")
@Slf4j
@EnableFeignClients
public class FlowSideApplication implements ApplicationListener<ApplicationReadyEvent> {

    public static void main(String[] args) {
        SpringApplication.run(FlowSideApplication.class, args);

    }

    @Override
    public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) {
        try {
            Map<String, DictEntity> dictInfo = getDictInfo();
            log.info("[项目启动时调用g1] 字典数据:{}条", dictInfo.size());
            Map<Integer, OperatorInfo> operatorInfo = getOperatorInfo(null);
            log.info("[项目启动时调用g1] 电站数据:{}条", operatorInfo.size());
            dictInfo.keySet().forEach(k -> DISCOUNT_TYPE.put(k, dictInfo.get(k)));
            operatorInfo.keySet().forEach(k -> OPERATOR_INFO.put(k, operatorInfo.get(k)));
        } catch (Exception e) {
            log.error("[项目启动时调用g1] 发生错误,", e);
        }
    }


}
  • 调用工具类
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.ykc.config.common.entity.Header;
import com.ykc.config.common.entity.Message;
import com.ykc.config.common.entity.RequestData;
import com.ykc.flowside.entity.base.DictEntity;
import com.ykc.flowside.exception.FlowSideException;
import com.ykc.flowside.feign.IBaseServerFeign;
import com.ykc.flowside.form.flow.OrgForm;
import com.ykc.flowside.vo.GunInfo;
import com.ykc.flowside.vo.OperatorInfo;
import com.ykc.flowside.vo.OrgVO;
import com.ykc.service.RedisService;
import com.ykc.util.CodeHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.ykc.flowside.constants.Constants.FLOW_SIDE;


/**
 * @author lisongyu
 * @ClassName cn.lisongyu.utils.FeignStaticUtils
 * @description
 * @create 2019年04月08日 16:30
 */
@Component
@Slf4j
public class FeignStaticUtils {

  //存放运营商信息
    public static ConcurrentHashMap<Integer, OperatorInfo> OPERATOR_INFO = new ConcurrentHashMap<>();
    //存放字典信息
    public static ConcurrentHashMap<String, DictEntity> DISCOUNT_TYPE = new ConcurrentHashMap<>();
    
    private static IBaseServerFeign iBaseServerFeign;


    private static RedisService redisService;

    @Autowired  
    public FeignStaticUtils(IBaseServerFeign iBaseServerFeign, RedisService redisService) {
        FeignStaticUtils.iBaseServerFeign = iBaseServerFeign;
        FeignStaticUtils.redisService = redisService;
    }


    /**
     * 获取服务费折扣类型
     *
     * @return
     */
    public static Map<String, DictEntity> getDictInfo() {
        //调用G1接口
        RequestData<Object> requestData = new RequestData<>();
        requestData.setHeader(new Header());
        Map<String, List<String>> param = new HashMap<>();
        param.put("types", Collections.singletonList("service_discount_type"));
        requestData.setBody(param);
        Message dictionary = iBaseServerFeign.getDictionary(requestData);
        List<DictEntity> discountTypes = JSONArray.parseArray(JSON.toJSONString(
                (JSON.parseObject(
                        JSON.toJSONString(dictionary.getBody()), Map.class).get("service_discount_type"))),
                DictEntity.class);
        //将字典项转换成map
        return discountTypes.stream().collect(Collectors.toMap(DictEntity::getId, a -> a, (k1, k2) -> k1));
    }


    /**
     * 获取运营商信息
     *
     * @return
     */
    public static Map<Integer, OperatorInfo> getOperatorInfo(Integer operatorId) {
        //调用G1接口 获取运营商信息
        RequestData<Object> requestData = new RequestData<>();
        requestData.setHeader(new Header());
        Map<String, List<String>> param = new HashMap<>();
        if (CodeHelper.isNull(operatorId))
            param.put("idList", Collections.emptyList());
        else
            param.put("idList", Collections.singletonList(String.valueOf(operatorId)));
        requestData.setBody(param);
        Message message = iBaseServerFeign.selectOperatorNameById(requestData);
        List<OperatorInfo> operatorInfos = JSONArray.parseArray(JSON.toJSONString(message.getBody()), OperatorInfo.class);
        //将运营商信息转换成map
        return operatorInfos.stream().collect(Collectors.toMap(OperatorInfo::getStationOperatorId, a -> a, (k1, k2) -> k1));
    }




    /**
     * 模糊查询运营商信息
     *
     * @return
     */
    public static List<Integer> likeByOperatorName(String stationOperatorName) {
        //调用G1接口 模糊查询运营商信息
        try {
            RequestData<Object> requestData = new RequestData<>();
            requestData.setHeader(new Header());
            Map<String, String> param = new HashMap<>();
            param.put("stationOperatorName", stationOperatorName);
            requestData.setBody(param);
            Message message = iBaseServerFeign.likeByOperatorName(requestData);
            List<OperatorInfo> operatorInfos = JSONArray.parseArray(JSON.toJSONString(message.getBody()), OperatorInfo.class);
            return operatorInfos.stream().map(OperatorInfo::getStationOperatorId).collect(Collectors.toList());
        } catch (Exception e) {
            log.error("[调用G1接口 模糊查询运营商信息] 发生错误", e);
            return Collections.singletonList(0);
        }
    }


}
  • 由于在静态类中使用@Autowired注入会默认成null,所以要使用当前类去重新获取一下.

以上是关于spring启动容器加载成功后执行调用方法的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot 项目启动后执行代码

spring零碎知识点

spring boot容器加载完后执行特定操作

Spring实战Spring容器初始化完成后执行初始化数据方法

Spring实战Spring容器初始化完成后执行初始化数据方法

怎样查看spring容器都有哪些bean