2016-04-20 6 views
0

In meinem Frühling Boot-Anwendung null ist, ich habe eine solche @Configuration Klasse:@Autowired RabbitTemplate trotz bestehender @Bean es

@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); 
} 
} 

und ich kann in meinen Logs sehen, dass es ganz gut läuft.

Ich habe auch einen Quartz Job, wie

@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); 
} 
} 

und in diesem Code amqpTemplate ist null konfiguriert ist. Ich bin verwirrt, was könnte ein Grund für dieses Verhalten sein?

+0

Wie viele Container haben Sie konfiguriert? – hahn

+1

Wie instanziieren Sie QuartzMockingJob? – gmaslowski

+0

@ gmaslowski über newJob (QuartzMockingJob.class). Ich denke, das könnte das Problem verursachen. –

Antwort

1

Dies wurde selbst gelöst, da der grundlegende Quarzuhr-Job außerhalb des automatischen Verdrahtungsschemas liegt, sofern nicht einige Aktionen ausgeführt werden.

Basierend auf https://github.com/davidkiss/spring-boot-quartz-demo, war ich erfolgreich in der Lage, einen Job mit @autowiring innerhalb seiner Definition hinzuzufügen.