Search in sources :

Example 11 with ConfigurableBeanFactory

use of org.springframework.beans.factory.config.ConfigurableBeanFactory in project chassis by Kixeye.

the class LoggingConfiguration method initialize.

@PostConstruct
public void initialize() {
    AbstractConfiguration config = ConfigurationManager.getConfigInstance();
    if (config.containsKey(LOGBACK_CONFIG_NAME)) {
        System.out.println("Loading logging config.");
        reloadLogging(config.getString(LOGBACK_CONFIG_NAME));
    }
    config.addConfigurationListener(new ConfigurationListener() {

        @Override
        public synchronized void configurationChanged(ConfigurationEvent event) {
            if ((event.getType() == AbstractConfiguration.EVENT_ADD_PROPERTY || event.getType() == AbstractConfiguration.EVENT_SET_PROPERTY) && StringUtils.equalsIgnoreCase(LOGBACK_CONFIG_NAME, event.getPropertyName()) && event.getPropertyValue() != null && !event.isBeforeUpdate()) {
                System.out.println("Reloading logging config.");
                reloadLogging((String) event.getPropertyValue());
            }
        }
    });
    ConfigurationListener flumeConfigListener = new ConfigurationListener() {

        private FlumeLoggerLoader loggerLoader = null;

        public synchronized void configurationChanged(ConfigurationEvent event) {
            if (!(event.getType() == AbstractConfiguration.EVENT_SET_PROPERTY || event.getType() == AbstractConfiguration.EVENT_ADD_PROPERTY || event.getType() == AbstractConfiguration.EVENT_CLEAR_PROPERTY)) {
                return;
            }
            if (FlumeLoggerLoader.FLUME_LOGGER_ENABLED_PROPERTY.equals(event.getPropertyName())) {
                if ("true".equals(event.getPropertyValue())) {
                    if (loggerLoader == null) {
                        // construct the bean
                        loggerLoader = (FlumeLoggerLoader) applicationContext.getBean(FlumeLoggerLoader.PROTOTYPE_BEAN_NAME, "chassis");
                    }
                // else we already have one so we're cool
                } else {
                    if (loggerLoader != null) {
                        // delete the bean
                        ConfigurableBeanFactory beanFactory = (ConfigurableBeanFactory) applicationContext.getParentBeanFactory();
                        beanFactory.destroyBean(FlumeLoggerLoader.PROTOTYPE_BEAN_NAME, loggerLoader);
                        loggerLoader = null;
                    }
                // else we don't have any so we're cool
                }
            } else if (FlumeLoggerLoader.RELOAD_PROPERTIES.contains(event.getPropertyValue())) {
                // only reload if we're already running - otherwise ignore
                if (loggerLoader != null) {
                    // delete the bean
                    ConfigurableBeanFactory beanFactory = (ConfigurableBeanFactory) applicationContext.getParentBeanFactory();
                    beanFactory.destroyBean(FlumeLoggerLoader.PROTOTYPE_BEAN_NAME, loggerLoader);
                    loggerLoader = null;
                    // construct the bean
                    loggerLoader = (FlumeLoggerLoader) applicationContext.getBean(FlumeLoggerLoader.PROTOTYPE_BEAN_NAME, "chassis");
                }
            // else we don't have any so we're cool
            }
        }
    };
    config.addConfigurationListener(flumeConfigListener);
    flumeConfigListener.configurationChanged(new ConfigurationEvent(this, AbstractConfiguration.EVENT_SET_PROPERTY, FlumeLoggerLoader.FLUME_LOGGER_ENABLED_PROPERTY, config.getProperty(FlumeLoggerLoader.FLUME_LOGGER_ENABLED_PROPERTY), false));
}
Also used : AbstractConfiguration(org.apache.commons.configuration.AbstractConfiguration) ConfigurationListener(org.apache.commons.configuration.event.ConfigurationListener) ConfigurationEvent(org.apache.commons.configuration.event.ConfigurationEvent) ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) PostConstruct(javax.annotation.PostConstruct)

Example 12 with ConfigurableBeanFactory

use of org.springframework.beans.factory.config.ConfigurableBeanFactory in project camel by apache.

the class HystrixAutoConfiguration method addHystrixConfigurations.

@PostConstruct
public void addHystrixConfigurations() {
    if (!(beanFactory instanceof ConfigurableBeanFactory)) {
        LOGGER.warn("BeanFactory is not of type ConfigurableBeanFactory");
        return;
    }
    final ConfigurableBeanFactory factory = (ConfigurableBeanFactory) beanFactory;
    final Map<String, Object> properties = new HashMap<>();
    for (Map.Entry<String, HystrixConfigurationCommon> entry : config.getConfigurations().entrySet()) {
        // clear the properties map for reuse
        properties.clear();
        // extract properties
        IntrospectionSupport.getProperties(entry.getValue(), properties, null, false);
        try {
            HystrixConfigurationDefinition definition = new HystrixConfigurationDefinition();
            IntrospectionSupport.setProperties(camelContext, camelContext.getTypeConverter(), definition, properties);
            // Registry the definition
            factory.registerSingleton(entry.getKey(), definition);
        } catch (Exception e) {
            throw new BeanCreationException(entry.getKey(), e);
        }
    }
}
Also used : ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) BeanCreationException(org.springframework.beans.factory.BeanCreationException) HashMap(java.util.HashMap) HystrixConfigurationCommon(org.apache.camel.model.HystrixConfigurationCommon) HashMap(java.util.HashMap) Map(java.util.Map) HystrixConfigurationDefinition(org.apache.camel.model.HystrixConfigurationDefinition) BeanCreationException(org.springframework.beans.factory.BeanCreationException) BeansException(org.springframework.beans.BeansException) PostConstruct(javax.annotation.PostConstruct)

Example 13 with ConfigurableBeanFactory

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

the class DefaultScopedObjectTests method testBadTargetBeanName.

private static void testBadTargetBeanName(final String badTargetBeanName) {
    ConfigurableBeanFactory factory = mock(ConfigurableBeanFactory.class);
    new DefaultScopedObject(factory, badTargetBeanName);
}
Also used : ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory)

Example 14 with ConfigurableBeanFactory

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

the class BeanFactoryAnnotationUtils method isQualifierMatch.

/**
	 * Check whether the named bean declares a qualifier of the given name.
	 * @param qualifier the qualifier to match
	 * @param beanName the name of the candidate bean
	 * @param beanFactory the {@code BeanFactory} from which to retrieve the named bean
	 * @return {@code true} if either the bean definition (in the XML case)
	 * or the bean's factory method (in the {@code @Bean} case) defines a matching
	 * qualifier value (through {@code <qualifier>} or {@code @Qualifier})
	 * @since 5.0
	 */
public static boolean isQualifierMatch(Predicate<String> qualifier, String beanName, BeanFactory beanFactory) {
    // Try quick bean name or alias match first...
    if (qualifier.test(beanName)) {
        return true;
    }
    if (beanFactory != null) {
        for (String alias : beanFactory.getAliases(beanName)) {
            if (qualifier.test(alias)) {
                return true;
            }
        }
        try {
            if (beanFactory instanceof ConfigurableBeanFactory) {
                BeanDefinition bd = ((ConfigurableBeanFactory) beanFactory).getMergedBeanDefinition(beanName);
                // Explicit qualifier metadata on bean definition? (typically in XML definition)
                if (bd instanceof AbstractBeanDefinition) {
                    AbstractBeanDefinition abd = (AbstractBeanDefinition) bd;
                    AutowireCandidateQualifier candidate = abd.getQualifier(Qualifier.class.getName());
                    if (candidate != null) {
                        Object value = candidate.getAttribute(AutowireCandidateQualifier.VALUE_KEY);
                        if (value != null && qualifier.test(value.toString())) {
                            return true;
                        }
                    }
                }
                // Corresponding qualifier on factory method? (typically in configuration class)
                if (bd instanceof RootBeanDefinition) {
                    Method factoryMethod = ((RootBeanDefinition) bd).getResolvedFactoryMethod();
                    if (factoryMethod != null) {
                        Qualifier targetAnnotation = AnnotationUtils.getAnnotation(factoryMethod, Qualifier.class);
                        if (targetAnnotation != null) {
                            return qualifier.test(targetAnnotation.value());
                        }
                    }
                }
            }
            // Corresponding qualifier on bean implementation class? (for custom user types)
            Class<?> beanType = beanFactory.getType(beanName);
            if (beanType != null) {
                Qualifier targetAnnotation = AnnotationUtils.getAnnotation(beanType, Qualifier.class);
                if (targetAnnotation != null) {
                    return qualifier.test(targetAnnotation.value());
                }
            }
        } catch (NoSuchBeanDefinitionException ex) {
        // Ignore - can't compare qualifiers for a manually registered singleton object
        }
    }
    return false;
}
Also used : ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) AutowireCandidateQualifier(org.springframework.beans.factory.support.AutowireCandidateQualifier) Method(java.lang.reflect.Method) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) AutowireCandidateQualifier(org.springframework.beans.factory.support.AutowireCandidateQualifier)

Example 15 with ConfigurableBeanFactory

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

the class AbstractBeanFactory method getMergedBeanDefinition.

/**
	 * Return a RootBeanDefinition for the given bean, by merging with the
	 * parent if the given bean's definition is a child bean definition.
	 * @param beanName the name of the bean definition
	 * @param bd the original bean definition (Root/ChildBeanDefinition)
	 * @param containingBd the containing bean definition in case of inner bean,
	 * or {@code null} in case of a top-level bean
	 * @return a (potentially merged) RootBeanDefinition for the given bean
	 * @throws BeanDefinitionStoreException in case of an invalid bean definition
	 */
protected RootBeanDefinition getMergedBeanDefinition(String beanName, BeanDefinition bd, BeanDefinition containingBd) throws BeanDefinitionStoreException {
    synchronized (this.mergedBeanDefinitions) {
        RootBeanDefinition mbd = null;
        // Check with full lock now in order to enforce the same merged instance.
        if (containingBd == null) {
            mbd = this.mergedBeanDefinitions.get(beanName);
        }
        if (mbd == null) {
            if (bd.getParentName() == null) {
                // Use copy of given root bean definition.
                if (bd instanceof RootBeanDefinition) {
                    mbd = ((RootBeanDefinition) bd).cloneBeanDefinition();
                } else {
                    mbd = new RootBeanDefinition(bd);
                }
            } else {
                // Child bean definition: needs to be merged with parent.
                BeanDefinition pbd;
                try {
                    String parentBeanName = transformedBeanName(bd.getParentName());
                    if (!beanName.equals(parentBeanName)) {
                        pbd = getMergedBeanDefinition(parentBeanName);
                    } else {
                        BeanFactory parent = getParentBeanFactory();
                        if (parent instanceof ConfigurableBeanFactory) {
                            pbd = ((ConfigurableBeanFactory) parent).getMergedBeanDefinition(parentBeanName);
                        } else {
                            throw new NoSuchBeanDefinitionException(parentBeanName, "Parent name '" + parentBeanName + "' is equal to bean name '" + beanName + "': cannot be resolved without an AbstractBeanFactory parent");
                        }
                    }
                } catch (NoSuchBeanDefinitionException ex) {
                    throw new BeanDefinitionStoreException(bd.getResourceDescription(), beanName, "Could not resolve parent bean definition '" + bd.getParentName() + "'", ex);
                }
                // Deep copy with overridden values.
                mbd = new RootBeanDefinition(pbd);
                mbd.overrideFrom(bd);
            }
            // Set default singleton scope, if not configured before.
            if (!StringUtils.hasLength(mbd.getScope())) {
                mbd.setScope(RootBeanDefinition.SCOPE_SINGLETON);
            }
            // definition will not have inherited the merged outer bean's singleton status.
            if (containingBd != null && !containingBd.isSingleton() && mbd.isSingleton()) {
                mbd.setScope(containingBd.getScope());
            }
            // instance of the bean, or at least have already created an instance before.
            if (containingBd == null && isCacheBeanMetadata()) {
                this.mergedBeanDefinitions.put(beanName, mbd);
            }
        }
        return mbd;
    }
}
Also used : ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) BeanDefinitionStoreException(org.springframework.beans.factory.BeanDefinitionStoreException) BeanFactory(org.springframework.beans.factory.BeanFactory) ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException)

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