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
你是如何實例ConnectorScheduler - 我沒有看到一個定義它 –
extols
2014-12-02 12:32:38
請包括完整的堆棧跟蹤 – Secondo 2014-12-02 12:37:44