spring boot在啟動項目之后執行的實現方法


如果是在springboot啟動過程中調用實現方法的話,實現方法里的不能讀取到application.properties配置文件里的配置信息

具體實現代碼如下

 1 import java.io.IOException;
 2 import java.util.concurrent.TimeoutException;
 3 
 4 import org.springframework.beans.factory.annotation.Autowired;
 5 import org.springframework.context.ApplicationListener;
 6 import org.springframework.context.annotation.Configuration;
 7 import org.springframework.context.event.ContextRefreshedEvent;
 8 
 9 import com.rabbitmq.client.ConsumerCancelledException;
10 import com.rabbitmq.client.ShutdownSignalException;
11 import com.yarlung.service.rabbitmq.CustomerMQ;
12 
13 @Configuration
14 public class ApplicationStartup implements ApplicationListener<ContextRefreshedEvent>{ 
15      @Autowired
16      private CustomerMQ customerMQ;
17      
18      @Override
19      public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {  //項目啟動后,執行啟動消費者方法
20          try {
21             customerMQ.customer();   //消費者的實現方法
22         } catch (ShutdownSignalException | ConsumerCancelledException | IOException | TimeoutException
23                 | InterruptedException e) {
24             // TODO Auto-generated catch block
25             e.printStackTrace();
26         }
27      }
28      
29 }

 


免責聲明!

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



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