java-普通類文件@Autowired自動注入為null


@Autowired注解在非Controller中注入為null

1、配置文件(類文件所在的包)

<context:component-scan base-package="net.nblh.utils.common" />

2、類文件

關於@PostConstruct:被@PostConstruct修飾的方法會在服務器加載Servlet的時候運行,並且只會被服務器調用一次,類似於Serclet的inti()方法。被@PostConstruct修飾的方法會在構造函數之后,init()方法之前運行。

@Component //申明為spring組件
public class GetSpeechRecognitionInfo {
    @Autowired
    private IConfDayService confdayService;
    
    // 靜態初使化當前類
    public static GetSpeechRecognitionInfo getSpeechRecognitionInfo;
    //注解@PostConstruct,這樣方法就會在Bean初始化之后被Spring容器執行
    @PostConstruct
    public void init() {
        getSpeechRecognitionInfo = this;
    }

    public static void syncDocument(String topicId) {
        List<TopicFile> fileList = getSpeechRecognitionInfo.confdayService.getConfFileByIssue(topicId);//議題材料列表
    }
}

 


免責聲明!

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



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