java – Spring 3 @Autowired注释问题

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

简而言之,我得到一个NullPointerException,我在AutoWired类上调用一个方法.

我的班级是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中设置以下内容.

spring-beans.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context.xsd">

 fig/>


  

最后我有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):

cope>testcope>
spring-beans

更新

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

public class App 
{   
    @Autowired
    private static SayHello hello;

    public static void main( String[] args )
    {
       hello.greet();
    }
}
最佳答案
您需要在AppTest上添加一些注释:

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

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

cope>testcope>

相关文章

这篇文章主要介绍了spring的事务传播属性REQUIRED_NESTED的原...
今天小编给大家分享的是一文解析spring中事务的传播机制,相...
这篇文章主要介绍了SpringCloudAlibaba和SpringCloud有什么区...
本篇文章和大家了解一下SpringCloud整合XXL-Job的几个步骤。...
本篇文章和大家了解一下Spring延迟初始化会遇到什么问题。有...
这篇文章主要介绍了怎么使用Spring提供的不同缓存注解实现缓...