Search in sources :

Example 6 with ConfigurableBeanFactory

use of org.springframework.beans.factory.config.ConfigurableBeanFactory in project spring-framework by spring-projects.

the class AbstractApplicationEventMulticaster method setBeanFactory.

@Override
public void setBeanFactory(BeanFactory beanFactory) {
    this.beanFactory = beanFactory;
    if (beanFactory instanceof ConfigurableBeanFactory) {
        ConfigurableBeanFactory cbf = (ConfigurableBeanFactory) beanFactory;
        if (this.beanClassLoader == null) {
            this.beanClassLoader = cbf.getBeanClassLoader();
        }
        this.retrievalMutex = cbf.getSingletonMutex();
    }
}
Also used : ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory)

Example 7 with ConfigurableBeanFactory

use of org.springframework.beans.factory.config.ConfigurableBeanFactory in project spring-framework by spring-projects.

the class JmsListenerAnnotationBeanPostProcessor method setBeanFactory.

/**
	 * Making a {@link BeanFactory} available is optional; if not set,
	 * {@link JmsListenerConfigurer} beans won't get autodetected and an
	 * {@link #setEndpointRegistry endpoint registry} has to be explicitly configured.
	 */
@Override
public void setBeanFactory(BeanFactory beanFactory) {
    this.beanFactory = beanFactory;
    if (beanFactory instanceof ConfigurableBeanFactory) {
        this.embeddedValueResolver = new EmbeddedValueResolver((ConfigurableBeanFactory) beanFactory);
    }
    this.registrar.setBeanFactory(beanFactory);
}
Also used : ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) EmbeddedValueResolver(org.springframework.beans.factory.config.EmbeddedValueResolver)

Example 8 with ConfigurableBeanFactory

use of org.springframework.beans.factory.config.ConfigurableBeanFactory in project spring-framework by spring-projects.

the class AbstractExpressionEvaluatingCondition method evaluateExpression.

private <A extends Annotation> boolean evaluateExpression(String expression, boolean loadContext, Class<A> annotationType, ExtensionContext extensionContext) {
    AnnotatedElement element = extensionContext.getElement().get();
    GenericApplicationContext gac = null;
    ApplicationContext applicationContext;
    if (loadContext) {
        applicationContext = SpringExtension.getApplicationContext(extensionContext);
    } else {
        gac = new GenericApplicationContext();
        gac.refresh();
        applicationContext = gac;
    }
    if (!(applicationContext instanceof ConfigurableApplicationContext)) {
        if (logger.isWarnEnabled()) {
            String contextType = (applicationContext != null ? applicationContext.getClass().getName() : "null");
            logger.warn(String.format("@%s(\"%s\") could not be evaluated on [%s] since the test " + "ApplicationContext [%s] is not a ConfigurableApplicationContext", annotationType.getSimpleName(), expression, element, contextType));
        }
        return false;
    }
    ConfigurableBeanFactory configurableBeanFactory = ((ConfigurableApplicationContext) applicationContext).getBeanFactory();
    BeanExpressionResolver expressionResolver = configurableBeanFactory.getBeanExpressionResolver();
    BeanExpressionContext beanExpressionContext = new BeanExpressionContext(configurableBeanFactory, null);
    Object result = expressionResolver.evaluate(configurableBeanFactory.resolveEmbeddedValue(expression), beanExpressionContext);
    if (gac != null) {
        gac.close();
    }
    if (result instanceof Boolean) {
        return ((Boolean) result).booleanValue();
    } else if (result instanceof String) {
        String str = ((String) result).trim().toLowerCase();
        if ("true".equals(str)) {
            return true;
        }
        Assert.state("false".equals(str), () -> String.format("@%s(\"%s\") on %s must evaluate to \"true\" or \"false\", not \"%s\"", annotationType.getSimpleName(), expression, element, result));
        return false;
    } else {
        String message = String.format("@%s(\"%s\") on %s must evaluate to a String or a Boolean, not %s", annotationType.getSimpleName(), expression, element, (result != null ? result.getClass().getName() : "null"));
        throw new IllegalStateException(message);
    }
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) ApplicationContext(org.springframework.context.ApplicationContext) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) BeanExpressionResolver(org.springframework.beans.factory.config.BeanExpressionResolver) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) AnnotatedElement(java.lang.reflect.AnnotatedElement) BeanExpressionContext(org.springframework.beans.factory.config.BeanExpressionContext)

Example 9 with ConfigurableBeanFactory

use of org.springframework.beans.factory.config.ConfigurableBeanFactory in project spring-framework by spring-projects.

the class AbstractJaxWsServiceExporter method publishEndpoints.

/**
	 * Publish all {@link javax.jws.WebService} annotated beans in the
	 * containing BeanFactory.
	 * @see #publishEndpoint
	 */
public void publishEndpoints() {
    Set<String> beanNames = new LinkedHashSet<>(this.beanFactory.getBeanDefinitionCount());
    beanNames.addAll(Arrays.asList(this.beanFactory.getBeanDefinitionNames()));
    if (this.beanFactory instanceof ConfigurableBeanFactory) {
        beanNames.addAll(Arrays.asList(((ConfigurableBeanFactory) this.beanFactory).getSingletonNames()));
    }
    for (String beanName : beanNames) {
        try {
            Class<?> type = this.beanFactory.getType(beanName);
            if (type != null && !type.isInterface()) {
                WebService wsAnnotation = type.getAnnotation(WebService.class);
                WebServiceProvider wsProviderAnnotation = type.getAnnotation(WebServiceProvider.class);
                if (wsAnnotation != null || wsProviderAnnotation != null) {
                    Endpoint endpoint = createEndpoint(this.beanFactory.getBean(beanName));
                    if (this.endpointProperties != null) {
                        endpoint.setProperties(this.endpointProperties);
                    }
                    if (this.executor != null) {
                        endpoint.setExecutor(this.executor);
                    }
                    if (wsAnnotation != null) {
                        publishEndpoint(endpoint, wsAnnotation);
                    } else {
                        publishEndpoint(endpoint, wsProviderAnnotation);
                    }
                    this.publishedEndpoints.add(endpoint);
                }
            }
        } catch (CannotLoadBeanClassException ex) {
        // ignore beans where the class is not resolvable
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) WebServiceProvider(javax.xml.ws.WebServiceProvider) Endpoint(javax.xml.ws.Endpoint) WebService(javax.jws.WebService) CannotLoadBeanClassException(org.springframework.beans.factory.CannotLoadBeanClassException)

Example 10 with ConfigurableBeanFactory

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

the class GenericBeanFactoryAccessor method findAnnotationOnBean.

/**
     * Find a {@link Annotation} of <code>annotationType</code> on the specified
     * bean, traversing its interfaces and super classes if no annotation can be
     * found on the given class itself, as well as checking its raw bean class
     * if not found on the exposed bean reference (e.g. in case of a proxy).
     * @param beanName the name of the bean to look for annotations on
     * @param annotationType the annotation class to look for
     * @return the annotation of the given type found, or <code>null</code>
     * @see org.springframework.core.annotation.AnnotationUtils#findAnnotation(Class, Class)
     */
public <A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType) {
    Class<?> handlerType = beanFactory.getType(beanName);
    A ann = AnnotationUtils.findAnnotation(handlerType, annotationType);
    if (ann == null && beanFactory instanceof ConfigurableBeanFactory && beanFactory.containsBeanDefinition(beanName)) {
        ConfigurableBeanFactory cbf = (ConfigurableBeanFactory) beanFactory;
        BeanDefinition bd = cbf.getMergedBeanDefinition(beanName);
        if (bd instanceof AbstractBeanDefinition) {
            AbstractBeanDefinition abd = (AbstractBeanDefinition) bd;
            if (abd.hasBeanClass()) {
                Class<?> beanClass = abd.getBeanClass();
                ann = AnnotationUtils.findAnnotation(beanClass, annotationType);
            }
        }
    }
    return ann;
}
Also used : ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition)

Aggregations

ConfigurableBeanFactory (org.springframework.beans.factory.config.ConfigurableBeanFactory)18 ArrayList (java.util.ArrayList)3 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)3 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)3 PostConstruct (javax.annotation.PostConstruct)2 ProxyFactory (org.springframework.aop.framework.ProxyFactory)2 BeanFactory (org.springframework.beans.factory.BeanFactory)2 EmbeddedValueResolver (org.springframework.beans.factory.config.EmbeddedValueResolver)2 AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)2 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)2 HandlerMethodArgumentResolver (org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver)2 AnnotatedElement (java.lang.reflect.AnnotatedElement)1 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 Map (java.util.Map)1 WebService (javax.jws.WebService)1 Endpoint (javax.xml.ws.Endpoint)1 WebServiceProvider (javax.xml.ws.WebServiceProvider)1 HystrixConfigurationCommon (org.apache.camel.model.HystrixConfigurationCommon)1