我尝试运行以下命令并在main函数中获取NullPointerException。我不知道为什么这个@Autowired方法没有初始化surveyDao变量。 以下是相关代码:
@ContextConfiguration( locations = {"test-context.xml"} )
@TransactionConfiguration(defaultRollback=true)
@Transactional
public class MyTest {
protected static SurveyDao surveyDao;
@Autowired
public void setSurveyDao(SurveyDao surveyDAO){
MyTest.surveyDao = surveyDAO;
}
public static void main(String args[]) {
CollectSurvey survey = surveyDao.load("form");
}
}
test-context.xml的内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"
default-lazy-init="true"
default-autowire="byName">
<context:annotation-config/>
<!-- <bean id="applicationContextProvider" class="org.openforis.collect.context.ApplicationContextAwareImpl" /> -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="file:${user.dir}/dev.properties"/>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="${collect.devdb.url}"/>
<property name="username" value="${collect.devdb.username}" />
<property name="password" value="${collect.devdb.password}" />
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="surveyDao" class="org.openforis.collect.persistence.SurveyDao" init-method="init">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="dynamicTableDao" class="org.openforis.collect.persistence.DynamicTableDao">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
</beans>
答案 0 :(得分:2)
我不确定你要完成什么我只能说它不是spring框架的典型用法。也许如果你写下你的意图,就有可能提出更好的建议。
运行main方法时,根本不处理您的注释。没有构建上下文,因此忽略了test-context.xml
。如果你想从main方法构建上下文,请尝试:
FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext("test-context.xml");
并将MyTest定义为bean以查看surveyDao的注入。