0
간단한 Java 프로젝트에서 bean을 주입하려고하는데 ProductInformationServiceImpl에 대해 NullPointerException이 발생합니다. Autowire를 사용하는 Spring - Inject Bean
이
은 내가 사용하는 클래스입니다AppConfig.java (Spring 설정 클래스)
package com.test.testing.config;
@Configuration
@ComponentScan(basePackages = "com.test")
public class AppConfig {
}
TestService.java 인터페이스 :
package com.test.testing.service;
public interface TestService {
String doIt();
}
TestService.java 구현 :
package com.test.testing.service;
@Service
public class TestServiceImpl implements TestService {
public String doIt() {
return "you did it!";
}
}
Main 클래스 :
package com.test.testing;
public class App {
public static void main(String[] args){
Test w = new Test();
System.out.println(w.getTestService().doIt());
}
}
class Test {
@Autowired
private TestService testServiceImpl;
public TestService getTestService() {
return testServiceImpl;
}
public void setTestService(TestService testServiceImpl) {
this.testServiceImpl = testServiceImpl;
}
}
내가 사용하여 빈을 검색하는 몇 가지 검사했다 :
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
TestService app = (TestService)context.getBean("testServiceImpl");
을하지만 내가하지 않으려는 무언가이다.
Autowired는 컨텍스트에서 bean을 얻은 경우에만 작동합니다. JUnit을 @RunWith (SpringJUnit4ClassRunner.class) 및 @ContextConfiguration과 함께 사용하여 테스트 할 수 있습니다. – user1516873
디버거를 사용하여 어떤 콩이 '컨텍스트'에 있는지 확인하십시오. – Jens
미안하지만, 달성하려고하는 것이 @Test가 아닙니다. 이것은 간단한 독립형 응용 프로그램입니다. ''문맥을 사용하는 것은 내가 피하려고하는 것입니다. 자동 배선을하고 싶습니다. – Tomarto