如果是在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 }