美文网首页
静态方法中调用Spring注入的方法

静态方法中调用Spring注入的方法

作者: 蓝胖子的白日梦丶 | 来源:发表于2020-06-12 17:15 被阅读0次
import org.springframework.beans.factory.annotation.Autowired;
 
import javax.annotation.PostConstruct;
 
public class InitMethod {
 
    /**
     * 需要调用的服务
     */
    @Autowired
    private SpringService springService;
 
    private static InitMethod initMethod;
 
    // PostConstruct 注释用于在依赖关系注入完成之后需要执行的方法上,以执行任何初始化
    @PostConstruct
    public void init(){
        initMethod = this;
        initMethod.springService = this.springService;
    }
 
    private static void test() {
        initMethod.springService.method();
    }
 
}

相关文章

网友评论

      本文标题:静态方法中调用Spring注入的方法

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