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
答案 0 :(得分:2)
只有在需要自动装配值的bean由Spring容器管理时,Spring依赖注入才有效。您的上下文定义不包含ConnectorScheduler
的bean定义,所以我假设您正在尝试手动创建它,这会导致注入问题。
添加适当的定义可以解决您的问题。
<bean id="connectorScheduler" class="org.your.package.ConnectorScheduler" />