gallery/image/112611.html">http://www.cnblogs.com/hongten/gallery/image/112611.html
com.b510.app.test; org.springframework.context.ApplicationContext; org.springframework.context.support.ClasspathXmlApplicationContext; com.b510.service.AnimalService; SpringTest { main(String[] args) { ApplicationContext act = ClasspathXmlApplicationContext("beans.xml"); 获取id为animaleServiceOfDog的Bean AnimalService animalServiceOfDog = (AnimalService) act .getBean("animaleServiceOfDog"); animalServiceOfDog.getInfo(); 获取id为animaleServiceOfCat的Bean AnimalService animalServiceOfCat = (AnimalService) act .getBean("animaleServiceOfCat"); animalServiceOfCat.getInfo(); } }
beanfactoryPostProcessor.java
com.b510.app.util; org.springframework.beans.BeansException; org.springframework.beans.factory.config.beanfactoryPostProcessor; org.springframework.beans.factory.config.ConfigurableListablebeanfactory;beanfactoryPostProcessor接口 * * MybeanfactoryPostProcessor beanfactoryPostProcessor {方法,对Spring进行后处理。 * * arambeanfactory * spring容器本身 postProcessbeanfactory( ConfigurableListablebeanfactory beanfactory) BeansException { System.out.println("比较一下上面信息********************"); System.out.println("spring容器是:" + beanfactory); System.out.println("比较一下下面信息********************"); }}
com.b510.service; AnimalService { 获取相关信息 getInfo(); }
com.b510.service; MeatService { 方法whatMeat 一个字符串 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() { "猪肉"; } }
2012-3-12 20:28:20 org.springframework.context.support.AbstractApplicationContext prepareRefresh信息: Refreshing org.springframework.context.support.ClasspathXmlApplicationContext@15eb0a9: display name [org.springframework.context.support.ClasspathXmlApplicationContext@15eb0a9]; startup date [Mon Mar 12 20:28:20 CST 2012]; root of context hierarchy2012-3-12 20:28:20 org.springframework.beans.factory.xml.XmlBeanDeFinitionReader loadBeanDeFinitions信息: Loading XML bean deFinitions from path resource [beans.xml]2012-3-12 20:28:24 org.springframework.context.support.AbstractApplicationContext obtainFreshbeanfactory信息: Bean factory application context [org.springframework.context.support.ClasspathXmlApplicationContext@15eb0a9]: org.springframework.beans.factory.support.DefaultListablebeanfactory@4a63d8比较一下上面信息********************spring容器是:org.springframework.beans.factory.support.DefaultListablebeanfactory@4a63d8: defining beans [meatServiceOfFish,meatServiceOfPork,animaleServiceOfDog,animaleServiceOfCat,appBeanPostProcessor]; root of factory hierarchy比较一下下面信息********************2012-3-12 20:28:24 org.springframework.beans.factory.support.DefaultListablebeanfactory preInstantiateSingletons信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListablebeanfactory@4a63d8: defining beans [meatServiceOfFish,appBeanPostProcessor]; root of factory hierarchy鱼肉类被初始化了猪肉类被初始化了狗类被初始化了猫类被初始化了我是狗,我的年龄是:12,我很喜欢吃猪肉我是猫,我的年龄是:3,我很喜欢吃鱼肉
内容和系统输出的信息,连个是相同的。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。