스프링 프레임 워크를 시작하기위한 튜토리얼을 살펴본 결과 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();
}
}
똑똑하고, 그랬어! 필자의 예상대로 테스트가 끝나고 출력 결과가 출력됩니다.주 응용 프로그램에서 autowiring을 사용하려면 비슷한 작업을 수행해야합니까? 감사합니다 @ zagyi! – cast01
아니요, 링크 된 기사에 정의 된대로'App' 클래스를 사용하면 xml 구성의' '행에 따라 주석을 처리합니다. –
zagyi
건배 @zagyi, BeanFactory를 인스턴스화하지 않고 명시 적으로 getBean()을 사용하지 않고도 자동 정렬 주석을 사용하는 것처럼 할 수있는 방법이 있는지 확실하지 않았습니다. – cast01