加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

spring开发_BeanPostProcessor_Bean后处理器

发布时间:2020-12-15 01:52:34 所属栏目:大数据 来源:网络整理
导读: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

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; MeatService { String whatMeat(); }

com.b510.service.impl; com.b510.service.AnimalService; com.b510.service.MeatService; CatServiceBean AnimalService { age; MeatService meatService; CatServiceBean(){ System.out.println("猫类被初始化了"); } getAge() { age; } @Override getInfo() { System.out.println("我是猫,我的年龄是:"+age+",我很喜欢吃"+meatService.whatMeat()); } MeatService getMeatService() { meatService; } setAge( age) { .age = age; } setMeatService(MeatService meatService) { .meatService = meatService; } }

com.b510.service.impl; com.b510.service.AnimalService; com.b510.service.MeatService; DogServiceBean AnimalService { age; MeatService meatService; DogServiceBean() { System.out.println("狗类被初始化了"); } getAge() { age; } @Override getInfo() { System.out.println("我是狗,我的年龄是:" + age + ",我很喜欢吃" + meatService.whatMeat()); } MeatService getMeatService() { meatService; } setAge( age) { .age = age; } setMeatService(MeatService meatService) { .meatService = meatService; } }

com.b510.service.impl; com.b510.service.MeatService; FishServiceBean MeatService { FishServiceBean(){ System.out.println("鱼肉类被初始化了"); } @Override String whatMeat() { "鱼肉"; } }

com.b510.service.impl; com.b510.service.MeatService; PorkServiceBean MeatService { PorkServiceBean(){ System.out.println("猪肉类被初始化了"); } @Override String whatMeat() { "猪肉"; } }

xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http: ="com.b510.service.impl.FishServiceBean"> ="com.b510.service.impl.PorkServiceBean"> ="com.b510.service.impl.DogServiceBean" p:age="12" p:meatService-ref="meatServiceOfPork" /> ="com.b510.service.impl.CatServiceBean" p:age="3" p:meatService-ref="meatServiceOfFish" /> ="com.b510.app.util.AppBeanPostProcessor">

2012-3-12 15:41:10 org.springframework.context.support.AbstractApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@19c26f5: display name [org.springframework.context.support.ClassPathXmlApplicationContext@19c26f5]; startup date [Mon Mar 12 15:41:10 CST 2012]; root of context hierarchy 2012-3-12 15:41:10 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from path resource [beans.xml] 2012-3-12 15:41:13 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory 信息: Bean factory application context [org.springframework.context.support.ClassPathXmlApplicationContext@19c26f5]: org.springframework.beans.factory.support.DefaultListableBeanFactory@4a63d8 2012-3-12 15:41:13 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons 信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@4a63d8: defining beans [meatServiceOfFish,meatServiceOfPork,animaleServiceOfDog,animaleServiceOfCat,appBeanPostProcessor]; root of factory hierarchy 鱼肉类被初始化了 鱼肉Bean被后处理器初始化 postProcessAfterInitialization方法,被处理的Bean的名称为:meatServiceOfFish 猪肉类被初始化了 猪肉Bean被后处理器初始化 postProcessAfterInitialization方法,被处理的Bean的名称为:meatServiceOfPork 狗类被初始化了 狗类Bean被后处理器初始化 postProcessAfterInitialization方法,被处理的Bean的名称为:animaleServiceOfDog 猫类被初始化了 猫类Bean被后处理器初始化 postProcessAfterInitialization方法,被处理的Bean的名称为:animaleServiceOfCat 我是狗,我的年龄是:12,我很喜欢吃猪肉 我是猫,我的年龄是:3,我很喜欢吃鱼肉 *************手动注册BeanPostProcessor处理结果******************** 2012-3-12 15:41:13 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from path resource [beans.xml] 狗类被初始化了 猪肉类被初始化了 猪肉Bean被后处理器初始化 postProcessAfterInitialization方法,被处理的Bean的名称为:meatServiceOfPork 狗类Bean被后处理器初始化 postProcessAfterInitialization方法,被处理的Bean的名称为:animaleServiceOfDog 我是狗,我的年龄是:12,我很喜欢吃猪肉 猫类被初始化了 鱼肉类被初始化了 鱼肉Bean被后处理器初始化 postProcessAfterInitialization方法,被处理的Bean的名称为:meatServiceOfFish 猫类Bean被后处理器初始化 postProcessAfterInitialization方法,被处理的Bean的名称为:animaleServiceOfCat 我是猫,我的年龄是:3,我很喜欢吃鱼肉

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读