没有 "myOwnType" 类型的合格 bean - 像 String 这样的原始 bean 可以工作

No qualifying bean of type "myOwnType" - Primitive beans like String works

我有一个 Spring 项目并尝试将服务应用程序上下文传递给另一个服务 "spring free"。

我的 Spring 项目:

@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:

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

在我的其他项目中,我只是尝试调用 MySpringService spgSvc = MySpringService.implementation(); 导致 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.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.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access[=13=]0(ParentRunner.java:53)
at org.junit.runners.ParentRunner.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)

另一方面,我可以轻松传递原始 bean,它们只是一个字符串或整数。这种方法也已经在其他项目中起作用了!我检查了每一行代码,但我仍然不知道我在这里搞砸了什么。 Maven 安装成功。唯一不同的是,这次我在 JUNIT 测试中调用抛出异常的 "spring free" 项目。这里有人有想法吗?

编辑: 我试图在我的 bean 定义 @Qualifier(myQualifier) MySpringService mySpring 中用显式 @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.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.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access[=14=]0(ParentRunner.java:53)
at org.junit.runners.ParentRunner.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)

我的概念是正确的,但我在这里看到的问题是 class 及其实例加上 bean 的命名。尝试执行类似下面的操作,您可以选择选项 1 或选项 2 以在 class MyApiImplementation 中注入 MySpringService 对象。

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

    ...
}

我的问题是由于没有完全理解哪些使用 Spring 注释是必要的。

  1. "component scan"完全没有必要。
  2. MySpringService 的@Service 注释和@Bean 定义可能会导致问题。

删除@Service 和@ComponentScan 后我的服务工作正常!这种洞察力让我想到了 Grzegorz:http://www.baeldung.com/spring-nosuchbeandefinitionexception