林炳文Evankaka原创作品。转载请注明出处http://blog.csdn.net/evankaka
本文主要讲了Spring中Bean之间的关系,分为继承、依赖和引用三个类型。文章中都分别有例子和用法分析。
使用案例:
定义House.java:
package com.mucfc; public class House { private String houseSize; private String housePosition; private String housePrice; public String getHouseSize() { return houseSize; } public void setHouseSize(String houseSize) { this.houseSize = houseSize; } public String getHousePosition() { return housePosition; } public void setHousePosition(String housePosition) { this.housePosition = housePosition; } public String getHousePrice() { return housePrice; } public void setHousePrice(String housePrice) { this.housePrice = housePrice; } public String toString(){ return "房子大小:"+houseSize+" 房子位置:"+housePosition+" 房子价格:"+housePrice; } }
如果多个bean存在相同的配置信息,Spring允许我们定义一个父,子将自动继承父的配置信息。
<!-- 定义抽象bean --> <bean id="abstracthouse" class="com.mucfc.House" p:houseSize="150坪" p:housePosition="科苑花园" p:housePrice="15万"/> <!-- 继承于abstracthouse --> <bean id="house2" parent="abstracthouse" p:housePosition="汇景苑"/> <!-- 继承于abstracthouse --> <bean id="house3" parent="abstracthouse" p:housePrice="8万"/>使用:
House house5=applicationContext.getBean("house2",House.class); House house6=applicationContext.getBean("house3",House.class); System.out.println(house5); System.out.println(house6);输出结果:
房子大小:150坪 房子位置:汇景苑 房子价格:15万
房子大小:150坪 房子位置:科苑花园 房子价格:8万
house1跟house2都继承自abstracthouse,Spring会将父bean的配置信息传递给子bean,如果子bean提供了父bean已有的配置信息,那么子bean的会覆盖父bean的.
父bean的功能主要是为了简化子bean的配置,所以一般声明为abstract=“true”,表示这个不实例化为一个对应的Bean,如果用户不指定该属性为true,那么IOC容器会实例化一个名叫abstractcar的Bean。
<bean id="buyHouser" class="com.mucfc.depend.BuyHouser" /> <bean id="HouseAgent" class="com.mucfc.depend.HouseAgent" depends-on="buyHouser" />
<bean id="HouseAgent" class="com.mucfc.depend.HouseAgent" init-method="initHouseAgent" depends-on="buyHouser,sellHouser" />例子中使用了‘depends-on‘来表达对多个bean的依赖。
<bean id="buyHouser" class="com.mucfc.depend.BuyHouser" lazy-init="true"/> <bean id="sellHouser" class="com.mucfc.depend.SellHouser" />当ApplicationContext实现加载上述配置时,设置为lazy的bean-buyHouser将不会在ApplicationContext启动时提前被实例化,而not.lazy--sellHouser却会被提前实例化。
package com.mucfc.depend; public class BuyHouser { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public void initBuyHouser(){ System.out.println("初始化了BuyHouser"); } }2、卖房人SellHouser.java
package com.mucfc.depend; public class SellHouser { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public void initSellHouser (){ System.out.println("初始化了SellHouser "); } }3、房屋中介HouseAgent.java
package com.mucfc.depend; public class HouseAgent { private BuyHouser buyHouser; private SellHouser sellHouser; public BuyHouser getBuyHouser() { return buyHouser; } public void setBuyHouser(BuyHouser buyHouser) { this.buyHouser = buyHouser; } public SellHouser getSellHouser() { return sellHouser; } public void setSellHouser(SellHouser sellHouser) { this.sellHouser = sellHouser; } public void initHouseAgent(){ System.out.println("初始化了HouseAgent"); } }4、beans.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <bean id="houseAgent" class="com.mucfc.depend.HouseAgent" init-method="initHouseAgent" depends-on="buyHouser,sellHouser" /> <bean id="buyHouser" class="com.mucfc.depend.BuyHouser" init-method="initBuyHouser" lazy-init="true"/> <bean id="sellHouser" class="com.mucfc.depend.SellHouser" init-method="initSellHouser" lazy-init="true"/> </beans>5、测试
package com.mucfc.depend; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml"); } }结果:
<bean id="houseAgent1" class="com.mucfc.depend.HouseAgent" p:buyHouser="buyHouser" p:sellHouser="sellHouser"/> <bean id="houseAgent2" class="com.mucfc.depend.HouseAgent" p:buyHouser-ref="buyHouser" p:sellHouser-ref="sellHouser"/>或写成
<bean id = "houseAgent3" class = "com.mucfc.depend.HouseAgent" > <property name="buyHouser"> <ref bean="buyHouser"/> </property> <property name="sellHouser"> <ref local="sellHouser"/> </property> </bean><ref local="xx"/>
原文地址:http://blog.csdn.net/evankaka/article/details/45023835