【问题标题】:using @Autowired with static variable使用带有静态变量的 @Autowired
【发布时间】:2012-03-28 03:06:20
【问题描述】:

我尝试运行以下命令并在主函数中获取 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>

【问题讨论】:

    标签: java spring autowired


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 我在 main() 中添加了上下文初始化,并将 MyTest 定义为 bean &lt;bean id="myTest" class="collcomm.main.MyTest" init-method="init" /&gt;,但surveyDao 仍然为空
    • 如果正在处理注释并创建 bean,它应该可以工作。在上下文完成初始化后检查它是否为空?
    • 好吧,我这样做了:FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext("test-context.xml"); System.out.println(surveyDao==null); 现在当我在 println 之前添加surveyDao = (SurveyDao)context.getBean("surveyDao"); 时,我已经正确初始化了surveyDao
    • 好的。那是因为你使用了我一开始错过的default-lazy-init="true"。 Spring 在没有 this 的情况下默认预实例化单例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-27
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 2023-03-18
    • 2021-10-28
    相关资源
    最近更新 更多