微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

spring开发_使用p名称空间配置属性

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

xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http:spring-beans-3.0.xsd"> ="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" />

名称空间没有标准的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,我很喜欢吃鱼肉

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐