Search in sources :

Example 86 with ConfigurableListableBeanFactory

use of org.springframework.beans.factory.config.ConfigurableListableBeanFactory in project grails-core by grails.

the class AbstractGrailsPluginManager method doRuntimeConfiguration.

/**
 * Base implementation that simply goes through the list of plugins and calls doWithRuntimeConfiguration on each
 * @param springConfig The RuntimeSpringConfiguration instance
 */
public void doRuntimeConfiguration(RuntimeSpringConfiguration springConfig) {
    ApplicationContext context = springConfig.getUnrefreshedApplicationContext();
    AutowireCapableBeanFactory autowireCapableBeanFactory = context.getAutowireCapableBeanFactory();
    if (autowireCapableBeanFactory instanceof ConfigurableListableBeanFactory) {
        ConfigurableListableBeanFactory beanFactory = (ConfigurableListableBeanFactory) autowireCapableBeanFactory;
        ConversionService existingConversionService = beanFactory.getConversionService();
        ConverterRegistry converterRegistry;
        if (existingConversionService == null) {
            GenericConversionService conversionService = new GenericConversionService();
            converterRegistry = conversionService;
            beanFactory.setConversionService(conversionService);
        } else {
            converterRegistry = (ConverterRegistry) existingConversionService;
        }
        converterRegistry.addConverter(new Converter<NavigableMap.NullSafeNavigator, Object>() {

            @Override
            public Object convert(NavigableMap.NullSafeNavigator source) {
                return null;
            }
        });
    }
    checkInitialised();
    for (GrailsPlugin plugin : pluginList) {
        if (plugin.supportsCurrentScopeAndEnvironment() && plugin.isEnabled(context.getEnvironment().getActiveProfiles())) {
            plugin.doWithRuntimeConfiguration(springConfig);
        }
    }
}
Also used : NavigableMap(org.grails.config.NavigableMap) AutowireCapableBeanFactory(org.springframework.beans.factory.config.AutowireCapableBeanFactory) ApplicationContext(org.springframework.context.ApplicationContext) ConverterRegistry(org.springframework.core.convert.converter.ConverterRegistry) GrailsPlugin(grails.plugins.GrailsPlugin) ConversionService(org.springframework.core.convert.ConversionService) GenericConversionService(org.springframework.core.convert.support.GenericConversionService) ConfigObject(groovy.util.ConfigObject) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) GenericConversionService(org.springframework.core.convert.support.GenericConversionService)

Example 87 with ConfigurableListableBeanFactory

use of org.springframework.beans.factory.config.ConfigurableListableBeanFactory in project spring-cloud-connectors by spring-cloud.

the class AbstractCloudServiceConnectorFactory method afterPropertiesSet.

@Override
public void afterPropertiesSet() throws Exception {
    ConfigurableListableBeanFactory beanFactory = (ConfigurableListableBeanFactory) getBeanFactory();
    if (cloud == null) {
        if (beanFactory.getBeansOfType(CloudFactory.class).isEmpty()) {
            beanFactory.registerSingleton(CLOUD_FACTORY_BEAN_NAME, new CloudFactory());
        }
        CloudFactory cloudFactory = beanFactory.getBeansOfType(CloudFactory.class).values().iterator().next();
        cloud = cloudFactory.getCloud();
    }
    if (!StringUtils.hasText(serviceId)) {
        List<? extends ServiceInfo> infos = cloud.getServiceInfos(serviceConnectorType);
        if (infos.size() != 1) {
            throw new CloudException("Expected 1 service matching " + serviceConnectorType.getName() + " type, but found " + infos.size());
        }
        serviceId = infos.get(0).getId();
    }
    super.afterPropertiesSet();
}
Also used : CloudFactory(org.springframework.cloud.CloudFactory) CloudException(org.springframework.cloud.CloudException) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)

Example 88 with ConfigurableListableBeanFactory

use of org.springframework.beans.factory.config.ConfigurableListableBeanFactory in project spring-cloud-connectors by spring-cloud.

the class CloudScanHelper method initializeCloud.

private void initializeCloud(BeanDefinitionRegistry registry) {
    if (cloud != null) {
        return;
    }
    ConfigurableListableBeanFactory beanFactory = (ConfigurableListableBeanFactory) registry;
    if (beanFactory.getBeansOfType(CloudFactory.class).isEmpty()) {
        beanFactory.registerSingleton(CLOUD_FACTORY_BEAN_NAME, new CloudFactory());
    }
    CloudFactory cloudFactory = beanFactory.getBeansOfType(CloudFactory.class).values().iterator().next();
    cloud = cloudFactory.getCloud();
}
Also used : CloudFactory(org.springframework.cloud.CloudFactory) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)

Example 89 with ConfigurableListableBeanFactory

use of org.springframework.beans.factory.config.ConfigurableListableBeanFactory in project spring-boot by spring-projects.

the class OnBeanCondition method getMatchingBeans.

protected final MatchResult getMatchingBeans(ConditionContext context, Spec<?> spec) {
    ClassLoader classLoader = context.getClassLoader();
    ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
    boolean considerHierarchy = spec.getStrategy() != SearchStrategy.CURRENT;
    Set<Class<?>> parameterizedContainers = spec.getParameterizedContainers();
    if (spec.getStrategy() == SearchStrategy.ANCESTORS) {
        BeanFactory parent = beanFactory.getParentBeanFactory();
        Assert.isInstanceOf(ConfigurableListableBeanFactory.class, parent, "Unable to use SearchStrategy.ANCESTORS");
        beanFactory = (ConfigurableListableBeanFactory) parent;
    }
    MatchResult result = new MatchResult();
    Set<String> beansIgnoredByType = getNamesOfBeansIgnoredByType(classLoader, beanFactory, considerHierarchy, spec.getIgnoredTypes(), parameterizedContainers);
    for (String type : spec.getTypes()) {
        Collection<String> typeMatches = getBeanNamesForType(classLoader, considerHierarchy, beanFactory, type, parameterizedContainers);
        Iterator<String> iterator = typeMatches.iterator();
        while (iterator.hasNext()) {
            String match = iterator.next();
            if (beansIgnoredByType.contains(match) || ScopedProxyUtils.isScopedTarget(match)) {
                iterator.remove();
            }
        }
        if (typeMatches.isEmpty()) {
            result.recordUnmatchedType(type);
        } else {
            result.recordMatchedType(type, typeMatches);
        }
    }
    for (String annotation : spec.getAnnotations()) {
        Set<String> annotationMatches = getBeanNamesForAnnotation(classLoader, beanFactory, annotation, considerHierarchy);
        annotationMatches.removeAll(beansIgnoredByType);
        if (annotationMatches.isEmpty()) {
            result.recordUnmatchedAnnotation(annotation);
        } else {
            result.recordMatchedAnnotation(annotation, annotationMatches);
        }
    }
    for (String beanName : spec.getNames()) {
        if (!beansIgnoredByType.contains(beanName) && containsBean(beanFactory, beanName, considerHierarchy)) {
            result.recordMatchedName(beanName);
        } else {
            result.recordUnmatchedName(beanName);
        }
    }
    return result;
}
Also used : HierarchicalBeanFactory(org.springframework.beans.factory.HierarchicalBeanFactory) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) BeanFactory(org.springframework.beans.factory.BeanFactory) ListableBeanFactory(org.springframework.beans.factory.ListableBeanFactory) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)

Example 90 with ConfigurableListableBeanFactory

use of org.springframework.beans.factory.config.ConfigurableListableBeanFactory in project spring-boot by spring-projects.

the class OnExpressionCondition method getMatchOutcome.

@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
    String expression = (String) metadata.getAnnotationAttributes(ConditionalOnExpression.class.getName()).get("value");
    expression = wrapIfNecessary(expression);
    ConditionMessage.Builder messageBuilder = ConditionMessage.forCondition(ConditionalOnExpression.class, "(" + expression + ")");
    expression = context.getEnvironment().resolvePlaceholders(expression);
    ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
    if (beanFactory != null) {
        boolean result = evaluateExpression(beanFactory, expression);
        return new ConditionOutcome(result, messageBuilder.resultedIn(result));
    }
    return ConditionOutcome.noMatch(messageBuilder.because("no BeanFactory available."));
}
Also used : ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)

Aggregations

ConfigurableListableBeanFactory (org.springframework.beans.factory.config.ConfigurableListableBeanFactory)104 Test (org.junit.Test)16 Test (org.junit.jupiter.api.Test)15 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)13 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)12 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)10 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)10 ApplicationContext (org.springframework.context.ApplicationContext)9 BeanDefinitionRegistry (org.springframework.beans.factory.support.BeanDefinitionRegistry)8 GenericBeanDefinition (org.springframework.beans.factory.support.GenericBeanDefinition)7 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)7 XmlWebApplicationContext (org.springframework.web.context.support.XmlWebApplicationContext)7 Map (java.util.Map)6 BeanFactory (org.springframework.beans.factory.BeanFactory)6 ListableBeanFactory (org.springframework.beans.factory.ListableBeanFactory)6 Collectors (java.util.stream.Collectors)5 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)5 HashMap (java.util.HashMap)4 BeanFactoryPostProcessor (org.springframework.beans.factory.config.BeanFactoryPostProcessor)4 SimpleApplicationEventMulticaster (org.springframework.context.event.SimpleApplicationEventMulticaster)4