【问题标题】:Spring 3 @Autowired Annotation IssuesSpring 3 @Autowired 注释问题
【发布时间】:2013-02-09 18:02:08
【问题描述】:

我一直在看一些 Spring Framework 入门教程,并遇到了this one。完成后,我遇到了以下错误。 (我已经更新为使用 Spring 框架的第 3 版,但仍然遇到相同的错误。

简而言之,我在调用 AutoWired 类的方法时收到 NullPointerException。

我的班级是 SayHello:

public class SayHello {

private String name;

public void setName(String name) {
    this.name = name;
}

public String getName() {
    return name;
}

public void greet() {
    System.out.println("Hello " + getName());
}

}

然后我在 /src/test/resources/applicationContext.xml 中设置以下内容。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context.xsd">

 <context:annotation-config/>


  <bean id="hello" class="package.SayHello">
    <property name="name" value="John Smith" />
  </bean

最后我有了 AppTest 类,我尝试在其中自动连接 SayHello 对象以进行测试。

public class AppTest {

@Autowired
private SayHello hello;

@Test
public void testApp()
{
    hello.greet();
    Assert.assertTrue( true );
}
}

当我尝试运行 AppTest 时出现以下错误:

java.lang.NullPointerException
at package.AppTest.testApp(AppTest.java:19)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

我查看了有关 stackoverflow 的各种文章和问题,但在尝试了一些但没有得到任何结果之后,我想我会创建一个新帖子。

干杯。

如果有帮助,这是我正在使用的依赖项(Spring v3.2.1):

<dependencies>

<!-- JUnit -->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
    <scope>test</scope>
</dependency>


<!-- Spring -->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-beans</artifactId>
  <version>${org.springframework.version}</version>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-core</artifactId>
  <version>${org.springframework.version}</version>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-webmvc</artifactId>
  <version>${org.springframework.version}</version>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context-support</artifactId>
  <version>${org.springframework.version}</version>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-aop</artifactId>
  <version>${org.springframework.version}</version>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-jms</artifactId>
  <version>${org.springframework.version}</version>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>${org.springframework.version}</version>
</dependency>

更新

我还尝试在主应用程序中使用@Autowired 注释,如下所示。并且仍然得到 NullPointerException 错误。

public class App 
{   
    @Autowired
    private static SayHello hello;

    public static void main( String[] args )
    {
       hello.greet();
    }
}

【问题讨论】:

    标签: java spring junit autowired


    【解决方案1】:

    您需要在AppTest 上添加一些注释:

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = "classpath:/applicationContext.xml")
    

    这些注解是在spring-test中定义的,所以也要加上这个依赖:

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>${org.springframework.version}</version>
        <scope>test</scope>
    </dependency>
    

    【讨论】:

    • 太棒了,做到了!我的测试现在按我的预期通过并打印输出。我是否必须做类似的事情才能使自动装配与主应用程序一起工作?谢谢@zagyi!
    • 不,如果您使用链接的文章中定义的App类,它将按照xml配置中&lt;context:annotation-config/&gt;行的指示处理注释。
    • 干杯@zagyi,我不确定是否有办法做到这一点,而不必实例化BeanFactory并显式使用getBean(),我喜欢使用自动装配注释。
    • 这是必需的,因为必须有一些东西可以引导 Spring。在测试的情况下,它是SpringJUnit4ClassRunner(与 JUnit 框架一起使用),在 webapp 中它由 web.xml 中配置的 ContextLoaderListener 完成,但在独立应用程序中,您必须手动完成。跨度>
    • 啊,我现在明白了,有道理!感谢您澄清@zagyi! :)
    【解决方案2】:

    @Autowired 注释只有在 AppTest 本身由 Spring 管理时才有效。看起来您正在一个简单的单元测试中测试您的设置。这行不通。如果您想在 JUnit 测试中自动装配,您将不得不查看 Spring 的测试支持。但这是一个完全不同的话题。

    【讨论】:

    • 感谢@chkal,我也在主应用程序中尝试了 Autowired,但仍然出现同样的错误,我更新问题以显示这一点。
    【解决方案3】:

    在教程中,您已链接此代码实例化 bean:

    BeanFactory factory = new XmlBeanFactory(
       new ClassPathResource("application-context.xml"));
    
    SayHello hello = (SayHello) factory.getBean("hello");
    

    您的 SayHello bean 为空。

    【讨论】:

    • 感谢@Aubin,我遇到了许多仅依靠 applicationContext.xml 来处理 bean 的示例。我相信本教程使用该方法,因为它使用 Spring 2。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-17
    • 2015-12-19
    • 1970-01-01
    相关资源
    最近更新 更多