gpt4 book ai didi

java - 将 @Autowired 与静态变量一起使用

转载 作者:行者123 更新时间:2023-12-02 07:55:47 27 4
gpt4 key购买 nike

我尝试运行以下命令并在 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 方法时,您的注释根本不会被处理。没有构建上下文,因此您的 test-context.xml 将被忽略。如果您想从主方法构建上下文,请尝试:

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

并将 MyTest 定义为一个 bean 以查看 SurveyDao 的注入(inject)。

关于java - 将 @Autowired 与静态变量一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9654818/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com