2013-02-24 97 views
5

我一直在看一些Spring Framework入门教程,并且遇到了this one。通过后,我遇到了下面的错误。 (因为我已经更新为使用Spring Framework的版本3,我仍然得到同样的错误。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类,我尝试AutoWire 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) 

我有一个了解各种不同的文章和计算器的问题,但尝试了几个,没有得到任何地方后,思想我会创建一个新帖子。

干杯。

如果有帮助,继承人的依赖,我使用(春季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(); 
    } 
} 

回答

6

你会需要一些注解AppTest

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

这些注解是在春季测试定义的,所以添加这种依赖性,以及:

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

辉煌,做到了!我的测试现在正在按照我的预期传递和打印输出。我是否必须做类似的事情才能使自动装配与主应用程序一起工作?谢谢@zagyi! – cast01 2013-02-24 14:40:00

+0

不,如果您使用在您链接的文章中定义的App类,它将按照xml配置中的行指示处理注释。 – zagyi 2013-02-24 14:44:15

+0

Cheers @zagyi,我不确定是否有一种方法可以做到这一点,而无需实例化BeanFactory并显式使用getBean(),id喜欢使用自动布线注释。 – cast01 2013-02-24 14:47:26

3

@Autowired注释只会工作,如果AppTest本身是由Spring管理。它看起来像你在一个简单的单元测试中测试你的设置。这不起作用。如果你想在JUnit测试中自动装配,你将不得不看看Spring的测试支持。但这是一个完全不同的话题。

+0

谢谢@chkal,我也尝试过在主应用程序中使用Autowired,但仍然得到相同的错误,不适当更新问题以显示该问题。 – cast01 2013-02-24 14:33:24

1

在教程中,您链接的代码实例化豆​​类:

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

SayHello hello = (SayHello) factory.getBean("hello"); 

您的SayHello bean 一片空白。

+0

感谢@Aubin,我遇到了很多依靠applicationContext.xml处理bean的示例。我相信教程使用Spring 2的方法。 – cast01 2013-02-24 14:35:02