quartz autowired field为null

时间:2017-04-18 08:24:30

标签: spring quartz-scheduler

我有QuartzJobBean

public abstract class SqsQueueListener extends QuartzJobBean {

    @Value("test-queue")
    private String queueName;

    @Autowired
    private AmazonSQS sqsClient;

    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        LOGGER.info("Attempt to get message for SQS queue for client: [{}].", sqsClient);
        GetQueueUrlResult result = sqsClient.getQueueUrl(queueName);
    }

    protected abstract void notificationAction(String message);

}

我的非抽象实现扩展了该类,但没有什么特别之处。 预计autowire sqsClient。

SqsClient是使用FactoryBean

创建的
<bean id="sqsClient" class="pack.SqsClientFactoryBean"/>

在日志中,它向我显示对象已成功创建的消息,因此工厂本身可以正常工作。

xml配置如下所示

<bean id="sqsProcessingJob" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
    <property name="jobClass" value="pack.RuleReloadingSqsService"/>
    <property name="jobDataAsMap">
        <map> 
            <entry key="sqsClient" value="sqsClient"/>
        </map>
    </property>
</bean>

<bean id="sqsTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
    <property name="jobDetail" ref="sqsProcessingJob"/>
    <property name="repeatInterval" value="10000"/>
</bean>

<bean class="pack.quartz.DisablingSchedulerFactoryBean">
    <property name="triggers">
        <list>
            <ref bean="sqsTrigger"/>
        </list>
    </property>
</bean>

但是,当我运行我的应用程序时,我sqsClientnull。有什么问题?

1 个答案:

答案 0 :(得分:0)

我忘了为sqsClient字段添加setter。

private AmazonSQS sqsClient;

@Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {}

public void setSqsClient(AmazonSQS sqsClient) {
    this.sqsClient = sqsClient;
}