href="http://www.cnblogs.com/hongten/gallery/image/112581.HTML">http://www.cnblogs.com/hongten/gallery/image/112581.HTML
com.b510.app.test; org.springframework.beans.factory.xml.Xmlbeanfactory; org.springframework.context.ApplicationContext; org.springframework.context.support.ClasspathXmlApplicationContext; org.springframework.core.io.ClassPathResource; com.b510.app.util.AppBeanPostProcessor; com.b510.service.AnimalService; SpringTest { main(String[] args) { ApplicationContext act = ClasspathXmlApplicationContext("beans.xml"); AnimalService animalServiceOfDog = (AnimalService) act .getBean("animaleServiceOfDog"); animalServiceOfDog.getInfo(); AnimalService animalServiceOfCat = (AnimalService) act .getBean("animaleServiceOfCat"); animalServiceOfCat.getInfo(); System.out .println("*************手动注册BeanPostProcessor处理结果********************"); registerManually(); } registerManually() { ClassPathResource isr = ClassPathResource("beans.xml"); Xmlbeanfactory factory = Xmlbeanfactory(isr); AppBeanPostProcessor prr = (AppBeanPostProcessor) factory.getBean( "appBeanPostProcessor",AppBeanPostProcessor.); factory.addBeanPostProcessor(prr); AnimalService animalServiceOfDog = (AnimalService) factory .getBean("animaleServiceOfDog"); animalServiceOfDog.getInfo(); AnimalService animalServiceOfCat = (AnimalService) factory .getBean("animaleServiceOfCat"); animalServiceOfCat.getInfo(); } }
com.b510.app.util; org.springframework.beans.BeansException; org.springframework.beans.factory.config.BeanPostProcessor; com.b510.service.impl.CatServiceBean; com.b510.service.impl.DogServiceBean; com.b510.service.impl.FishServiceBean; com.b510.service.impl.PorkServiceBean; AppBeanPostProcessor BeanPostProcessor { @OverrIDe Object postProcessAfterInitialization(Object bean,String beanname) BeansException { System.out.println("postProcessAfterInitialization方法,被处理的Bean的名称为:" + beanname); bean; } @OverrIDe Object postProcessBeforeInitialization(Object bean,String beanname) BeansException { (bean FishServiceBean) { System.out.println("鱼肉Bean被后处理器初始化"); } (bean PorkServiceBean) { System.out.println("猪肉Bean被后处理器初始化"); } (bean DogServiceBean) { System.out.println("狗类Bean被后处理器初始化"); } (bean CatServiceBean) { System.out.println("猫类Bean被后处理器初始化"); } bean; } }
com.b510.service; AnimalService { getInfo(); }
com.b510.service;