Java 使用带有静态变量的@Autowired

Java 使用带有静态变量的@Autowired,java,spring,autowired,Java,Spring,Autowired,我尝试运行以下命令并在main函数中获取NullPointerException。我不知道为什么这个@Autowired方法不初始化surveyDao变量。 以下是相关代码: @ContextConfiguration( locations = {"test-context.xml"} ) @TransactionConfiguration(defaultRollback=true) @Transactional public class MyTest { protected st

我尝试运行以下命令并在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>

我不确定您想要实现什么,我只能说这不是spring框架的典型用法。也许如果你写下你的意图,就有可能提出更好的建议

运行main方法时,根本不会处理注释。未生成上下文,因此将忽略您的
测试context.xml
。如果要从主方法生成上下文,请尝试:

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

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

我在main()中添加了上下文初始化,并将MyTest定义为bean
,但surveyDao仍然为空。如果正在处理注释并创建bean,它应该可以工作。在初始化上下文后检查它是否为null?好的,我这样做了:
FileSystemXmlApplicationContext context=newfilesystemxmlapplicationcontext(“testcontext.xml”);System.out.println(surveyDao==null)surveyDao=(surveyDao)context.getBean(“surveyDao”)打印前,我已正确初始化SurveyDaoOK。这是因为您使用了
default lazy init=“true”
,而我最初没有使用它。Spring默认情况下会预实例化单例,而不使用此选项。