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

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

Introduction

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

Prototype

void addBeanPostProcessor(BeanPostProcessor beanPostProcessor);

Source Link

Document

Add a new BeanPostProcessor that will get applied to beans created by this factory.

Usage

From source file:org.springframework.context.support.PostProcessorRegistrationDelegate.java

public static void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory,
        AbstractApplicationContext applicationContext) {

    String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false);

    // Register BeanPostProcessorChecker that logs an info message when
    // a bean is created during BeanPostProcessor instantiation, i.e. when
    // a bean is not eligible for getting processed by all BeanPostProcessors.
    int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length;
    beanFactory.addBeanPostProcessor(new BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount));

    // Separate between BeanPostProcessors that implement PriorityOrdered,
    // Ordered, and the rest.
    List<BeanPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
    List<BeanPostProcessor> internalPostProcessors = new ArrayList<>();
    List<String> orderedPostProcessorNames = new ArrayList<>();
    List<String> nonOrderedPostProcessorNames = new ArrayList<>();
    for (String ppName : postProcessorNames) {
        if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
            BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
            priorityOrderedPostProcessors.add(pp);
            if (pp instanceof MergedBeanDefinitionPostProcessor) {
                internalPostProcessors.add(pp);
            }//from ww w  . java  2 s  . c  o  m
        } else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
            orderedPostProcessorNames.add(ppName);
        } else {
            nonOrderedPostProcessorNames.add(ppName);
        }
    }

    // First, register the BeanPostProcessors that implement PriorityOrdered.
    sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
    registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);

    // Next, register the BeanPostProcessors that implement Ordered.
    List<BeanPostProcessor> orderedPostProcessors = new ArrayList<>();
    for (String ppName : orderedPostProcessorNames) {
        BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
        orderedPostProcessors.add(pp);
        if (pp instanceof MergedBeanDefinitionPostProcessor) {
            internalPostProcessors.add(pp);
        }
    }
    sortPostProcessors(orderedPostProcessors, beanFactory);
    registerBeanPostProcessors(beanFactory, orderedPostProcessors);

    // Now, register all regular BeanPostProcessors.
    List<BeanPostProcessor> nonOrderedPostProcessors = new ArrayList<>();
    for (String ppName : nonOrderedPostProcessorNames) {
        BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
        nonOrderedPostProcessors.add(pp);
        if (pp instanceof MergedBeanDefinitionPostProcessor) {
            internalPostProcessors.add(pp);
        }
    }
    registerBeanPostProcessors(beanFactory, nonOrderedPostProcessors);

    // Finally, re-register all internal BeanPostProcessors.
    sortPostProcessors(internalPostProcessors, beanFactory);
    registerBeanPostProcessors(beanFactory, internalPostProcessors);

    // Re-register post-processor for detecting inner beans as ApplicationListeners,
    // moving it to the end of the processor chain (for picking up proxies etc).
    beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext));
}

From source file:org.springframework.context.support.PostProcessorRegistrationDelegate.java

/**
 * Register the given BeanPostProcessor beans.
 *//*from w  w  w .  ja  v a  2 s.  co  m*/
private static void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory,
        List<BeanPostProcessor> postProcessors) {

    for (BeanPostProcessor postProcessor : postProcessors) {
        beanFactory.addBeanPostProcessor(postProcessor);
    }
}

From source file:org.springframework.richclient.application.ProgressMonitoringBeanFactoryPostProcessor.java

/**
 * Notifies this instance's associated progress monitor of progress made
 * while processing the given bean factory.
 * //  w w w  . j a  va  2s  .  co  m
 * @param beanFactory the bean factory that is to be processed.
 */
public void postProcessBeanFactory(final ConfigurableListableBeanFactory beanFactory) throws BeansException {

    if (beanFactory == null) {
        return;
    }

    String[] beanNames = beanFactory.getBeanDefinitionNames();
    int singletonBeanCount = 0;

    for (int i = 0; i < beanNames.length; i++) {
        // using beanDefinition to check singleton property because when
        // accessing through
        // context (applicationContext.isSingleton(beanName)), bean will be
        // created already,
        // possibly bypassing other BeanFactoryPostProcessors
        BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanNames[i]);

        if (beanDefinition.isSingleton()) {
            singletonBeanCount++;
        }

    }

    this.progressMonitor.taskStarted(this.loadingAppContextMessage, singletonBeanCount);

    beanFactory.addBeanPostProcessor(new ProgressMonitoringBeanPostProcessor(beanFactory));

}

From source file:org.springframework.web.struts.ContextLoaderPlugIn.java

/**
 * Instantiate the WebApplicationContext for the ActionServlet, either a default
 * XmlWebApplicationContext or a custom context class if set. This implementation
 * expects custom contexts to implement ConfigurableWebApplicationContext.
 * Can be overridden in subclasses./*from w ww .  j av a  2  s  .c  o m*/
 * @throws org.springframework.beans.BeansException if the context couldn't be initialized
 * @see #setContextClass
 * @see org.springframework.web.context.support.XmlWebApplicationContext
 */
protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent)
        throws BeansException {

    if (logger.isDebugEnabled()) {
        logger.debug("ContextLoaderPlugIn for Struts ActionServlet '" + getServletName() + "', module '"
                + getModulePrefix() + "' will try to create custom WebApplicationContext "
                + "context of class '" + getContextClass().getName() + "', using parent context [" + parent
                + "]");
    }
    if (!ConfigurableWebApplicationContext.class.isAssignableFrom(getContextClass())) {
        throw new ApplicationContextException(
                "Fatal initialization error in ContextLoaderPlugIn for Struts ActionServlet '"
                        + getServletName() + "', module '" + getModulePrefix()
                        + "': custom WebApplicationContext class [" + getContextClass().getName()
                        + "] is not of type ConfigurableWebApplicationContext");
    }

    ConfigurableWebApplicationContext wac = (ConfigurableWebApplicationContext) BeanUtils
            .instantiateClass(getContextClass());
    wac.setParent(parent);
    wac.setServletContext(getServletContext());
    wac.setNamespace(getNamespace());
    if (getContextConfigLocation() != null) {
        wac.setConfigLocations(StringUtils.tokenizeToStringArray(getContextConfigLocation(),
                ConfigurableWebApplicationContext.CONFIG_LOCATION_DELIMITERS));
    }
    wac.addBeanFactoryPostProcessor(new BeanFactoryPostProcessor() {
        public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
            beanFactory.addBeanPostProcessor(new ActionServletAwareProcessor(getActionServlet()));
        }
    });
    wac.refresh();
    return wac;
}