gallery/image/112563.html">http://www.cnblogs.com/hongten/gallery/image/112563.html
名称空间/src/com/b510/app/test/SpringTest.java
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(); } }
名称空间/src/com/b510/service/AnimalService.java
com.b510.service; AnimalService { 获取相关信息 getInfo(); }
名称空间/src/com/b510/service/MeatService.java
com.b510.service; MeatService { 方法whatMeat 一个字符串 String whatMeat(); }
名称空间/src/com/b510/service/impl/CatServiceBean.java
com.b510.service.impl; com.b510.service.AnimalService; com.b510.service.MeatService; CatServiceBean AnimalService { age; MeatService meatService; getAge() { age; } setAge( age) { .age = age; } @Override getInfo() { System.out.println("我是猫,我的年龄是:"+age+",我很喜欢吃"+meatService.whatMeat()); } MeatService getMeatService() { meatService; } setMeatService(MeatService meatService) { .meatService = meatService; } }
名称空间/src/com/b510/service/impl/DogServiceBean.java
com.b510.service.impl; com.b510.service.AnimalService; com.b510.service.MeatService; DogServiceBean AnimalService { age; MeatService meatService; MeatService getMeatService() { meatService; } setMeatService(MeatService meatService) { .meatService = meatService; } getAge() { age; } setAge( age) { .age = age; } @Override getInfo() { System.out.println("我是狗,我的年龄是:" + age + ",我很喜欢吃" + meatService.whatMeat()); } }
名称空间/src/com/b510/service/impl/FishServiceBean.java
com.b510.service.impl; com.b510.service.MeatService; FishServiceBean MeatService { @Override String whatMeat() { "鱼肉"; } }
名称空间/src/com/b510/service/impl/PorkServiceBean.java
com.b510.service.impl; com.b510.service.MeatService; PorkServiceBean MeatService { @Override String whatMeat() { "猪肉"; } }
名称空间/src/beans.xml
名称空间没有标准的XML格式灵活,如果某个Bean的属性名称是以"-ref"结尾的,那么采用p名称空间定义是就会出现错误,采用标准的XML格式是
错误滴!!!
<div class="cnblogs_code">
2012-3-12 12:58:51 org.springframework.context.support.AbstractApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.ClasspathXmlApplicationContext@c1b531: display name [org.springframework.context.support.ClasspathXmlApplicationContext@c1b531]; startup date [Mon Mar 12 12:58:51 CST 2012]; root of context hierarchy 2012-3-12 12:58:51 org.springframework.beans.factory.xml.XmlBeanDeFinitionReader loadBeanDeFinitions 信息: Loading XML bean deFinitions from path resource [beans.xml] 2012-3-12 12:58:54 org.springframework.context.support.AbstractApplicationContext obtainFreshbeanfactory 信息: Bean factory application context [org.springframework.context.support.ClasspathXmlApplicationContext@c1b531]: org.springframework.beans.factory.support.DefaultListablebeanfactory@111a775 2012-3-12 12:58:54 org.springframework.beans.factory.support.DefaultListablebeanfactory preInstantiateSingletons 信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListablebeanfactory@111a775: defining beans [meatServiceOfFish,meatServiceOfPork,animaleServiceOfDog,animaleServiceOfCat]; root of factory hierarchy 我是狗,我的年龄是:12,我很喜欢吃猪肉 我是猫,我的年龄是:3,我很喜欢吃鱼肉