[spring-projects/spring-boot]将 @SpyBean 与 @Qualifer 一起使用在 Spring 1.5.x 中不起作用

2019-03-22 88 views
5

大家好,在 Spring boot 文档中它说

如果请求类型有多个 bean,则必须在字段级别指定限定符元数据:

@SpyBean
@Qualifier("example")
private ExampleService service;

这在 Spring boot 1.5.19.RELEASE 上对我不起作用。简单的例子是

@SpringBootApplication
public class QualifierspyApplication {
    public static void main(String[] args) {
        SpringApplication.run(QualifierspyApplication.class, args);
    }

    @Primary
    @Bean("simpleService1")
    public SimpleService simpleService1() {
        return new SimpleService("hello 1");
    }

    @Bean("simpleService2")
    public SimpleService simpleService2() {
        return new SimpleService("hello 2");
    }
}

测试是

@RunWith(SpringRunner.class)
@SpringBootTest
public class QualifierspyApplicationFailingTest {
    @SpyBean
    @Qualifier("simpleService2")
    private SimpleService simpleService2;

    @Test
    public void testSpy() {
        Assert.assertEquals("hello 2", simpleService2.method());
    }
}

该测试仅在我输入@SpyBean(name = "simpleService2").如果我删除@Primary它会失败

java.lang.IllegalStateException: No bean found for definition [SpyDefinition@209da20d name = '', typeToSpy = se.lolotron.qualifierspy.SimpleService, reset = AFTER]

您可以在此处找到示例。

回答

4

感谢您提供样品。这对我来说看起来像是一个错误。确定 bean 名称时可能应该查看限定符

4

@mbhave 我可以解决这个问题bug吗?

2

@rhamedy 感谢您的提议。我们将于本周五发布,我希望能提前解决这个问题。如果您在接下来的 2 天内有时间提出建议,请继续。

7

@snicoll 我查看了determineBeanName@mbhave 的上述评论中提到的内容,但我无法弄清楚如何访问@Qualifier其中注释的值。不会SpyDefinition暴露任何东西。

我也遇到了这个问题https://github.com/spring-projects/spring-boot/pull/11077,虽然它与此无关,但bug@Primary.

我可以在晚上再次研究这个问题(如果不会太晚的话),如果能得到一些帮助/建议那就太好了。

6

我想我有办法解决!基本上在determineBeanName检查后的方法中if (StringUtils.hasText(definition.getName()))我执行以下操作

  • 循环遍历传递的bean

  • 对于每个 bean 检查是否definition.getQualifier()不为空

  • 使用 来QualifierDefinition.matches(...)判断 bean 名称是否与Qualifier值匹配,如果为 true 那么这就是正确的determinedbean 名称

  • 如果上述步骤未能返回,则继续执行正常流程并检查@Primary

使用这个解决方案,失败的测试1.5.x正在通过,我需要做更多检查。 if 检查的使用QualifierDefinition.matches(...)有点可怕,但很有效。修复后该determineBeanName方法如下所示

private String determineBeanName(String[] existingBeans, SpyDefinition definition,
            BeanDefinitionRegistry registry) {
        if (StringUtils.hasText(definition.getName())) {
            return definition.getName();
        }

        for (String bean : existingBeans) {
            if (definition.getQualifier() != null) {
                if (definition.getQualifier().matches(
                        (ConfigurableListableBeanFactory) this.beanFactory, bean)) {
                    return bean;
                }
            }
        }

        if (existingBeans.length == 1) {
            return existingBeans[0];
        }
        return determinePrimaryCandidate(registry, existingBeans,
                definition.getTypeToSpy());
    }

for loop是新增加的吗?有任何反馈吗? @斯尼科尔

4

@rhamedy 这determineBeanName只是我最初的预感。我没有仔细看过。我刚刚看了一下 master 上的代码,根据您提到的问题,它已经被重构了很多。这不是 and master 中的错误,因为这里2.1.x检查了限定符。

让我们看看团队的其他成员是否认为这个问题只值得修复1.5.x

6

我注意到了这个1.5.x标签,我也知道它在master.我的修复是为了1.5.x(基于它)。对我来说听起来不错,无论如何我都会推动 PR,并将其留给团队来决定什么是最好的。

5

鉴于存在解决方法,我认为我们不应该在 1.5.x 中修复此问题,特别是因为它将在 4 个多月的时间内达到 EOL。

0

我同意安迪的观点。考虑到 1.5.x 的生命周期有限,我们只想针对严重错误而触及 1.5.x。

5

你好,我在 2.1.7 版本中遇到了同样的问题

4

@surapuramakhil Madhura上面已经指出这应该适用于2.1.x.如果您不这么认为,请附上一个我们可以自己运行的小样本,我们可以重新讨论此问题。

1

@snicoll 你可以尝试运行下面提到的测试吗

https://github.com/surapuramakhil/springBootTestDataJpaIssue/blob/master/src/test/java/com/akhil/demo/SpyQualifierTest.java

如果您使用模拟 bean 而不是间谍,那么 bean 限定符就可以工作。但理想情况下,限定符也应该适用于 SpyBean。

当我运行测试时,我收到以下输出。

9:46:23.876 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.akhil.demo.SpyQualifierTest]
19:46:23.876 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.akhil.demo.SpyQualifierTest]
19:46:23.899 [main] DEBUG org.springframework.test.context.support.TestPropertySourceUtils - Adding inlined properties to environment: {spring.jmx.enabled=false, org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true, server.port=-1}

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.6.RELEASE)

2020-04-18 19:46:24.146  INFO 21715 --- [           main] com.akhil.demo.SpyQualifierTest          : Starting SpyQualifierTest on tech-laptop-bsd with PID 21715 (started by akhil-kfn in /home/akhil-kfn/OpenSource/springBootTestDataJpaIssue)
2020-04-18 19:46:24.147  INFO 21715 --- [           main] com.akhil.demo.SpyQualifierTest          : No active profile set, falling back to default profiles: default
2020-04-18 19:46:24.528  WARN 21715 --- [kground-preinit] o.s.h.c.j.Jackson2ObjectMapperBuilder    : For Jackson Kotlin classes support please add "com.fasterxml.jackson.module:jackson-module-kotlin" to the classpath
2020-04-18 19:46:24.925  WARN 21715 --- [           main] o.s.w.c.s.GenericWebApplicationContext   : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dummyController': Unsatisfied dependency expressed through field 'lucky'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'okhttp3.OkHttpClient' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=okhttpBean)}
2020-04-18 19:46:25.223 ERROR 21715 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Field lucky in com.akhil.demo.controllers.DummyController required a bean of type 'okhttp3.OkHttpClient' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)
    - @org.springframework.beans.factory.annotation.Qualifier(value=okhttpBean)

The following candidates were found but could not be injected:
    - User-defined bean

Action:

Consider revisiting the entries above or defining a bean of type 'okhttp3.OkHttpClient' in your configuration.

2020-04-18 19:46:25.227 ERROR 21715 --- [           main] o.s.test.context.TestContextManager      : Caught exception while allowing TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener@294425a7] to prepare test instance [com.akhil.demo.SpyQualifierTest@22fa55b2]

java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:132) ~[spring-test-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:123) ~[spring-test-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:190) ~[spring-test-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:132) ~[spring-test-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:244) ~[spring-test-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227) [spring-test-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289) [spring-test-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) [junit-4.12.jar:4.12]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291) [spring-test-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:246) [spring-test-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97) [spring-test-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) [junit-4.12.jar:4.12]
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) [junit-4.12.jar:4.12]
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) [junit-4.12.jar:4.12]
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) [junit-4.12.jar:4.12]
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) [junit-4.12.jar:4.12]
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) [spring-test-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) [spring-test-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363) [junit-4.12.jar:4.12]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190) [spring-test-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137) [junit-4.12.jar:4.12]
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) [junit-rt.jar:na]
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33) [junit-rt.jar:na]
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230) [junit-rt.jar:na]
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58) [junit-rt.jar:na]
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dummyController': Unsatisfied dependency expressed through field 'lucky'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'okhttp3.OkHttpClient' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=okhttpBean)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:643) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:130) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1422) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:882) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878) ~[spring-context-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) ~[spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) ~[spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) ~[spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
    at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:126) ~[spring-boot-test-2.2.6.RELEASE.jar:2.2.6.RELEASE]
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99) ~[spring-test-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124) ~[spring-test-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    ... 24 common frames omitted
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'okhttp3.OkHttpClient' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=okhttpBean)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1700) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1256) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1210) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    ... 42 common frames omitted

附: lucky 是我必须 okhttp 对象的变量名