Example usage for org.springframework.beans.factory.config ConfigurableListableBeanFactory getBeanDefinition

List of usage examples for org.springframework.beans.factory.config ConfigurableListableBeanFactory getBeanDefinition

Introduction

In this page you can find the example usage for org.springframework.beans.factory.config ConfigurableListableBeanFactory getBeanDefinition.

Prototype

BeanDefinition getBeanDefinition(String beanName) throws NoSuchBeanDefinitionException;

Source Link

Document

Return the registered BeanDefinition for the specified bean, allowing access to its property values and constructor argument value (which can be modified during bean factory post-processing).

Usage

From source file:io.github.hzpz.spring.boot.autoconfigure.mongeez.MongoDbFactoryDependsOnPostProcessor.java

private static BeanDefinition getBeanDefinition(String beanName, ConfigurableListableBeanFactory beanFactory) {
    try {/*from   w  w w  .  java2s .c  o m*/
        return beanFactory.getBeanDefinition(beanName);
    } catch (NoSuchBeanDefinitionException ex) {
        BeanFactory parentBeanFactory = beanFactory.getParentBeanFactory();
        if (parentBeanFactory instanceof ConfigurableListableBeanFactory) {
            return getBeanDefinition(beanName, (ConfigurableListableBeanFactory) parentBeanFactory);
        }
        throw ex;
    }
}

From source file:com.mtgi.analytics.aop.config.v11.BtXmlPersisterBeanDefinitionParser.java

public static void configureLogRotation(ParserContext parserContext, ConfigurableListableBeanFactory factory,
        String schedule) {//from   w w  w  . j av a 2  s  . c  o m
    BeanDefinition trigger = factory.getBeanDefinition(CONFIG_ROTATE_TRIGGER);
    configureTriggerDefinition(trigger, schedule,
            parserContext.getReaderContext().generateBeanName(trigger) + "_rotate");
    registerPostProcessor(parserContext, factory, CONFIG_SCHEDULER, CONFIG_ROTATE_TRIGGER);
}

From source file:org.echocat.jomon.spring.BeanFactoryUtils.java

@Nullable
public static Class<?> findTypeOfBeanDefinition(@Nonnull ConfigurableListableBeanFactory beanFactory,
        @Nonnull String beanName) {
    Class<?> type;/* w  ww .j av a2  s  .  c  o m*/
    final BeanDefinition definition = beanFactory.getBeanDefinition(beanName);
    if (definition != null && definition.getFactoryMethodName() == null) {
        final String beanClassName = definition.getBeanClassName();
        if (beanClassName != null) {
            try {
                type = AutomaticServicesDiscovery.class.getClassLoader().loadClass(beanClassName);
            } catch (final ClassNotFoundException ignored) {
                type = null;
            }
        } else {
            type = null;
        }
    } else {
        type = null;
    }
    return type == null || FactoryBean.class.isAssignableFrom(type) ? null : type;
}

From source file:org.echocat.jomon.spring.BeanFactoryUtils.java

@Nullable
public static String findScopeOfBeanDefinition(@Nonnull ConfigurableListableBeanFactory beanFactory,
        @Nonnull String beanName) throws NoSuchBeanDefinitionException {
    final BeanDefinition definition = beanFactory.getBeanDefinition(beanName);
    if (definition == null) {
        throw new NoSuchBeanDefinitionException("Could not find a bean named '" + beanName + "'.");
    }//from w  w  w. ja  va2s  .  c  om
    return definition.getScope();
}

From source file:uk.org.ponder.rsac.support.BeanDefUtil.java

static AbstractBeanDefinition getMergedBeanDefinition(ConfigurableListableBeanFactory factory, String beanName,
        boolean includingAncestors) throws BeansException {
    try {/*from  w  w  w.  j  a  v  a  2s  . c  o m*/
        return getMergedBeanDefinition(factory, beanName, factory.getBeanDefinition(beanName));
    } catch (NoSuchBeanDefinitionException ex) {
        if (includingAncestors && factory.getParentBeanFactory() instanceof ConfigurableListableBeanFactory) {
            return getMergedBeanDefinition((ConfigurableListableBeanFactory) factory.getParentBeanFactory(),
                    beanName, true);
        } else {
            throw ex;
        }
    }
}

From source file:org.orderofthebee.addons.support.tools.repo.caches.CacheLookupUtils.java

/**
 * Resolves the {@link CacheStatistics cache statistics} of the {@link TransactionalCache transactional cache} instance that facades a
 * specific {@link SimpleCache shared//from  w ww.  j  a v  a2  s  . c om
 * cache} and provides it in a more script-friendly representation.
 *
 * @param sharedCacheName
 *            the name of the shared cache
 * @return a facade to a snapshot of the cache statistics
 */
public static AlfrescoCacheStatsFacade resolveStatisticsViaTransactional(final String sharedCacheName) {
    ParameterCheck.mandatoryString("sharedCacheName", sharedCacheName);

    LOGGER.debug("Trying to resolve transactional cache for shared cache {}", sharedCacheName);

    String txnCacheName = null;

    final WebApplicationContext applicationContext = ContextLoader.getCurrentWebApplicationContext();

    if (applicationContext instanceof ConfigurableApplicationContext) {
        final ConfigurableListableBeanFactory beanFactory = ((ConfigurableApplicationContext) applicationContext)
                .getBeanFactory();
        final String[] txnCacheBeanNames = applicationContext.getBeanNamesForType(TransactionalCache.class,
                false, false);

        // this is a rather ugly reference lookup, but so far I see no other way
        for (final String txnCacheBeanName : txnCacheBeanNames) {
            final BeanDefinition txnCacheDefinition = beanFactory.getBeanDefinition(txnCacheBeanName);

            final PropertyValue sharedCacheValue = txnCacheDefinition.getPropertyValues()
                    .getPropertyValue("sharedCache");
            final PropertyValue nameValue = txnCacheDefinition.getPropertyValues().getPropertyValue("name");
            if (nameValue != null && sharedCacheValue != null) {
                final Object sharedCacheRef = sharedCacheValue.getValue();
                if (sharedCacheRef instanceof RuntimeBeanReference) {
                    final String sharedCacheBeanName = ((RuntimeBeanReference) sharedCacheRef).getBeanName();

                    Object nameValueObj = nameValue.getValue();
                    if (nameValueObj instanceof TypedStringValue) {
                        nameValueObj = ((TypedStringValue) nameValueObj).getValue();
                    }

                    if (EqualsHelper.nullSafeEquals(sharedCacheBeanName, sharedCacheName)) {
                        if (txnCacheName != null) {
                            LOGGER.info("Shared cache {} is referenced by multiple transactional caches",
                                    sharedCacheName);
                            txnCacheName = null;
                            break;
                        }
                        txnCacheName = String.valueOf(nameValueObj);
                        LOGGER.debug("Resolved transactional cache {} for shared cache {}", txnCacheName,
                                sharedCacheName);
                    } else {
                        try {
                            final DefaultSimpleCache<?, ?> defaultSimpleCache = applicationContext
                                    .getBean(sharedCacheBeanName, DefaultSimpleCache.class);
                            if (EqualsHelper.nullSafeEquals(defaultSimpleCache.getCacheName(),
                                    sharedCacheName)) {
                                if (txnCacheName != null) {
                                    LOGGER.info(
                                            "Shared cache {} is referenced by multiple transactional caches",
                                            sharedCacheName);
                                    txnCacheName = null;
                                    break;
                                }
                                txnCacheName = String.valueOf(nameValueObj);
                                LOGGER.debug("Resolved transactional cache {} for shared cache {}",
                                        txnCacheName, sharedCacheName);
                            }
                            continue;
                        } catch (final BeansException be) {
                            // ignore - can be expected e.g. in EE or with alternative cache implementations
                        }
                    }
                }
            }
        }

        if (txnCacheName == null) {
            LOGGER.debug("Unable to resolve unique transactional cache for shared cache {}", sharedCacheName);
        }
    } else {
        LOGGER.debug(
                "Application context is not a configurable application context - unable to resolve transactional cache");
    }

    AlfrescoCacheStatsFacade facade = null;
    if (txnCacheName != null) {
        final CacheStatistics cacheStatistics = applicationContext.getBean("cacheStatistics",
                CacheStatistics.class);
        try {
            final Map<OpType, OperationStats> allStats = cacheStatistics.allStats(txnCacheName);
            facade = new AlfrescoCacheStatsFacade(allStats);
        } catch (final NoStatsForCache e) {
            facade = new AlfrescoCacheStatsFacade(Collections.emptyMap());
        }
    }

    return facade;
}

From source file:me.springframework.di.spring.SpringConfigurationLoader.java

/**
 * Returns a {@link Map} of {@link MutableInstance MutableInstances},
 * indexed by name.// w  ww  .j  a va2  s  .  c  o  m
 * 
 * @param registry
 *            The {@link BeanDefinitionRegistry} holding the bean
 *            definitions.
 * @return A {@link Map} of {@link MutableInstance MutableInstances}
 *         representing the root beans defined by the
 *         {@link ListableBeanFactory}.
 */
protected static MutableContext loadBeans(ConfigurableListableBeanFactory factory) {
    MutableContext context = new MutableContext();
    for (String name : factory.getBeanDefinitionNames()) {
        for (String alias : factory.getAliases(name)) {
            context.addAlias(alias, name);
        }
    }

    for (String name : factory.getBeanDefinitionNames()) {
        BeanDefinition definition = factory.getBeanDefinition(name);
        if (!definition.isAbstract()) {
            BeanDefinition merged = factory.getMergedBeanDefinition(name);
            MutableInstance instance = new MutableInstance(name);
            load(instance, merged, context);
            context.addInstance(name, instance);
        }
    }
    return context;
}

From source file:com.dianping.simple.spring.TestBeanFactoryPostProcessor.java

@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    // TODO Auto-generated method stub
    BeanDefinition bd = beanFactory.getBeanDefinition("testServiceTarget");
    bd.getPropertyValues().addPropertyValue("name", "no monkey");
    logger.info("postProcessBeanFactory " + bd.getBeanClassName());
}

From source file:com.apporiented.spring.override.BeanOverrideProcessor.java

public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

    BeanDefinition target = beanFactory.getBeanDefinition(ref);
    overwriteBeanDefinition(target, beanDefinition);
}

From source file:com.easyshop.datasource.MyBatisBeanFactoryPostProcessor.java

/**
 * @param beanFactory/* w  w w. j  a  v  a2 s.  co m*/
 * @throws BeansException
 * @see org.springframework.beans.factory.config.BeanFactoryPostProcessor#postProcessBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)
 */
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

    BeanDefinition bd = beanFactory.getBeanDefinition(sqlSessionFactoryBeanName);
    MutablePropertyValues propertyValues = bd.getPropertyValues();
    PropertyValue propertyValue = propertyValues.getPropertyValue("mapperLocations");
    Object value = propertyValue.getValue();

    ManagedList<TypedStringValue> locations = new ManagedList<TypedStringValue>();
    for (String location : mapperLocations) {
        locations.add(new TypedStringValue(location));
        log.info(" SQL Mapper  :{} " + location);
    }

    if (value == null) {
        PropertyValue newValue = new PropertyValue(propertyValue, locations);
        propertyValues.addPropertyValue(newValue);
    } else if (value instanceof String) {
        locations.add(new TypedStringValue((String) value));
        PropertyValue newValue = new PropertyValue(propertyValue, locations);
        propertyValues.addPropertyValue(newValue);
    } else if (value instanceof ManagedList) {
        ((ManagedList) value).addAll(locations);
    } else if (value instanceof TypedStringValue) {
        locations.add((TypedStringValue) value);
        PropertyValue newValue = new PropertyValue(propertyValue, locations);
        propertyValues.addPropertyValue(newValue);
    }
}