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

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)