List of usage examples for org.springframework.context.annotation ConditionContext getBeanFactory
@Nullable ConfigurableListableBeanFactory getBeanFactory();
From source file:lodsve.core.condition.SpringBootCondition.java
private void recordEvaluation(ConditionContext context, String classOrMethodName, ConditionOutcome outcome) { if (context.getBeanFactory() != null) { ConditionEvaluationReport.get(context.getBeanFactory()).recordConditionEvaluation(classOrMethodName, this, outcome); }// w w w.j av a 2 s .co m }
From source file:lodsve.core.condition.OnWebApplicationCondition.java
private ConditionOutcome isWebApplication(ConditionContext context, AnnotatedTypeMetadata metadata) { if (!ClassUtils.isPresent(WEB_CONTEXT_CLASS, context.getClassLoader())) { return ConditionOutcome.noMatch("web application classes not found"); }//from ww w . ja va 2 s .co m if (context.getBeanFactory() != null) { String[] scopes = context.getBeanFactory().getRegisteredScopeNames(); if (ObjectUtils.containsElement(scopes, "session")) { return ConditionOutcome.match("found web application 'session' scope"); } } if (context.getEnvironment() instanceof StandardServletEnvironment) { return ConditionOutcome.match("found web application StandardServletEnvironment"); } if (context.getResourceLoader() instanceof WebApplicationContext) { return ConditionOutcome.match("found web application WebApplicationContext"); } return ConditionOutcome.noMatch("not a web application"); }
From source file:at.porscheinformatik.common.spring.web.extended.annotation.DefaultBeanCondition.java
@Override public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { Map<String, Object> annotationAttributes = metadata.getAnnotationAttributes(DefaultBean.class.getName()); Class<?>[] classes = (Class<?>[]) annotationAttributes.get("value"); ConfigurableListableBeanFactory factory = context.getBeanFactory(); for (Class<?> c : classes) { String[] beanNamesForType = factory.getBeanNamesForType(c, false, false); if (beanNamesForType != null) { for (String beanName : beanNamesForType) { if (factory.containsBean(beanName)) { return false; }/* ww w. jav a 2s. co m*/ } } } return true; }
From source file:lodsve.core.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 w w.j a v a2 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; }
From source file:lodsve.core.condition.OnExpressionCondition.java
@Override public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { String expression = (String) metadata.getAnnotationAttributes(ConditionalOnExpression.class.getName()) .get("value"); String rawExpression = expression; if (!expression.startsWith("#{")) { // For convenience allow user to provide bare expression with no #{} wrapper expression = "#{" + expression + "}"; }//w ww . j av a2 s . c o m // Explicitly allow environment placeholders inside the expression expression = context.getEnvironment().resolvePlaceholders(expression); ConfigurableListableBeanFactory beanFactory = context.getBeanFactory(); BeanExpressionResolver resolver = (beanFactory != null) ? beanFactory.getBeanExpressionResolver() : null; BeanExpressionContext expressionContext = (beanFactory != null) ? new BeanExpressionContext(beanFactory, null) : null; if (resolver == null) { resolver = new StandardBeanExpressionResolver(); } boolean result = (Boolean) resolver.evaluate(expression, expressionContext); StringBuilder message = new StringBuilder("SpEL expression"); if (metadata instanceof ClassMetadata) { message.append(" on " + ((ClassMetadata) metadata).getClassName()); } message.append(": " + rawExpression); return new ConditionOutcome(result, message.toString()); }
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 ww. j av a 2 s .c om 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
@Override public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { StringBuffer matchMessage = new StringBuffer(); if (metadata.isAnnotated(ConditionalOnBean.class.getName())) { BeanSearchSpec spec = new BeanSearchSpec(context, metadata, ConditionalOnBean.class); List<String> matching = getMatchingBeans(context, spec); if (matching.isEmpty()) { return ConditionOutcome.noMatch("@ConditionalOnBean " + spec + " found no beans"); }/* ww w . ja va2 s .c o m*/ matchMessage.append("@ConditionalOnBean " + spec + " found the following " + matching); } if (metadata.isAnnotated(ConditionalOnSingleCandidate.class.getName())) { BeanSearchSpec spec = new SingleCandidateBeanSearchSpec(context, metadata, ConditionalOnSingleCandidate.class); List<String> matching = getMatchingBeans(context, spec); if (matching.isEmpty()) { return ConditionOutcome.noMatch("@ConditionalOnSingleCandidate " + spec + " found no beans"); } else if (!hasSingleAutowireCandidate(context.getBeanFactory(), matching)) { return ConditionOutcome.noMatch("@ConditionalOnSingleCandidate " + spec + " found no primary candidate amongst the" + " following " + matching); } matchMessage.append("@ConditionalOnSingleCandidate " + spec + " found " + "a primary candidate amongst the following " + matching); } if (metadata.isAnnotated(ConditionalOnMissingBean.class.getName())) { BeanSearchSpec spec = new BeanSearchSpec(context, metadata, ConditionalOnMissingBean.class); List<String> matching = getMatchingBeans(context, spec); if (!matching.isEmpty()) { return ConditionOutcome .noMatch("@ConditionalOnMissingBean " + spec + " found the following " + matching); } matchMessage.append(matchMessage.length() == 0 ? "" : " "); matchMessage.append("@ConditionalOnMissingBean " + spec + " found no beans"); } return ConditionOutcome.match(matchMessage.toString()); }
From source file:org.springframework.boot.autoconfigure.condition.OnExpressionCondition.java
@Override public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { String checking = ConditionLogUtils.getPrefix(logger, metadata); String value = (String) metadata.getAnnotationAttributes(ConditionalOnExpression.class.getName()) .get("value"); if (!value.startsWith("#{")) { // For convenience allow user to provide bare expression with no #{} wrapper value = "#{" + value + "}"; }/*from w w w. j ava 2 s . com*/ if (logger.isDebugEnabled()) { StringBuilder builder = new StringBuilder(checking).append("Evaluating expression"); if (metadata instanceof ClassMetadata) { builder.append(" on " + ((ClassMetadata) metadata).getClassName()); } builder.append(": " + value); logger.debug(builder.toString()); } // Explicitly allow environment placeholders inside the expression value = context.getEnvironment().resolvePlaceholders(value); ConfigurableListableBeanFactory beanFactory = context.getBeanFactory(); BeanExpressionResolver resolver = beanFactory.getBeanExpressionResolver(); BeanExpressionContext expressionContext = (beanFactory != null) ? new BeanExpressionContext(beanFactory, null) : null; if (resolver == null) { resolver = new StandardBeanExpressionResolver(); } boolean result = (Boolean) resolver.evaluate(value, expressionContext); if (logger.isDebugEnabled()) { logger.debug(checking + "Finished matching and result is matches=" + result); } return result; }
From source file:org.springframework.boot.autoconfigure.condition.OnNotWebApplicationCondition.java
@Override public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { String checking = ConditionLogUtils.getPrefix(logger, metadata); if (!ClassUtils.isPresent("org.springframework.web.context.support.GenericWebApplicationContext", null)) { if (logger.isDebugEnabled()) { logger.debug(checking + "Web application classes not found"); }/*from ww w .j a v a 2s . co m*/ return true; } boolean result = !StringUtils .arrayToCommaDelimitedString(context.getBeanFactory().getRegisteredScopeNames()) .contains("session"); if (logger.isDebugEnabled()) { logger.debug(checking + "Web application context found: " + !result); } return result; }
From source file:org.springframework.boot.autoconfigure.condition.OnWebApplicationCondition.java
@Override public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { String checking = ConditionLogUtils.getPrefix(logger, metadata); if (!ClassUtils.isPresent("org.springframework.web.context.support.GenericWebApplicationContext", null)) { if (logger.isDebugEnabled()) { logger.debug(checking + "Web application classes not found"); }//from ww w . jav a2s. co m return false; } boolean result = StringUtils.arrayToCommaDelimitedString(context.getBeanFactory().getRegisteredScopeNames()) .contains("session") || context.getEnvironment() instanceof StandardServletEnvironment; if (logger.isDebugEnabled()) { logger.debug(checking + "Web application context found: " + result); } return result; }