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:net.solarnetwork.web.gemini.ServerOsgiBundleXmlWebApplicationContext.java

/**
 * Register request/session scopes, a {@link ServletContextAwareProcessor},
 * etc./*from   w  ww. j  a  v  a  2 s  .co m*/
 * 
 * @see WebApplicationContextUtils#registerWebApplicationScopes(ConfigurableListableBeanFactory)
 */
@Override
protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
    super.postProcessBeanFactory(beanFactory);

    // Drive the kernel's bean factory post processors.
    BundleContext bundleContext = getBundleContext();
    if (bundleContext != null) {
        ServiceReference<OsgiBeanFactoryPostProcessor> sr = bundleContext
                .getServiceReference(OsgiBeanFactoryPostProcessor.class);
        if (sr != null) {
            OsgiBeanFactoryPostProcessor kernelPostProcessor = bundleContext.getService(sr);
            try {
                kernelPostProcessor.postProcessBeanFactory(bundleContext, beanFactory);
            } catch (Exception e) {
                throw new ApplicationContextException("Kernel bean factory post processor failed", e);
            } finally {
                bundleContext.ungetService(sr);
            }
        }
    }

    beanFactory.addBeanPostProcessor(new ServletContextAwareProcessor(getServletContext(), getServletConfig()));
    beanFactory.ignoreDependencyInterface(ServletContextAware.class);
    beanFactory.ignoreDependencyInterface(ServletConfigAware.class);
    beanFactory.registerResolvableDependency(ServletContext.class, getServletContext());
    beanFactory.registerResolvableDependency(ServletConfig.class, getServletConfig());

    WebApplicationContextUtils.registerWebApplicationScopes(beanFactory);
}

From source file:com.ms.commons.test.BaseTestCase.java

/**
 * Service???//from w w  w .  java2s  . c o  m
 * 
 * @throws InstantiationException
 * @throws IllegalAccessException
 */
protected void initMoke() throws InstantiationException, IllegalAccessException {
    // Mock once on startup
    if (isMockOn()) {

        if (log.isDebugEnabled()) {
            log.debug("================>initMock!");
        }
        ConfigurableApplicationContext context = (ConfigurableApplicationContext) CommonServiceLocator
                .getApplicationContext();
        context.addBeanFactoryPostProcessor(new BeanFactoryPostProcessor() {

            public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
                    throws BeansException {
                beanFactory.addBeanPostProcessor(new MockBeanPostProcessor());
            }
        });
        context.refresh();
    }
}

From source file:org.artifactory.spring.ArtifactoryApplicationContext.java

@Override
protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
    super.prepareBeanFactory(beanFactory);
    //Add our own post processor that registers all reloadable beans auto-magically after construction
    beanFactory.addBeanPostProcessor(new BeanPostProcessor() {
        @Override/* ww  w .  jav a  2s  . c  o m*/
        public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
            Class<?> targetClass = AopUtils.getTargetClass(bean);
            if (ReloadableBean.class.isAssignableFrom(targetClass)) {
                Reloadable annotation;
                if (targetClass.isAnnotationPresent(Reloadable.class)) {
                    annotation = targetClass.getAnnotation(Reloadable.class);
                    Class<? extends ReloadableBean> beanClass = annotation.beanClass();
                    addReloadableBean(beanClass);
                } else {
                    throw new IllegalStateException("Bean " + targetClass.getName()
                            + " requires initialization beans to be initialized, but no such beans were found");
                }
            }
            return bean;
        }

        @Override
        public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
            //Do nothing
            return bean;
        }
    });
}

From source file:org.eclipse.gemini.blueprint.extender.internal.support.OsgiAnnotationPostProcessor.java

public void postProcessBeanFactory(BundleContext bundleContext, ConfigurableListableBeanFactory beanFactory)
        throws BeansException, OsgiException {

    Bundle bundle = bundleContext.getBundle();
    try {/*from  w w w  .j  a  v  a 2  s. c o m*/
        // Try and load the annotation code using the bundle classloader
        Class<?> annotationBppClass = bundle.loadClass(ANNOTATION_BPP_CLASS);
        // instantiate the class
        final BeanPostProcessor annotationBeanPostProcessor = (BeanPostProcessor) BeanUtils
                .instantiateClass(annotationBppClass);

        // everything went okay so configure the BPP and add it to the BF
        ((BeanFactoryAware) annotationBeanPostProcessor).setBeanFactory(beanFactory);
        ((BeanClassLoaderAware) annotationBeanPostProcessor)
                .setBeanClassLoader(beanFactory.getBeanClassLoader());
        ((BundleContextAware) annotationBeanPostProcessor).setBundleContext(bundleContext);
        beanFactory.addBeanPostProcessor(annotationBeanPostProcessor);
    } catch (ClassNotFoundException exception) {
        log.info("Spring-DM annotation package could not be loaded from bundle ["
                + OsgiStringUtils.nullSafeNameAndSymName(bundle) + "]; annotation processing disabled...");
        if (log.isDebugEnabled())
            log.debug("Cannot load annotation injection processor", exception);
    }
}

From source file:org.geoserver.test.GeoServerTestApplicationContext.java

@Override
protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
    beanFactory.addBeanPostProcessor(new ServletContextAwareProcessor(this.servletContext));
    beanFactory.ignoreDependencyInterface(ServletContextAware.class);
    beanFactory.ignoreDependencyInterface(ServletConfigAware.class);

    WebApplicationContextUtils.registerWebApplicationScopes(beanFactory, this.servletContext);
    WebApplicationContextUtils.registerEnvironmentBeans(beanFactory, this.servletContext);
}

From source file:org.robospring.RoboSpring.java

private static AbstractXmlApplicationContext createContext(String contextConfigLocation) {

    String scheme;//from  ww w  .j  a  v  a 2s.  co m
    String resourceName;

    try {
        URI url = new URI(contextConfigLocation);
        scheme = url.getScheme();
        if (scheme == null) {
            scheme = "classpath";
        }
        resourceName = url.getPath();
    } catch (URISyntaxException e) {
        log.warn("ContextConfigLocation does not contain a scheme identifier - using classpath:/ as default.");
        // Trying to load from class path with this name
        scheme = "classpath";
        resourceName = contextConfigLocation;
    }

    if ("classpath".equals(scheme)) {
        if (parentContext != null) {
            String[] configLocations = new String[] { resourceName };
            return new ClassPathXmlApplicationContext(configLocations, parentContext) {
                protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
                    beanFactory.addBeanPostProcessor(new AndroidContextAwareProcessor(androidAppContext));
                    super.prepareBeanFactory(beanFactory);
                };
            };
        } else {
            return new ClassPathXmlApplicationContext(resourceName);
        }
    } else if ("file".equals(scheme)) {
        if (parentContext != null) {
            String[] configLocations = new String[] { resourceName };
            return new FileSystemXmlApplicationContext(configLocations, parentContext) {
                protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
                    beanFactory.addBeanPostProcessor(new AndroidContextAwareProcessor(androidAppContext));
                    super.prepareBeanFactory(beanFactory);
                };
            };
        } else {
            return new FileSystemXmlApplicationContext(resourceName);
        }
    } else {
        throw new IllegalArgumentException(
                "Cannot handle scheme '" + scheme + "' for loading Spring configuration.");
    }
}

From source file:org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.java

/**
 * Register ServletContextAwareProcessor.
 * @see ServletContextAwareProcessor//from   www .j  a  v  a 2 s.c om
 */
@Override
protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
    beanFactory.addBeanPostProcessor(new WebApplicationContextServletContextAwareProcessor(this));
    beanFactory.ignoreDependencyInterface(ServletContextAware.class);
}

From source file:org.springframework.context.annotation.ConfigurationClassPostProcessor.java

/**
 * Prepare the Configuration classes for servicing bean requests at runtime
 * by replacing them with CGLIB-enhanced subclasses.
 *///w ww .j  a v  a 2  s .co m
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
    int factoryId = System.identityHashCode(beanFactory);
    if (this.factoriesPostProcessed.contains(factoryId)) {
        throw new IllegalStateException(
                "postProcessBeanFactory already called on this post-processor against " + beanFactory);
    }
    this.factoriesPostProcessed.add(factoryId);
    if (!this.registriesPostProcessed.contains(factoryId)) {
        // BeanDefinitionRegistryPostProcessor hook apparently not supported...
        // Simply call processConfigurationClasses lazily at this point then.
        processConfigBeanDefinitions((BeanDefinitionRegistry) beanFactory);
    }

    enhanceConfigurationClasses(beanFactory);
    beanFactory.addBeanPostProcessor(new ImportAwareBeanPostProcessor(beanFactory));
}

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

/**
 * Configure the factory's standard context characteristics,
 * such as the context's ClassLoader and post-processors.
 * @param beanFactory the BeanFactory to configure
 *//*w  w w  .  j av  a2 s. c o m*/
protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
    // Tell the internal bean factory to use the context's class loader etc.
    beanFactory.setBeanClassLoader(getClassLoader());
    beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));
    beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));

    // Configure the bean factory with context callbacks.
    beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
    beanFactory.ignoreDependencyInterface(EnvironmentAware.class);
    beanFactory.ignoreDependencyInterface(EmbeddedValueResolverAware.class);
    beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);
    beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);
    beanFactory.ignoreDependencyInterface(MessageSourceAware.class);
    beanFactory.ignoreDependencyInterface(ApplicationContextAware.class);

    // BeanFactory interface not registered as resolvable type in a plain factory.
    // MessageSource registered (and found for autowiring) as a bean.
    beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory);
    beanFactory.registerResolvableDependency(ResourceLoader.class, this);
    beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this);
    beanFactory.registerResolvableDependency(ApplicationContext.class, this);

    // Register early post-processor for detecting inner beans as ApplicationListeners.
    beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));

    // Detect a LoadTimeWeaver and prepare for weaving, if found.
    if (beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
        beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
        // Set a temporary ClassLoader for type matching.
        beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
    }

    // Register default environment beans.
    if (!beanFactory.containsLocalBean(ENVIRONMENT_BEAN_NAME)) {
        beanFactory.registerSingleton(ENVIRONMENT_BEAN_NAME, getEnvironment());
    }
    if (!beanFactory.containsLocalBean(SYSTEM_PROPERTIES_BEAN_NAME)) {
        beanFactory.registerSingleton(SYSTEM_PROPERTIES_BEAN_NAME, getEnvironment().getSystemProperties());
    }
    if (!beanFactory.containsLocalBean(SYSTEM_ENVIRONMENT_BEAN_NAME)) {
        beanFactory.registerSingleton(SYSTEM_ENVIRONMENT_BEAN_NAME, getEnvironment().getSystemEnvironment());
    }
}

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

/**
 * Instantiate and invoke all registered BeanFactoryPostProcessor beans,
 * respecting explicit order if given./*from ww w .j  a  va  2 s  .  c  o  m*/
 * <p>Must be called before singleton instantiation.
 */
protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) {
    PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory,
            getBeanFactoryPostProcessors());

    // Detect a LoadTimeWeaver and prepare for weaving, if found in the meantime
    // (e.g. through an @Bean method registered by ConfigurationClassPostProcessor)
    if (beanFactory.getTempClassLoader() == null && beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
        beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
        beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
    }
}