2013-02-24 14 views
5

私はSpring Frameworkを始めるためのいくつかのチュートリアルを見て、this oneを見つけました。それに続いて、私は以下のエラーに遭遇しました。 (私は以来、Springフレームワークのバージョン3を使用するように更新していると私はまだ同じエラーを取得しています。要するに Spring 3 @Autowired Annotation Issues

が、私はAutoWiredクラスのメソッドを呼び出す場合NullPointerExceptionを取得しています。

私のクラスsayHelloです。私は、その後/src/test/resources/applicationContext.xmlで、次の設定

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()); 
} 

} 

<?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 

最後に、テストするSayHelloオブジェクトをAutoWireしようとする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の上ではなく、いくつかをしようとどこにもなっていない後、思想、さまざまな記事や質問を見て持っていた:私はAppTestを実行しようとすると、私は次のエラーを取得する

public class AppTest { 

@Autowired 
private SayHello hello; 

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

私は新しい投稿を作成します。

乾杯。

それが助け場合、私は(春V3.2.1)を使用していHERESに依存関係:

<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エラーが発生します。 AppTest自体は春によって管理されている場合

public class App 
{ 
    @Autowired 
    private static SayHello hello; 

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

答えて

6

あなたはAppTestにいくつかの注釈が必要になります

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

すばらしかったよ!私のテストは、今通り、私が期待していた通りに通過し、印刷出力しています。メインアプリケーションでオートワイヤリングを行うには、同様のことをしなければなりませんか?おかげで@ zagyi! – cast01

+0

いいえ、あなたがリンクした記事で定義されているように 'App'クラスを使うと、xml設定の' '行で指示されたように注釈を処理します。 – zagyi

+0

私はBeanFactoryをインスタンス化せずにgetBean()を明示的に使用しなくても、autowiredアノテーションを使用するような方法があるかどうかは分かりませんでした。 – cast01

3

@Autowired注釈にのみ、動作します。簡単な単体テストで設定をテストしているようです。これは動作しません。 JUnitテストでオートワイヤリングが必要な場合は、Springのテストサポートを参照する必要があります。しかし、それはまったく別の話題です。

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

これらの注釈は、春試験に定義されているので、同様にこの依存関係を追加します:

+0

ありがとうございます@chkal、私はまた、メインアプリケーションでAutowiredしようとしても、同じエラーが表示され、それを表示する質問を更新します。 – cast01

1

をチュートリアルでは、このコードをリンクしていますbeanをインスタンス化します。

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

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

SayHello bean 無効である。

+0

ありがとう@Aubin、私は豆を処理するapplicationContext.xmlに依存している多くの例を見てきました。このチュートリアルでは、Spring 2を使用する場合と同じ方法を使用していると思います。 – cast01

関連する問題