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));
}
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);
}
}
}
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);
}
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;
}
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;
}
}
Aggregations