我一直在看一些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();
}
}
輝煌,做到了!我的測試現在正在按照我的預期傳遞和打印輸出。我是否必須做類似的事情才能使自動裝配與主應用程序一起工作?謝謝@zagyi! – cast01 2013-02-24 14:40:00
不,如果您使用在您鏈接的文章中定義的App類,它將按照xml配置中的 行指示處理註釋。 –
zagyi
2013-02-24 14:44:15
Cheers @zagyi,我不確定是否有一種方法可以做到這一點,而無需實例化BeanFactory並顯式使用getBean(),id喜歡使用自動佈線註釋。 – cast01 2013-02-24 14:47:26