Java Code Examples for org.springframework.context.annotation.ConditionContext#getBeanFactory()

The following examples show how to use org.springframework.context.annotation.ConditionContext#getBeanFactory() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CentralDogmaClientAutoConfiguration.java    From centraldogma with Apache License 2.0 6 votes vote down vote up
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
    final ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
    if (beanFactory == null) {
        return true;
    }

    final String[] beanNames =
            BeanFactoryUtils.beanNamesForTypeIncludingAncestors(beanFactory, ClientFactory.class);

    for (String beanName : beanNames) {
        if (hasQualifier(beanFactory, beanName)) {
            return false;
        }
    }

    return true;
}
 
Example 2
Source File: RemoteConfigSourceConfigured.java    From kork with Apache License 2.0 6 votes vote down vote up
@Override
public boolean matches(ConditionContext context, @NotNull AnnotatedTypeMetadata metadata) {
  ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
  if (beanFactory != null) {
    Environment environment = context.getEnvironment();
    String remoteRepoTypes =
        environment.getProperty(
            "spring.cloud.config.remote-repo-types", DEFAULT_REMOTE_REPO_TYPES);

    for (String remoteRepoType : StringUtils.split(remoteRepoTypes, ',')) {
      if (beanFactory.containsBean(remoteRepoType + "EnvironmentRepository")
          || beanFactory.containsBean(remoteRepoType + "-env-repo0")) {
        return true;
      }
    }
  }
  return false;
}
 
Example 3
Source File: BeanTypeNotPresentCondition.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
	ListableBeanFactory factory = context.getBeanFactory();
	String[] names = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(factory, this.beanType, false, false);
	if (ObjectUtils.isEmpty(names)) {
		logger.debug("No bean of type [" + this.beanType + "]. Conditional configuration applies.");
		return true;
	}
	else {
		logger.debug("Found bean of type [" + this.beanType + "]. Conditional configuration does not apply.");
		return false;
	}
}
 
Example 4
Source File: FunctionDetectorCondition.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
	if (context != null &&  context.getBeanFactory() != null) {

		String[] functionTypes = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(context.getBeanFactory(), Function.class, true, false);
		String[] consumerTypes = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(context.getBeanFactory(), Consumer.class, true, false);
		String[] biFunctionTypes = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(context.getBeanFactory(), BiFunction.class, true, false);
		String[] biConsumerTypes = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(context.getBeanFactory(), BiConsumer.class, true, false);

		List<String> functionComponents = new ArrayList<>();

		functionComponents.addAll(Arrays.asList(functionTypes));
		functionComponents.addAll(Arrays.asList(consumerTypes));
		functionComponents.addAll(Arrays.asList(biFunctionTypes));
		functionComponents.addAll(Arrays.asList(biConsumerTypes));

		List<String> kafkaStreamsFunctions = pruneFunctionBeansForKafkaStreams(functionComponents, context);
		if (!CollectionUtils.isEmpty(kafkaStreamsFunctions)) {
			return ConditionOutcome.match("Matched. Function/BiFunction/Consumer beans found");
		}
		else {
			return ConditionOutcome.noMatch("No match. No Function/BiFunction/Consumer beans found");
		}
	}
	return ConditionOutcome.noMatch("No match. No Function/BiFunction/Consumer beans found");
}
 
Example 5
Source File: OnSearchPathLocatorPresent.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context,
		AnnotatedTypeMetadata metadata) {
	ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
	List<String> types = CompositeUtils
			.getCompositeTypeList(context.getEnvironment());

	// get EnvironmentRepository types from registered factories
	List<Class<? extends EnvironmentRepository>> repositoryTypes = new ArrayList<>();
	for (String type : types) {
		String factoryName = CompositeUtils.getFactoryName(type, beanFactory);
		Type[] actualTypeArguments = CompositeUtils
				.getEnvironmentRepositoryFactoryTypeParams(beanFactory, factoryName);
		Class<? extends EnvironmentRepository> repositoryType;
		repositoryType = (Class<? extends EnvironmentRepository>) actualTypeArguments[0];
		repositoryTypes.add(repositoryType);
	}

	boolean required = metadata
			.isAnnotated(ConditionalOnSearchPathLocator.class.getName());
	boolean foundSearchPathLocator = repositoryTypes.stream()
			.anyMatch(SearchPathLocator.class::isAssignableFrom);
	if (required && !foundSearchPathLocator) {
		return ConditionOutcome.noMatch(
				ConditionMessage.forCondition(ConditionalOnSearchPathLocator.class)
						.notAvailable(SearchPathLocator.class.getTypeName()));
	}
	if (!required && foundSearchPathLocator) {
		return ConditionOutcome.noMatch(ConditionMessage
				.forCondition(ConditionalOnMissingSearchPathLocator.class)
				.available(SearchPathLocator.class.getTypeName()));
	}
	return ConditionOutcome.match();
}