美文网首页
在工具类静态方法调用@Autowired注入的bean方法

在工具类静态方法调用@Autowired注入的bean方法

作者: 左边跟我画个龙 | 来源:发表于2020-05-09 10:21 被阅读0次

解决方案如下:

1.对工具类使用@Component 注解

2.@Autowired 注解注入bean

3.@PostConstruct 使用该注解定义init()方法,在方法中给logTool赋值

使用时调用logTool.xxxMapper.method();

@Component

public class XxxTool {

    @Autowired

    private XxxMapper xxxMapper;

    public static XxxTool xxxTool ;

    @PostConstruct

    public void init() {

        xxxTool = this;

    }

    public static void ok() {

        logTool.xxxMapper.insert(test);

    }

}

相关文章

网友评论

      本文标题:在工具类静态方法调用@Autowired注入的bean方法

      本文链接:https://www.haomeiwen.com/subject/qzmhnhtx.html