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