java类org.springframework.beans.factory.config.BeanFactoryPostProcessor的实例源码

SpringLogInjectionService.java 文件源码 项目:loginject 阅读 24 收藏 0 点赞 0 评论 0
@Override
public BeanFactoryPostProcessor getBindings(LogInject<_Logger_> logInject)
{
    return new BeanFactoryPostProcessor()
    {
        ThreadLocal<Object> injectee = new ThreadLocal<>();

        @Override
        public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException
        {
            DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory)beanFactory;
            AutowireCandidateResolver resolver = new ContextAnnotationAutowireCandidateResolver()
            {
                @Override
                public Object getSuggestedValue(DependencyDescriptor descriptor)
                {
                    if (descriptor.getDependencyType().equals(logInject.getLoggerClass()))
                    {
                        return logInject.createLogger(injectee.get().getClass());
                    }
                    return null;
                }
            };
            AutowiredAnnotationBeanPostProcessor beanPostProcessor = new AutowiredAnnotationBeanPostProcessor()
            {
                @Override
                public PropertyValues postProcessPropertyValues(PropertyValues values, PropertyDescriptor[] descriptors,
                    Object bean, String beanName) throws BeansException
                {
                    injectee.set(bean);
                    return super.postProcessPropertyValues(values, descriptors, bean, beanName);
                }
            };
            beanPostProcessor.setBeanFactory(defaultListableBeanFactory);
            defaultListableBeanFactory.addBeanPostProcessor(beanPostProcessor);
            defaultListableBeanFactory.setAutowireCandidateResolver(resolver);
        }
    };
}
ContextLoaderPlugIn.java 文件源码 项目:spring-struts-forwardport 阅读 16 收藏 0 点赞 0 评论 0
/**
 * Instantiate the WebApplicationContext for the ActionServlet, either a default
 * XmlWebApplicationContext or a custom context class if set.
 * <p>This implementation expects custom contexts to implement ConfigurableWebApplicationContext.
 * Can be overridden in subclasses.
 * @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()));
                    beanFactory.ignoreDependencyType(ActionServlet.class);
                }
            }
    );

    wac.refresh();
    return wac;
}
ContextUtils.java 文件源码 项目:spring-component-framework 阅读 26 收藏 0 点赞 0 评论 0
/**
 * 继承上级上下文中的属性配置器
 *
 * @param inheriting  上级上下文
 * @param context 当前上下文
 */
public static void inheritParentProperties(ApplicationContext inheriting,
                                           GenericApplicationContext context) {
    if (!(inheriting instanceof AbstractApplicationContext)) return;
    List<BeanFactoryPostProcessor> processors =
            ((AbstractApplicationContext) inheriting).getBeanFactoryPostProcessors();
    for (BeanFactoryPostProcessor processor : processors) {
        if (processor instanceof PropertyResourceConfigurer)
            context.addBeanFactoryPostProcessor(processor);
    }
}
AbstractApplicationContext.java 文件源码 项目:class-guard 阅读 29 收藏 0 点赞 0 评论 0
/**
 * Invoke the given BeanFactoryPostProcessor beans.
 */
private void invokeBeanFactoryPostProcessors(
        Collection<? extends BeanFactoryPostProcessor> postProcessors, ConfigurableListableBeanFactory beanFactory) {

    for (BeanFactoryPostProcessor postProcessor : postProcessors) {
        postProcessor.postProcessBeanFactory(beanFactory);
    }
}
ConfigurationClassProcessingTests.java 文件源码 项目:class-guard 阅读 25 收藏 0 点赞 0 评论 0
public BeanFactoryPostProcessor beanFactoryPostProcessor() {
    return new BeanFactoryPostProcessor() {
        @Override
        public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
            BeanDefinition bd = beanFactory.getBeanDefinition("beanPostProcessor");
            bd.getPropertyValues().addPropertyValue("nameSuffix", "-processed-" + myProp);
        }
    };
}
ConfigurationClassAndBFPPTests.java 文件源码 项目:class-guard 阅读 22 收藏 0 点赞 0 评论 0
@Bean
public BeanFactoryPostProcessor bfpp() {
    return new BeanFactoryPostProcessor() {
        @Override
        public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
            // no-op
        }
    };
}
ConfigurationClassAndBFPPTests.java 文件源码 项目:class-guard 阅读 21 收藏 0 点赞 0 评论 0
@Bean
public static final BeanFactoryPostProcessor bfpp() {
    return new BeanFactoryPostProcessor() {
        @Override
        public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
            // no-op
        }
    };
}
ContextLoaderPlugIn.java 文件源码 项目:class-guard 阅读 18 收藏 0 点赞 0 评论 0
/**
 * Instantiate the WebApplicationContext for the ActionServlet, either a default
 * XmlWebApplicationContext or a custom context class if set.
 * <p>This implementation expects custom contexts to implement ConfigurableWebApplicationContext.
 * Can be overridden in subclasses.
 * @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()));
                    beanFactory.ignoreDependencyType(ActionServlet.class);
                }
            }
    );

    wac.refresh();
    return wac;
}
FactoryBeanTests.java 文件源码 项目:class-guard 阅读 28 收藏 0 点赞 0 评论 0
@Test
public void testFactoryBeansWithIntermediateFactoryBeanAutowiringFailure() throws Exception {
    DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(factory).loadBeanDefinitions(WITH_AUTOWIRING_CONTEXT);

    BeanFactoryPostProcessor ppc = (BeanFactoryPostProcessor) factory.getBean("propertyPlaceholderConfigurer");
    ppc.postProcessBeanFactory(factory);

    Beta beta = (Beta) factory.getBean("beta");
    Alpha alpha = (Alpha) factory.getBean("alpha");
    Gamma gamma = (Gamma) factory.getBean("gamma");
    assertSame(beta, alpha.getBeta());
    assertSame(gamma, beta.getGamma());
}
SakaiApplicationContext.java 文件源码 项目:sakai 阅读 23 收藏 0 点赞 0 评论 0
/**
 * Add bean-created post processors.
 * @param beanFactory
 */
public void invokePostProcessorCreators(ConfigurableListableBeanFactory beanFactory) {
    String[] postProcessorCreatorNames = beanFactory.getBeanNamesForType(BeanFactoryPostProcessorCreator.class, false, false);
    for (int i = 0; i < postProcessorCreatorNames.length; i++) {
        BeanFactoryPostProcessorCreator postProcessorCreator = (BeanFactoryPostProcessorCreator)beanFactory.getBean(postProcessorCreatorNames[i]);
        for (BeanFactoryPostProcessor beanFactoryPostProcessor : postProcessorCreator.getBeanFactoryPostProcessors()) {
            addBeanFactoryPostProcessor(beanFactoryPostProcessor);
        }
    }
}


问题


面经


文章

微信
公众号

扫码关注公众号