2012-03-11 3 views
0

다음을 실행하여 main 함수에서 NullPointerException을 얻으려고합니다. 이 @Autowired 메소드가 surveyDao 변수를 초기화하지 않는 이유를 모르겠습니다. 다음은 관련 코드를 다음과 같이@Autowired with static variables

@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

나는 당신이 내가 단지 말할 수 달성 하려는지 확실하지 않다 스프링 프레임 워크의 일반적인 사용법은 아닙니다. 어쩌면 당신이 당신의 의도를 쓰면 더 나은 조언을 내놓을 수있을 것입니다.

주 방법을 실행할 때 주석이 전혀 처리되지 않습니다. 컨텍스트가 작성되지 않으므로 test-context.xml은 무시됩니다. 기본 메소드에서 컨텍스트를 빌드하려면 다음을 시도하십시오. surveyDao의 주입을 보려면 bean으로 MyTest를 정의하십시오.

+0

main()에서 컨텍스트 초기화를 추가하고 bean '로 MyTest를 정의했지만 surveyDao가 여전히 null입니다. – krltos

+0

어노테이션이 처리되고 Bean이 작성되면 작동해야합니다. 당신은 문맥이 초기화 된 후에 null을 확인 하는가? – mrembisz

+0

글쎄, 내가 해냈어 :'FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext ("test-context.xml"); I는'surveyDao = (SurveyDao) context.getBean ("surveyDao")를 추가 할 때' 지금; \t \t \t \t 에서 System.out.println (surveyDao == 널)는 '이전에 println I 올바르게 surveyDao – krltos