2012-03-11 52 views
0

我尝试运行以下并在主函数中获得NullPointerException。我不知道为什么这个@Autowired方法不会初始化surveyDao变量。 下面是相关的代码:使用@Autowired与静态变量

@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"); 
} 

}测试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> 

回答

2

我不知道什么是你想完成我只能告诉这不是Spring框架的典型用法。也许如果你写下你的意图,就有可能提出更好的建议。

当您运行主要方法时,根本没有处理您的注释。没有上下文构建,因此您的test-context.xml被忽略。如果要从主要方法构建上下文,请尝试:

FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext("test-context.xml"); 

并将MyTest定义为bean以查看注入surveyDao。

+0

我在main()中添加了上下文初始化,并在MyTest中定义了一个bean,但surveyDao仍然为null(bean id =“myTest”class =“collcomm.main.MyTest”init-method =“init”/>' – krltos 2012-03-11 13:18:31

+0

如果正在处理注释并创建bean,它应该可以工作。在上下文完成初始化后检查它是否为空? – mrembisz 2012-03-11 15:09:16

+0

嗯,我这样做:'FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext(“test-context.xml”); \t \t \t \t的System.out.println(surveyDao == NULL);' ,现在当我加入'surveyDao =(SurveyDao)context.getBean( “surveyDao”);'的println之前我已经正确初始化surveyDao – krltos 2012-03-11 16:19:39