java - Spring Autowiring : doesn't work

标签 java spring quartz-scheduler

Autowiring quartz中使用的类时出现java.lang.NullPointerException

入口点是:

public class AppContainer {
    public static void main(String[] args) {
        ApplicationContext c1 = new ClassPathXmlApplicationContext("rabbit-listener-context.xml");
    }
}

rabbit-listener-context.xml 是:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:c="http://www.springframework.org/schema/c"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:task="http://www.springframework.org/schema/task"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xmlns:rabbit="http://www.springframework.org/schema/rabbit"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
       http://www.springframework.org/schema/task
       http://www.springframework.org/schema/task/spring-task-3.2.xsd
       http://www.springframework.org/schema/rabbit
       http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-2.5.xsd">

   <context:annotation-config />
    <bean id="placeholderConfig"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>app.conf</value>
            </list>
        </property>
    </bean>

    <import resource="dataSources.xml" />

</beans>

dataSources.xml 是:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xmlns:jdbc="http://www.springframework.org/schema/jdbc" 
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
       http://www.springframework.org/schema/jdbc 
       http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    <context:annotation-config />
    <import resource="properties.xml"/>

    <bean id="dataSource"  class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.postgresql.Driver"/>
        <property name="url" value="jdbc:postgresql://${db.host}/example"/>
        <property name="username" value="${db.userName}"/>
        <property name="password" value="${db.password}"/>
    </bean>


    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" >
        <constructor-arg type="javax.sql.DataSource"><ref bean="dataSource"></ref></constructor-arg>
    </bean>
</beans>

那么要连接的类是:

public class ConnectorScheduler implements Job {

    final private String sqlQuery = ...";
    private JdbcTemplate jdbcTemplate;

    @Autowired(required = true) 
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
    private AmqpTemplate rabbitMQTemplate;

    @Autowired(required = true) 
    public void setAmqpTemplate(AmqpTemplate rabbitMQTemplate) {
        this.rabbitMQTemplate = rabbitMQTemplate;
    }

    private String queueName;

    public void execute(JobExecutionContext context) {
     //   SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
        Long connectorID = context.getJobDetail().getJobDataMap().getLong("ConnectorID");
           System.out.println("jdbc templaet is "+jdbcTemplate);
        SchedulerBean schedulerBean= jdbcTemplate.query(sqlQuery,new Object[] {connectorID}, new SchedulerMapper()).iterator().next();

..

但它出错,这意味着 jdbcTemplate 未连接。

java.lang.NullPointerException

最佳答案

仅当需要 Autowiring 值的 bean 由 Spring 容器管理时,Spring 依赖注入(inject)才有效。您的上下文定义不包含 ConnectorScheduler 的 bean 定义,因此我假设您尝试手动创建它,这会导致注入(inject)问题。

添加适当的定义应该可以解决您的问题。

<bean id="connectorScheduler" class="org.your.package.ConnectorScheduler" />

关于java - Spring Autowiring : doesn't work,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27249521/

相关文章:

Java 8 Spliterator(或类似的)返回一个值,前提是只有一个值

java - spring websocket 中的 MultiServerUserRegistry 是什么?

java - 如何保存 Quartz 执行的作业?

Java 任务 setOnSucceded 不起作用

java - 将字节数组分解为不同的数据类型?

java - AES 文件解密 “given final block not properly padded”

spring - Faker for Grails 给出变量未定义错误

Spring MVC 在 select 标签中保留选定的值

grails - 禁用 Grails Quartz 作业并在 Controller 中启动它

quartz-scheduler - 使用 Quartz.NET 安排任务每天在特定时间运行