Springboot依賴注入 Service類中使用靜態變量


 

@Component 
public class ServerHandler extends IoHandlerAdapter { 
    @Autowired 
    protected HealthDataService healthDataService; 
    private static ServerHandler serverHandler;

    @PostConstruct
    //通過@PostConstruct實現初始化bean之前進行的操作 
    public void init() {
        serverHandler = this; 
        serverHandler.healthDataService = this.healthDataService; 
        // 初使化時將已靜態化的testService實例化 
    } 
    //測試調用 
    public void test(){ 
        serverHandler.healthDataService.<你的service層方法>;
    }
}

 

引自 https://blog.csdn.net/georgeshaw1/article/details/74943089


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM