Springboot 工具类中引用service/Component

描述:工具类中有时需要使用配置文件中的变量,或调用service中的方法,这时需要注入service或component

  • 静态类中是无法直接引入service的,会报空指针异常
  • 可通过spring注解注入,如下:

方式一:

package com.apigateway.util;

import com.apigateway.config.GlobalConfig;
import com.apigateway.config.ThreadPoolConfig;
import com.apigateway.dto.LogCollectorDto;
import com.apigateway.dto.ParamDto;
import com.apigateway.enums.LogLevelEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import static com.apigateway.constant.CacheConstant.MQSWITCH_OPEN;

/**
 * @Author:xingpf
 * @Date: 2020/5/6  16:06
 * @Descriptions: 日志工具类,将日志打到MQ或日志采集平台
 */
@Component
public class LogUtil {

    private static final Logger log = LoggerFactory.getLogger(LogUtil.class);

    @Resource
    private GlobalConfig config;

    @Resource
    private ThreadPoolConfig threadConfig;


    private static GlobalConfig globalConfig;

    private static ThreadPoolConfig threadPoolConfig;


    @PostConstruct
    public void init() {
        globalConfig = this.config;
        threadPoolConfig = this.threadConfig;
    }

    /**
     * 将报文推至MQ
     *
     * @param paramDto
     */
    public static void logToMq(ParamDto paramDto) {
        //信息推送至MQ的开关开启时,才将信息推送至MQ,开关在nacos配置
        if (MQSWITCH_OPEN.equals(globalConfig.getLogToMqSwitch())) {
            threadPoolConfig.execute(() -> {
                try {
                    UdpUtil.sendMsgToMQ(paramDto.toJsonString(), globalConfig.getMqHostAddress(),
                            globalConfig.getMqHostPort(), globalConfig.getMqHostTimeOut());
                } catch (Exception e) {
                    log.error("[TranspondServiceImpl] [logToMq] Exception=", e);
                    wrapLogCollectorDto(paramDto.getSeqNum(),
                            "信息推送至MQ异常,消息内容={" + paramDto.toJsonString() + "}, Exception=" + e,
                            LogLevelEnum.EVENT.getLevel());
                }
            });
        }
    }

方式二:

package com.apigateway.util;

import com.apigateway.config.GlobalConfig;
import com.apigateway.config.ThreadPoolConfig;
import com.apigateway.dto.LogCollectorDto;
import com.apigateway.dto.ParamDto;
import com.apigateway.enums.LogLevelEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import static com.apigateway.constant.CacheConstant.MQSWITCH_OPEN;

/**
 * @Author:xingpf
 * @Date: 2020/5/6  16:06
 * @Descriptions: 日志工具类,将日志打到MQ或日志采集平台
 */
@Component
public class LogUtil {

    private static final Logger log = LoggerFactory.getLogger(LogUtil.class);

    private static GlobalConfig globalConfig;

    private static ThreadPoolConfig threadPoolConfig;
 
    @Autowired
    public void setGlobalConfig(GlobalConfig globalConfig) {
        LogUtil.globalConfig = globalConfig;
    }

    @Autowired
    public void setThreadPoolConfig(ThreadPoolConfig threadPoolConfig) {
        LogUtil.threadPoolConfig = threadPoolConfig;
    }


    /**
     * 将报文推至MQ
     *
     * @param paramDto
     */
    public static void logToMq(ParamDto paramDto) {
        //信息推送至MQ的开关开启时,才将信息推送至MQ,开关在nacos配置
        if (MQSWITCH_OPEN.equals(globalConfig.getLogToMqSwitch())) {
            threadPoolConfig.execute(() -> {
                try {
                    UdpUtil.sendMsgToMQ(paramDto.toJsonString(), globalConfig.getMqHostAddress(),
                            globalConfig.getMqHostPort(), globalConfig.getMqHostTimeOut());
                } catch (Exception e) {
                    log.error("[TranspondServiceImpl] [logToMq] Exception=", e);
                    wrapLogCollectorDto(paramDto.getSeqNum(),
                            "信息推送至MQ异常,消息内容={" + paramDto.toJsonString() + "}, Exception=" + e,
                            LogLevelEnum.EVENT.getLevel());
                }
            });
        }
    }  
}

注意:如果不是全包扫描的话,在启动类上的@ComponentScan注解中记得扫描工具类所在的包

ps:文章用于记录问题,如有不当之处,欢迎批评指正!

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值