spring开发_BeanPostProcessor_Bean后处理器

spring开发_BeanPostProcessor_Bean后处理器,第1张

概述java spring BeanPostProcessor Bean后处理器

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; 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,我很喜欢吃鱼肉

总结

以上是内存溢出为你收集整理的spring开发_BeanPostProcessor_Bean后处理器全部内容,希望文章能够帮你解决spring开发_BeanPostProcessor_Bean后处理器所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1270042.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存