Java Code Examples for org.springframework.beans.factory.config.BeanFactoryPostProcessor#postProcessBeanFactory()

The following examples show how to use org.springframework.beans.factory.config.BeanFactoryPostProcessor#postProcessBeanFactory() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BeanFactoryPostProcessorBootstrap.java    From spring-analysis-note with MIT License 6 votes vote down vote up
public static void main(String[] args) {
	ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("factory.bean/factory-post-processor.xml");
	BeanFactoryPostProcessor beanFactoryPostProcessor = (BeanFactoryPostProcessor) context.getBean("carPostProcessor");
	beanFactoryPostProcessor.postProcessBeanFactory(context.getBeanFactory());
	// 输出 :Car{maxSpeed=0, brand='*****', price=10000.0},敏感词被替换了
	System.out.println(context.getBean("car"));

	// 硬编码 后处理器执行时间
	BeanFactoryPostProcessor hardCodeBeanFactoryPostProcessor = new HardCodeBeanFactoryPostProcessor();
	context.addBeanFactoryPostProcessor(hardCodeBeanFactoryPostProcessor);
	// 更新上下文
	context.refresh();
	// 输出 :
	// Hard Code BeanFactory Post Processor execute time
	// Car{maxSpeed=0, brand='*****', price=10000.0}
	System.out.println(context.getBean("car"));
}
 
Example 2
Source File: FactoryBeanTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testFactoryBeansWithAutowiring() throws Exception {
	DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
	new XmlBeanDefinitionReader(factory).loadBeanDefinitions(WITH_AUTOWIRING_CONTEXT);

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

	assertNull(factory.getType("betaFactory"));

	Alpha alpha = (Alpha) factory.getBean("alpha");
	Beta beta = (Beta) factory.getBean("beta");
	Gamma gamma = (Gamma) factory.getBean("gamma");
	Gamma gamma2 = (Gamma) factory.getBean("gammaFactory");

	assertSame(beta, alpha.getBeta());
	assertSame(gamma, beta.getGamma());
	assertSame(gamma2, beta.getGamma());
	assertEquals("yourName", beta.getName());
}
 
Example 3
Source File: FactoryBeanTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testFactoryBeansWithAutowiring() throws Exception {
	DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
	new XmlBeanDefinitionReader(factory).loadBeanDefinitions(WITH_AUTOWIRING_CONTEXT);

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

	assertNull(factory.getType("betaFactory"));

	Alpha alpha = (Alpha) factory.getBean("alpha");
	Beta beta = (Beta) factory.getBean("beta");
	Gamma gamma = (Gamma) factory.getBean("gamma");
	Gamma gamma2 = (Gamma) factory.getBean("gammaFactory");

	assertSame(beta, alpha.getBeta());
	assertSame(gamma, beta.getGamma());
	assertSame(gamma2, beta.getGamma());
	assertEquals("yourName", beta.getName());
}
 
Example 4
Source File: FactoryBeanTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testFactoryBeansWithAutowiring() throws Exception {
	DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
	new XmlBeanDefinitionReader(factory).loadBeanDefinitions(WITH_AUTOWIRING_CONTEXT);

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

	assertNull(factory.getType("betaFactory"));

	Alpha alpha = (Alpha) factory.getBean("alpha");
	Beta beta = (Beta) factory.getBean("beta");
	Gamma gamma = (Gamma) factory.getBean("gamma");
	Gamma gamma2 = (Gamma) factory.getBean("gammaFactory");

	assertSame(beta, alpha.getBeta());
	assertSame(gamma, beta.getGamma());
	assertSame(gamma2, beta.getGamma());
	assertEquals("yourName", beta.getName());
}
 
Example 5
Source File: BeansConfigurationHelper.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
public static AbstractEngineConfiguration parseEngineConfiguration(Resource springResource, String beanName) {
    DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
    XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
    xmlBeanDefinitionReader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_XSD);
    xmlBeanDefinitionReader.loadBeanDefinitions(springResource);

    // Check non singleton beans for types
    // Do not eagerly initialize FactorBeans when getting BeanFactoryPostProcessor beans
    Collection<BeanFactoryPostProcessor> factoryPostProcessors = beanFactory.getBeansOfType(BeanFactoryPostProcessor.class, true, false).values();
    if (factoryPostProcessors.isEmpty()) {
        factoryPostProcessors = Collections.singleton(new PropertyPlaceholderConfigurer());
    }
    for (BeanFactoryPostProcessor factoryPostProcessor : factoryPostProcessors) {
        factoryPostProcessor.postProcessBeanFactory(beanFactory);
    }

    AbstractEngineConfiguration engineConfiguration = (AbstractEngineConfiguration) beanFactory.getBean(beanName);
    engineConfiguration.setBeans(new SpringBeanFactoryProxyMap(beanFactory));
    return engineConfiguration;
}
 
Example 6
Source File: BeansConfigurationHelper.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
public static ProcessEngineConfiguration parseProcessEngineConfiguration(Resource springResource, String beanName) {
    DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
    XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
    xmlBeanDefinitionReader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_XSD);
    xmlBeanDefinitionReader.loadBeanDefinitions(springResource);

    // Check non singleton beans for types
    // Do not eagerly initialize FactorBeans when getting BeanFactoryPostProcessor beans
    Collection<BeanFactoryPostProcessor> factoryPostProcessors = beanFactory.getBeansOfType(BeanFactoryPostProcessor.class, true, false).values();
    if (factoryPostProcessors.isEmpty()) {
        factoryPostProcessors = Collections.singleton(new PropertyPlaceholderConfigurer());
    }
    for (BeanFactoryPostProcessor factoryPostProcessor : factoryPostProcessors) {
        factoryPostProcessor.postProcessBeanFactory(beanFactory);
    }

    ProcessEngineConfigurationImpl processEngineConfiguration = (ProcessEngineConfigurationImpl) beanFactory.getBean(beanName);
    processEngineConfiguration.setBeans(new SpringBeanFactoryProxyMap(beanFactory));
    return processEngineConfiguration;
}
 
Example 7
Source File: PostProcessorRegistrationDelegate.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Invoke the given BeanFactoryPostProcessor beans.
 */
private static void invokeBeanFactoryPostProcessors(
		Collection<? extends BeanFactoryPostProcessor> postProcessors, ConfigurableListableBeanFactory beanFactory) {

	for (BeanFactoryPostProcessor postProcessor : postProcessors) {
		postProcessor.postProcessBeanFactory(beanFactory);
	}
}
 
Example 8
Source File: FactoryBeanTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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());
}
 
Example 9
Source File: PostProcessorRegistrationDelegate.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Invoke the given BeanFactoryPostProcessor beans.
 */
private static void invokeBeanFactoryPostProcessors(
		Collection<? extends BeanFactoryPostProcessor> postProcessors, ConfigurableListableBeanFactory beanFactory) {

	for (BeanFactoryPostProcessor postProcessor : postProcessors) {
		postProcessor.postProcessBeanFactory(beanFactory);
	}
}
 
Example 10
Source File: FactoryBeanTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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());
}
 
Example 11
Source File: PostProcessorRegistrationDelegate.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Invoke the given BeanFactoryPostProcessor beans.
 */
private static void invokeBeanFactoryPostProcessors(
		Collection<? extends BeanFactoryPostProcessor> postProcessors, ConfigurableListableBeanFactory beanFactory) {

	for (BeanFactoryPostProcessor postProcessor : postProcessors) {
		postProcessor.postProcessBeanFactory(beanFactory);
	}
}
 
Example 12
Source File: PostProcessorRegistrationDelegate.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Invoke the given BeanFactoryPostProcessor beans.
 */
private static void invokeBeanFactoryPostProcessors(
		Collection<? extends BeanFactoryPostProcessor> postProcessors, ConfigurableListableBeanFactory beanFactory) {

	for (BeanFactoryPostProcessor postProcessor : postProcessors) {
		postProcessor.postProcessBeanFactory(beanFactory);
	}
}
 
Example 13
Source File: FactoryBeanTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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());
}