1 16 17 package org.springframework.beans.factory.config; 18 19 import junit.framework.TestCase; 20 21 import org.springframework.beans.MutablePropertyValues; 22 import org.springframework.beans.TestBean; 23 import org.springframework.beans.factory.support.DefaultListableBeanFactory; 24 import org.springframework.beans.factory.support.RootBeanDefinition; 25 import org.springframework.context.support.StaticApplicationContext; 26 27 32 public class BeanFactoryPostProcessorTests extends TestCase { 33 34 public void testRegisteredBeanFactoryPostProcessor() { 35 StaticApplicationContext ac = new StaticApplicationContext(); 36 ac.registerSingleton("tb1", TestBean.class); 37 ac.registerSingleton("tb2", TestBean.class); 38 TestBeanFactoryPostProcessor bfpp = new TestBeanFactoryPostProcessor(); 39 ac.addBeanFactoryPostProcessor(bfpp); 40 assertFalse(bfpp.wasCalled); 41 ac.refresh(); 42 assertTrue(bfpp.wasCalled); 43 } 44 45 public void testDefinedBeanFactoryPostProcessor() { 46 StaticApplicationContext ac = new StaticApplicationContext(); 47 ac.registerSingleton("tb1", TestBean.class); 48 ac.registerSingleton("tb2", TestBean.class); 49 ac.registerSingleton("bfpp", TestBeanFactoryPostProcessor.class); 50 ac.refresh(); 51 TestBeanFactoryPostProcessor bfpp = (TestBeanFactoryPostProcessor) ac.getBean("bfpp"); 52 assertTrue(bfpp.wasCalled); 53 } 54 55 public void testMultipleDefinedBeanFactoryPostProcessors() { 56 StaticApplicationContext ac = new StaticApplicationContext(); 57 ac.registerSingleton("tb1", TestBean.class); 58 ac.registerSingleton("tb2", TestBean.class); 59 MutablePropertyValues pvs1 = new MutablePropertyValues(); 60 pvs1.addPropertyValue("initValue", "${key}"); 61 ac.registerSingleton("bfpp1", TestBeanFactoryPostProcessor.class, pvs1); 62 MutablePropertyValues pvs2 = new MutablePropertyValues(); 63 pvs2.addPropertyValue("properties", "key=value"); 64 ac.registerSingleton("bfpp2", PropertyPlaceholderConfigurer.class, pvs2); 65 ac.refresh(); 66 TestBeanFactoryPostProcessor bfpp = (TestBeanFactoryPostProcessor) ac.getBean("bfpp1"); 67 assertEquals("value", bfpp.initValue); 68 assertTrue(bfpp.wasCalled); 69 } 70 71 public void testBeanFactoryPostProcessorNotExecutedByBeanFactory() { 72 DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); 73 bf.registerBeanDefinition("tb1", new RootBeanDefinition(TestBean.class)); 74 bf.registerBeanDefinition("tb2", new RootBeanDefinition(TestBean.class)); 75 bf.registerBeanDefinition("bfpp", new RootBeanDefinition(TestBeanFactoryPostProcessor.class)); 76 TestBeanFactoryPostProcessor bfpp = (TestBeanFactoryPostProcessor) bf.getBean("bfpp"); 77 assertFalse(bfpp.wasCalled); 78 } 79 80 81 public static class TestBeanFactoryPostProcessor implements BeanFactoryPostProcessor { 82 83 public String initValue; 84 85 public void setInitValue(String initValue) { 86 this.initValue = initValue; 87 } 88 89 public boolean wasCalled = false; 90 91 public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) { 92 wasCalled = true; 93 } 94 } 95 96 } 97 | Popular Tags |