Java -- springboot 配置 工具类注入

1、文件内容

package com.vim.common.utils;

import com.vim.common.properties.WeixinConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

//1.加入 bean 管理
@Component
public class WeixinUtils {

    //2.当前静态类
    private static WeixinUtils weixinUtils;

    //3.需要注入的bean
    @Autowired
    private WeixinConfig config;

    //4.注入的方式
    @PostConstruct
    public void init() {
        weixinUtils = this;
    }
    
    //5.使用方式
    public static void test(){
        String token = weixinUtils.config.getToken();
    }
}

猜你喜欢

转载自blog.csdn.net/sky_eyeland/article/details/94715165