2016-10-27 9 views
3

나는 Spring 프로젝트를 가지고 있으며 서비스 응용 프로그램 컨텍스트를 "spring free"다른 서비스로 전달하려고 시도합니다. "myOwnType"유형의 정규 bean이 없음 - String과 같은 기본 bean이 작동 함

내 봄 프로젝트 :

@Service 
public class MySpringService implements MyApi { 

... 

private static MySpringService INSTANCE; 

@Autowired 
private MyRepository  repo; 

@SuppressWarnings("resource") 
public static MySpringService implementation() { 
    if (INSTANCE == null) { 
     ApplicationContext ctx = new AnnotationConfigApplicationContext(MyServiceConfig.class); 
     INSTANCE = ctx.getBean(MySpringService.class); 
    } 

    return INSTANCE; 
} 

이 해당 appconfig가 있습니다 : 난 그냥 (MySpringService spgSvc = MySpringService.implementation를 호출하려고 내 다른 다른 프로젝트에서

@Configuration 
@ComponentScan("de.mypackage.my.serivce") 
public class MyServiceConfig 
... 
@Bean 
public MySpringService mySpringService() { 
    return new MySpringService(); 
} 

); NoSuchBeanDefinitionException이 리드 :

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [de.mypackage.my.serivce.MySpringService] is defined 
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:348) 
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:335) 
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1088) 
at de.mypackage.my.serivce.MySpringService.implementation(MySpringService.java:54) 
at de.mypackage.second.service.OtherService.resolveCampaignTree(OtherService.java:18) 
at de.mypackage.second.service.ResolverTest.testMe(ResolverTest.java:22) 
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:606) 
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:86) 
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) 

을 다른 한편으로는 그냥 문자열 또는 정수 쉽게 원시 콩을 전달할 수 있습니다. 또한이 방법은 다른 프로젝트에서도 이미 작동합니다! 나는 모든 코드 열을 검사했지만, 나는 아직도 내가 무엇을 엉망인지 모른다. Maven 설치가 성공적으로 수행되었습니다. 예외가 던져지는 "봄 자유로운"프로젝트를 부르는 JUNIT 테스트 안에 이번에 만 차이가 있습니다. 아이디어가있는 사람은 누구입니까?

편집

: 나는 나의 빈 정의 공공 MySpringService의 mySpringService()에서 명시 적 @Qualifier (myQualifier) ​​ 그것을 해결하기 위해 노력했다. 하지만 INSTANCE = (MySpringService) ctx.getBean ("myQualifier");으로 bean을 가져 오려고합니다. 다음 예외에 리드 :

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'myQualifier' is defined 
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:677) 
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1180) 
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:284) 
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) 
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1076) 
at de.mypackage.my.serivce.MySpringService.implementation(MySpringService.java:54) 
at de.mypackage.second.service.OtherService.resolveCampaignTree(OtherService.java:18) 
at de.mypackage.second.service.ResolverTest.testMe(ResolverTest.java:22) 
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:606) 
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:86) 
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) 

답변

1

어떤 스프링 주석이 필요한지 완전히 이해하지 못하여 문제가 발생했습니다.

  1. "구성 요소 검사"는 완전히 필요하지 않았습니다.
  2. @Service 주석과 MySpringService의 @Bean 정의가 문제를 일으킬 수 있습니다.

@Service 및 @ComponentScan을 삭제 한 후 내 서비스가 정상적으로 작동합니다. 이 통찰력은 나를 데려왔다. Grzegorz : http://www.baeldung.com/spring-nosuchbeandefinitionexception

0

나는 올바른 개념을 가지고 있지만 내가 볼 수있는 여기에 문제는 클래스와 인스턴스 플러스 콩의 이름입니다. 아래처럼 뭔가를 시도하고 옵션 1 또는 옵션 2를 선택하여 MySpringService 객체를 MyApiImplementation 클래스에 삽입 할 수 있습니다.

@Configuration 
@ComponentScan({"de.mypackage.my.serivce"}) 
public class MyServiceConfig { 

     @Bean 
     public MySpringService mySpringService() { 
      return new MySpringService(); 
     } 
     ... 
} 

@Service 
public class MyApiImplementation implements MyApi { 

    /* Option: 1 
    * The mySpringService field name below should match the method name 
    * mySpringService() in the MyServiceConfig. 
    */ 
    @Autowired 
    private static MySpringService mySpringService; 

    /* Option: 2 
    * Or the below if the name of the field (in this case instance) does not 
    * match the method name mySpringService() in the MyServiceConfig. 
    */ 
    @Autowired 
    @Qualifier("mySpringService") 
    private static MySpringService instance; 

    ... 
}