@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);//議題材料列表 } }