2013-02-24 3 views
5

스프링 프레임 워크를 시작하기위한 튜토리얼을 살펴본 결과 this one이 나왔습니다. 뒤이어서 나는 아래의 오류를 만났다. (I 이후 스프링 프레임 워크의 버전 3을 사용하도록 업데이트하고 난 여전히 같은 오류를 얻고있다. 한마디로 Spring 3 @Autowired Annotation Issues

, 나는 내가 autowire가 클래스에 메소드를 호출는 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 클래스가 있습니다.

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)를 사용하고있어 그, 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 오류가 발생합니다.

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

똑똑하고, 그랬어! 필자의 예상대로 테스트가 끝나고 출력 결과가 출력됩니다.주 응용 프로그램에서 autowiring을 사용하려면 비슷한 작업을 수행해야합니까? 감사합니다 @ zagyi! – cast01

+0

아니요, 링크 된 기사에 정의 된대로'App' 클래스를 사용하면 xml 구성의''행에 따라 주석을 처리합니다. – zagyi

+0

건배 @zagyi, BeanFactory를 인스턴스화하지 않고 명시 적으로 getBean()을 사용하지 않고도 자동 정렬 주석을 사용하는 것처럼 할 수있는 방법이 있는지 확실하지 않았습니다. – cast01

3

@Autowired 주석에만 AppTest 경우 그 자체가 Spring에 의해 관리되고, 작동합니다. 간단한 단위 테스트로 설정을 테스트하는 것 같습니다. 이것은 작동하지 않습니다. JUnit 테스트에서 자동 와이어 링을 원한다면 Spring 테스트 지원을 살펴 봐야 할 것이다. 그러나 그것은 완전히 다른 주제입니다.

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

이 주석이 봄 테스트에 정의되어 있습니다, 그래서뿐만 아니라이 종속성을 추가 :

+0

감사합니다. @chkal, 나는 또한 주 응용 프로그램에서 Autowired를 시도했지만 여전히 동일한 오류가 발생합니다. – cast01

1

를 튜토리얼에서는이 코드를 연결 한 콩을 인스턴스화합니다.

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

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

SayHello 콩 null

+0

고마워요 @Aubin, bean을 처리하기 위해 applicationContext.xml에 의존하는 많은 예제를 보았습니다. 나는이 튜토리얼이 Spring 2를 사용하면서 그 메소드를 사용한다고 믿는다. – cast01

관련 문제