2012-03-11 16 views
0

私は以下を実行し、main関数でNullPointerExceptionを取得しようとします。なぜこの@AutowiredメソッドがsurveyDao変数を初期化しないのかわかりません。以下 は、関連するコードです:次のようにテストのcontext.xmlの@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"); 
} 

}

内容は次のとおりです。

<?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 "); \t \t \t \tのSystem.out.println(surveyDao == NULL); ' 、今、私は' surveyDaoは=(SurveyDao)context.getBean( "surveyDao")を加え; 'のprintln前に私が正しくオーケーsurveyDao – krltos