3

I have a Spring project and try to pass the services application context to an other service which is "spring free".

My Spring project:

@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;
}

This is the corresponding AppConfig:

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

In my other other project I just try to call MySpringService spgSvc = MySpringService.implementation(); which leads to a 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)

On the other hand I can pass easily primitive beans which are just a String or Integer. Also this approach already works in other projects! I checked every code row but I still do not know what I messed up here. Maven install worked successful. Only difference this time I am within a JUNIT test which calls the "spring free" project where the exception is thrown. Anyone here who has an idea?

EDIT: I tried to solve it with an explicit @Qualifier(myQualifier) at my bean definition public MySpringService mySpringService(). But trying to get the bean with INSTANCE = (MySpringService) ctx.getBean("myQualifier"); leads to following exception:

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)
grimbo
  • 95
  • 1
  • 11

2 Answers2

1

My problem occurred due not fully understanding which used Spring annotations are necessary.

  1. The "component scan" was completely unnecessary.
  2. The @Service annotation and the @Bean definition of MySpringService might lead to the problems.

After deleting the @Service and @ComponentScan my services works fine! This insight brought me Grzegorz: http://www.baeldung.com/spring-nosuchbeandefinitionexception

grimbo
  • 95
  • 1
  • 11
0

I have the right concept but the issue here that I can see is the naming of the class and its instances plus the beans. Try doing something like the below and you can choose either option 1, or option 2 to inject the MySpringService object in class 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;

    ...
}