2014-12-02 5 views
0

java.lang.NullPointerException, когда autowire класса, используемый в кварцепружины autowired: не работает

точка входа:

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

кролик-слушатель-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 
+1

Как вы инстанцировании ConnectorScheduler - Я не вижу определение для него – extols

+0

пожалуйста, включите полный StackTrace – Secondo

ответ

2

Впрыск с пружинной зависимостью работает только в том случае, если компонент, которому требуются автономные значения, управляется весенним контейнером. Определение вашего контекста не содержит определения компонента для ConnectorScheduler, поэтому я предполагаю, что вы пытаетесь создать его вручную, что вызывает проблему с инъекцией.

Добавление соответствующего определения должно решить вашу проблему.

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

ой, я использую «newJob (HelloJob.class)», чтобы инициализировать новую работу. –