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:org.kuali.rice.krad.datadictionary.uif.UifBeanFactoryPostProcessor.java

/**
 * Iterates through all beans in the factory and invokes processing
 *
 * @param beanFactory bean factory instance to process
 * @throws org.springframework.beans.BeansException
 *//* www .j ava 2s  .c  om*/
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    Set<String> processedBeanNames = new HashSet<String>();

    LOG.info("Beginning post processing of bean factory for UIF expressions");

    String[] beanNames = beanFactory.getBeanDefinitionNames();
    for (int i = 0; i < beanNames.length; i++) {
        String beanName = beanNames[i];
        BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName);

        processBeanDefinition(beanName, beanDefinition, beanFactory, processedBeanNames);
    }

    LOG.info("Finished post processing of bean factory for UIF expressions");
}

From source file:org.kuali.rice.krad.datadictionary.uif.UifBeanFactoryPostProcessor.java

/**
 * Retrieves the expression graph map set on the bean with given name. If the bean has not been processed
 * by the bean factory post processor, that is done before retrieving the map
 *
 * @param parentBeanName name of the parent bean to retrieve map for (if empty a new map will be returned)
 * @param beanFactory bean factory to retrieve bean definition from
 * @param processedBeanNames set of bean names that have been processed so far
 * @return expression graph map from parent or new instance
 *///  w ww  .j  a  va2s.  co  m
protected Map<String, String> getExpressionGraphFromParent(String parentBeanName,
        ConfigurableListableBeanFactory beanFactory, Set<String> processedBeanNames) {
    Map<String, String> expressionGraph = new HashMap<String, String>();
    if (StringUtils.isBlank(parentBeanName) || !beanFactory.containsBeanDefinition(parentBeanName)) {
        return expressionGraph;
    }

    BeanDefinition beanDefinition = beanFactory.getBeanDefinition(parentBeanName);
    if (!processedBeanNames.contains(parentBeanName)) {
        processBeanDefinition(parentBeanName, beanDefinition, beanFactory, processedBeanNames);
    }

    MutablePropertyValues pvs = beanDefinition.getPropertyValues();
    PropertyValue propertyExpressionsPV = pvs.getPropertyValue(UifPropertyPaths.EXPRESSION_GRAPH);
    if (propertyExpressionsPV != null) {
        Object value = propertyExpressionsPV.getValue();
        if ((value != null) && (value instanceof ManagedMap)) {
            expressionGraph.putAll((ManagedMap) value);
        }
    }

    return expressionGraph;
}

From source file:org.nextframework.persistence.GenericDAO.java

public JdbcTemplate getJdbcTemplate() {
    if (jdbcTemplate == null) {
        String[] beans = applicationContext.getBeanNamesForType(DataSource.class);
        if (beans.length == 1) {
            jdbcTemplate = new JdbcTemplate(applicationContext.getBean(beans[0], DataSource.class));
        } else if (applicationContext instanceof ConfigurableApplicationContext) {
            //TODO THIS LOGIC SHOULD NOT BE HERE
            //check the qualifiers to find the correct datasource
            ConfigurableListableBeanFactory beanFactory = ((ConfigurableApplicationContext) applicationContext)
                    .getBeanFactory();//from w w w  . j a va 2s.c  o m
            for (String beanName : beans) {
                AbstractBeanDefinition beanDefinition = (AbstractBeanDefinition) beanFactory
                        .getBeanDefinition(beanName);
                Set<AutowireCandidateQualifier> qualifiers = beanDefinition.getQualifiers();
                for (AutowireCandidateQualifier autowireCandidateQualifier : qualifiers) {
                    String[] attributeNames = autowireCandidateQualifier.attributeNames();
                    for (String attribute : attributeNames) {
                        Object value = autowireCandidateQualifier.getMetadataAttribute(attribute).getValue();
                        if (getPersistenceContext().equals(value)) {
                            jdbcTemplate = new JdbcTemplate(
                                    applicationContext.getBean(beanName, DataSource.class));
                            return jdbcTemplate;
                        }
                    }
                }
            }
        }
    }
    return jdbcTemplate;
}

From source file:org.pentaho.platform.engine.core.system.objfac.spring.PentahoBeanScopeValidatorPostProcessor.java

@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

    if (beanFactory != null && beanFactory.getBeanDefinitionCount() > 0) {

        for (String beanName : beanFactory.getBeanDefinitionNames()) {

            validateBeanScope(beanName, beanFactory.getBeanDefinition(beanName));

        }//from w  w w .ja  va 2s . c o  m
    }
}

From source file:org.springframework.amqp.rabbit.stocks.context.RefreshScope.java

public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

    beanFactory.registerScope(name, this);
    setSerializationId(beanFactory);/*from  w  w w.j a v a 2 s .co  m*/

    this.beanFactory = beanFactory;

    evaluationContext = new StandardEvaluationContext();
    evaluationContext.addPropertyAccessor(new BeanFactoryAccessor());

    Assert.state(beanFactory instanceof BeanDefinitionRegistry,
            "BeanFactory was not a BeanDefinitionRegistry, so RefreshScope cannot be used.");
    BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;

    for (String beanName : beanFactory.getBeanDefinitionNames()) {
        BeanDefinition definition = beanFactory.getBeanDefinition(beanName);
        // Replace this or any of its inner beans with scoped proxy if it
        // has this scope
        boolean scoped = name.equals(definition.getScope());
        Scopifier scopifier = new Scopifier(registry, name, proxyTargetClass, scoped);
        scopifier.visitBeanDefinition(definition);
        if (scoped) {
            createScopedProxy(beanName, definition, registry, proxyTargetClass);
        }
    }

}

From source file:org.springframework.batch.core.partition.gemfire.RemoteScope.java

public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

    beanFactory.registerScope(name, this);

    this.beanFactory = beanFactory;
    evaluationContext = new StandardEvaluationContext();
    evaluationContext.addPropertyAccessor(new BeanFactoryAccessor());

    Assert.state(beanFactory instanceof BeanDefinitionRegistry,
            "BeanFactory was not a BeanDefinitionRegistry, so RefreshScope cannot be used.");
    BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;

    String id = this.id;
    if (id == null) {
        String names = Arrays.asList(registry.getBeanDefinitionNames()).toString();
        logger.debug("Generating bean factory id from names: " + names);
        id = UUID.nameUUIDFromBytes(names.getBytes()).toString();
        logger.debug("Generated bean factory id: " + id);
    }//from   w ww .j  a  va2  s.  c  om

    Assert.state(beanFactory instanceof BeanDefinitionRegistry,
            "BeanFactory was not a DefaultListableBeanFactory, so RefreshScope cannot be used.");
    ((DefaultListableBeanFactory) beanFactory).setSerializationId(id);

    for (String beanName : beanFactory.getBeanDefinitionNames()) {
        BeanDefinition definition = beanFactory.getBeanDefinition(beanName);
        // Replace this or any of its inner beans with scoped proxy if it
        // has this scope
        boolean scoped = name.equals(definition.getScope());
        Scopifier scopifier = new Scopifier(registry, name, proxyTargetClass, scoped);
        scopifier.visitBeanDefinition(definition);
        if (scoped) {
            createScopedProxy(beanName, definition, registry, proxyTargetClass);
        }
    }

}

From source file:org.springframework.beans.factory.config.DeprecatedBeanWarner.java

@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    if (isLogEnabled()) {
        String[] beanNames = beanFactory.getBeanDefinitionNames();
        for (String beanName : beanNames) {
            String nameToLookup = beanName;
            if (beanFactory.isFactoryBean(beanName)) {
                nameToLookup = BeanFactory.FACTORY_BEAN_PREFIX + beanName;
            }/*from w w  w .  j  a  v  a2 s  .  c  o  m*/
            Class<?> beanType = beanFactory.getType(nameToLookup);
            if (beanType != null) {
                Class<?> userClass = ClassUtils.getUserClass(beanType);
                if (userClass.isAnnotationPresent(Deprecated.class)) {
                    BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName);
                    logDeprecatedBean(beanName, beanType, beanDefinition);
                }
            }
        }
    }
}

From source file:org.springframework.boot.autoconfigure.ComponentScanDetector.java

private void storeComponentScanBasePackages(ConfigurableListableBeanFactory beanFactory) {
    List<String> basePackages = new ArrayList<String>();
    for (String beanName : beanFactory.getBeanDefinitionNames()) {
        BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName);
        String[] basePackagesAttribute = (String[]) beanDefinition.getAttribute("componentScanBasePackages");
        if (basePackagesAttribute != null) {
            basePackages.addAll(Arrays.asList(basePackagesAttribute));
        }//from   w w w  .  j av a2 s.c o  m
        AnnotationMetadata metadata = getMetadata(beanDefinition);
        basePackages.addAll(getBasePackages(metadata));
    }
    AutoConfigurationUtils.storeBasePackages(beanFactory, basePackages);
}

From source file:org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration.java

/**
 * Determines if the {@code dataSource} being used by Spring was created from
 * {@link EmbeddedDataSourceConfiguration}.
 * @param beanFactory the bean factory//from  w w w  .  j a va  2s . c o  m
 * @return true if the data source was auto-configured.
 */
public static boolean containsAutoConfiguredDataSource(ConfigurableListableBeanFactory beanFactory) {
    try {
        BeanDefinition beanDefinition = beanFactory.getBeanDefinition("dataSource");
        return EmbeddedDataSourceConfiguration.class.getName().equals(beanDefinition.getFactoryBeanName());
    } catch (NoSuchBeanDefinitionException ex) {
        return false;
    }
}

From source file:org.springframework.cloud.context.scope.GenericScope.java

public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

    beanFactory.registerScope(name, this);
    setSerializationId(beanFactory);/*from ww w.  j a v  a  2  s.  co  m*/

    this.beanFactory = beanFactory;

    evaluationContext = new StandardEvaluationContext();
    evaluationContext.addPropertyAccessor(new BeanFactoryAccessor());

    if (!autoProxy) {
        // No need to try and create proxies
        return;
    }

    Assert.state(beanFactory instanceof BeanDefinitionRegistry,
            "BeanFactory was not a BeanDefinitionRegistry, so RefreshScope cannot be used.");
    BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;

    for (String beanName : beanFactory.getBeanDefinitionNames()) {
        BeanDefinition definition = beanFactory.getBeanDefinition(beanName);
        // Replace this or any of its inner beans with scoped proxy if it
        // has this scope
        boolean scoped = name.equals(definition.getScope());
        Scopifier scopifier = new Scopifier(registry, name, proxyTargetClass, scoped);
        scopifier.visitBeanDefinition(definition);
        if (scoped) {
            createScopedProxy(beanName, definition, registry, proxyTargetClass);
        }
    }

}