private boolean matchesBean(String advisedBeanName) { return BeanFactoryAnnotationUtils.isQualifierMatch( this.expressionPattern::matches, advisedBeanName, beanFactory); } }
/** * Retrieve all bean of type {@code T} from the given {@code BeanFactory} declaring a * qualifier (e.g. via {@code <qualifier>} or {@code @Qualifier}) matching the given * qualifier, or having a bean name matching the given qualifier. * @param beanFactory the factory to get the target beans from (also searching ancestors) * @param beanType the type of beans to retrieve * @param qualifier the qualifier for selecting among all type matches * @return the matching beans of type {@code T} * @throws BeansException if any of the matching beans could not be created * @since 5.1.1 * @see BeanFactoryUtils#beansOfTypeIncludingAncestors(ListableBeanFactory, Class) */ public static <T> Map<String, T> qualifiedBeansOfType( ListableBeanFactory beanFactory, Class<T> beanType, String qualifier) throws BeansException { String[] candidateBeans = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(beanFactory, beanType); Map<String, T> result = new LinkedHashMap<>(4); for (String beanName : candidateBeans) { if (isQualifierMatch(qualifier::equals, beanName, beanFactory)) { result.put(beanName, beanFactory.getBean(beanName, beanType)); } } return result; }
/** * Retrieve all bean of type {@code T} from the given {@code BeanFactory} declaring a * qualifier (e.g. via {@code <qualifier>} or {@code @Qualifier}) matching the given * qualifier, or having a bean name matching the given qualifier. * @param beanFactory the factory to get the target beans from (also searching ancestors) * @param beanType the type of beans to retrieve * @param qualifier the qualifier for selecting among all type matches * @return the matching beans of type {@code T} * @throws BeansException if any of the matching beans could not be created * @since 5.1.1 * @see BeanFactoryUtils#beansOfTypeIncludingAncestors(ListableBeanFactory, Class) */ public static <T> Map<String, T> qualifiedBeansOfType( ListableBeanFactory beanFactory, Class<T> beanType, String qualifier) throws BeansException { String[] candidateBeans = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(beanFactory, beanType); Map<String, T> result = new LinkedHashMap<>(4); for (String beanName : candidateBeans) { if (isQualifierMatch(qualifier::equals, beanName, beanFactory)) { result.put(beanName, beanFactory.getBean(beanName, beanType)); } } return result; }
String matchingBean = null; for (String beanName : candidateBeans) { if (isQualifierMatch(qualifier::equals, beanName, bf)) { if (matchingBean != null) { throw new NoUniqueBeanDefinitionException(beanType, matchingBean, beanName);
@Test public void testCustomWithEarlyResolution() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(CustomConfig.class, CustomPojo.class); ctx.refresh(); assertFalse(ctx.getBeanFactory().containsSingleton("testBean1")); assertFalse(ctx.getBeanFactory().containsSingleton("testBean2")); ctx.getBean("testBean2"); assertTrue(BeanFactoryAnnotationUtils.isQualifierMatch(value -> value.equals("boring"), "testBean2", ctx.getDefaultListableBeanFactory())); CustomPojo pojo = ctx.getBean(CustomPojo.class); assertThat(pojo.testBean.getName(), equalTo("interesting")); }
String matchingBean = null; for (String beanName : candidateBeans) { if (isQualifierMatch(qualifier::equals, beanName, bf)) { if (matchingBean != null) { throw new NoUniqueBeanDefinitionException(beanType, matchingBean, beanName);
@Test public void testCustomWithLazyResolution() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(CustomConfig.class, CustomPojo.class); assertFalse(ctx.getBeanFactory().containsSingleton("testBean1")); assertFalse(ctx.getBeanFactory().containsSingleton("testBean2")); assertTrue(BeanFactoryAnnotationUtils.isQualifierMatch(value -> value.equals("boring"), "testBean2", ctx.getDefaultListableBeanFactory())); CustomPojo pojo = ctx.getBean(CustomPojo.class); assertThat(pojo.testBean.getName(), equalTo("interesting")); TestBean testBean2 = BeanFactoryAnnotationUtils.qualifiedBeanOfType( ctx.getDefaultListableBeanFactory(), TestBean.class, "boring"); assertThat(testBean2.getName(), equalTo("boring")); }
T matchingBean = null; for (String beanName : candidateBeans.keySet()) { if (isQualifierMatch(qualifier, beanName, bf)) { if (matchingBean != null) { throw new IllegalStateException("No unique " + beanType.getSimpleName() +
private boolean matchesBean(String advisedBeanName) { return BeanFactoryAnnotationUtils.isQualifierMatch( this.expressionPattern::matches, advisedBeanName, beanFactory); } }