在我的Spring Boot应用程序中,我有一个@Configuration类:
@Configuration
public class AmqpConnectionConfig {
@Bean
@Primary // has to be here because of https://github.com/spring-projects/spring-boot/issues/2011
AmqpTemplate inquiryRabbitTemplate(ConnectionFactory factory) {
RabbitTemplate rabbitTemplate = new RabbitTemplate(factory);
return rabbitTemplate;
}
@Bean
ConnectionFactory connectionFactory() {
ConnectionFactory factory = new ConnectionFactory();
// Some host/port/password setup skipped...
return new CachingConnectionFactory(factory);
}
}
我可以在我的日志中看到它运行得很好。
我还有一个配置为
的Quartz作业 @Service
public class QuartzMockingJob implements Job {
@Autowired
AmqpTemplate amqpTemplate;
public QuartzMockingJob() {
// Instances of Job must have a public no-argument constructor.
}
public void execute(JobExecutionContext context) throws JobExecutionException {
// here I create object called "mock"
if (amqpTemplate!=null) {
amqpTemplate.convertAndSend("amq.fanout", "my.routing.key", mock);
}
}
并且在此代码中amqpTemplate为null。我很困惑,这种行为可能是什么原因?
答案 0 :(得分:1)
这已经解决了,因为基本的Quartz作业不属于自动布线模式,除非采取某些行动。
基于https://github.com/davidkiss/spring-boot-quartz-demo,我成功地在其定义中添加了@autowiring的作业。