2014-12-02 170 views
0

顯示java.lang.NullPointerException自動裝配在石英使用的類彈簧自動裝配:不起作用

入口點是當:

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 2014-12-02 12:32:38

+0

請包括完整的堆棧跟蹤 – Secondo 2014-12-02 12:37:44

回答

2

僅當需要自動裝載值的bean由Spring容器管理時,Spring依賴注入纔有效。您的上下文定義不包含ConnectorScheduler的bean定義,所以我假設您正在嘗試創建它,從而導致注入問題。

添加適當的定義應該可以解決您的問題。

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

哦,我使用「newJob(HelloJob.class)」給init新的工作。 – 2014-12-03 00:55:04