List of usage examples for org.springframework.boot.autoconfigure.condition SearchStrategy ALL
SearchStrategy ALL
To view the source code for org.springframework.boot.autoconfigure.condition SearchStrategy ALL.
Click Source Link
From source file:org.springframework.boot.autoconfigure.condition.AbstractOnBeanCondition.java
protected boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata, List<String> beanClasses, List<String> beanNames) throws LinkageError { String checking = ConditionLogUtils.getPrefix(this.logger, metadata); SearchStrategy search = (SearchStrategy) metadata.getAnnotationAttributes(annotationClass().getName()) .get("search"); search = search == null ? SearchStrategy.ALL : search; boolean considerHierarchy = search == SearchStrategy.ALL; boolean parentOnly = search == SearchStrategy.PARENTS; List<String> beanClassesFound = new ArrayList<String>(); List<String> beanNamesFound = new ArrayList<String>(); // eagerInit set to false to prevent early instantiation (some // factory beans will not be able to determine their object type at this // stage, so those are not eligible for matching this condition) ConfigurableListableBeanFactory beanFactory = context.getBeanFactory(); if (parentOnly) { BeanFactory parent = beanFactory.getParentBeanFactory(); if (!(parent instanceof ConfigurableListableBeanFactory)) { throw new IllegalStateException( "Cannot use parentOnly if parent is not ConfigurableListableBeanFactory"); }/*w w w .j ava 2 s. c o m*/ beanFactory = (ConfigurableListableBeanFactory) parent; } for (String beanClass : beanClasses) { try { Class<?> type = ClassUtils.forName(beanClass, context.getClassLoader()); String[] beans = (considerHierarchy ? BeanFactoryUtils.beanNamesForTypeIncludingAncestors(beanFactory, type, false, false) : beanFactory.getBeanNamesForType(type, false, false)); if (beans.length != 0) { beanClassesFound.add(beanClass); } } catch (ClassNotFoundException ex) { // swallow exception and continue } } for (String beanName : beanNames) { if (considerHierarchy ? beanFactory.containsBean(beanName) : beanFactory.containsLocalBean(beanName)) { beanNamesFound.add(beanName); } } boolean result = evaluate(beanClassesFound, beanNamesFound); if (this.logger.isDebugEnabled()) { logFoundResults(checking, "class", beanClasses, beanClassesFound); logFoundResults(checking, "name", beanNames, beanClassesFound); this.logger.debug(checking + "Match result is: " + result); } return result; }
From source file:org.springframework.boot.autoconfigure.condition.OnBeanCondition.java
private List<String> getMatchingBeans(ConditionContext context, BeanSearchSpec beans) { ConfigurableListableBeanFactory beanFactory = context.getBeanFactory(); if (beans.getStrategy() == SearchStrategy.PARENTS) { BeanFactory parent = beanFactory.getParentBeanFactory(); Assert.isInstanceOf(ConfigurableListableBeanFactory.class, parent, "Unable to use SearchStrategy.PARENTS"); beanFactory = (ConfigurableListableBeanFactory) parent; }/*from w ww. j a v a 2 s . c om*/ if (beanFactory == null) { return Collections.emptyList(); } List<String> beanNames = new ArrayList<String>(); boolean considerHierarchy = beans.getStrategy() == SearchStrategy.ALL; for (String type : beans.getTypes()) { beanNames.addAll(getBeanNamesForType(beanFactory, type, context.getClassLoader(), considerHierarchy)); } for (String ignoredType : beans.getIgnoredTypes()) { beanNames.removeAll( getBeanNamesForType(beanFactory, ignoredType, context.getClassLoader(), considerHierarchy)); } for (String annotation : beans.getAnnotations()) { beanNames.addAll(Arrays.asList(getBeanNamesForAnnotation(beanFactory, annotation, context.getClassLoader(), considerHierarchy))); } for (String beanName : beans.getNames()) { if (containsBean(beanFactory, beanName, considerHierarchy)) { beanNames.add(beanName); } } return beanNames; }