第五章第四节-bean的关系

来源:互联网 发布:linux shell 控制语句 编辑:程序博客网 时间:2024/05/01 14:34

        首先按照HelloWorld的方式创建一个普通的java工程,引入的jar与HelloWorld的一样。先简单介绍一下项目中涉及到内容。两个bean类,其中House是Person的成员。一个MainTest测试类。一个spring.xml配置文件。末尾有完整的代码。

一、引用Bean

        关于两个bean类和MainTest类不再做介绍,详细查看后面的完整代码。这里重点介绍配置文件的内容。

        第一个是关于引用bean的。在下面的配置示例中,person通过ref引用了一个id为house的bean,那么在初始化person时会将house注入到person的成员变量中去。

        这种引用的方式是支持级联属性配置,也就是说,可以在引用house之后在person中单独设置house的属性,如案例中house.area一样。

        配置中有一个注释的<null/>,它表示为house赋一个null的空值,一般默认没有给引用赋值的也是空值。

<!-- 引用Bean --><bean id="person" class="com.escore.beans.Person"><property name="name" value="齐天大圣"></property><property name="house" ref="house"></property><property name="house.area" value="200"></property><!-- 设置引用类型的null值,一般不设置默认也是null。<property name="house"><null/></property> --></bean><bean id="house" class="com.escore.beans.House"><property name="hName" value="水帘洞"></property></bean>
二、内部bean
        关于内部bean比较简单,就是在一个bean属性的内部,创建其成员变量的bean。如实例中创建的house,需要注意的是内部bean不需要id属性,它是不能被外面所访问到的。

<bean id="person2" class="com.escore.beans.Person"><property name="name" value="托塔李天王"></property><property name="house"><bean class="com.escore.beans.House"><property name="hName" value="玲珑宝塔"></property><property name="area" value="8"></property></bean></property></bean>
三、继承bean

         继承bean是通过parent属性指向一个bean的id,即可以继承parent类的属性,而且子类不需要在添加class属性。

         子类也可以再赋予自己的值来覆盖父类的复制。

         子类也可以添加class属性,但是class必须是父类或者父类的子类才行。如果子类有额外的属性也可以再赋值。

<bean id="person3" class="com.escore.beans.Person"><property name="name" value="雨蛙娘娘"></property><property name="house" ref="house"></property></bean><bean id="person4" parent="person3"><property name="name" value="亚当"></property></bean>
四、继承抽象bean

         抽象bean是将abstract属性设为true,抽象的bean是不能被实例化的。class属性也可以不写,但是继承它的子类必须写(如果抽象类注明了class,子类也可以不写)。其他的特性与继承普通的bean一样。

<bean id="person5" abstract="true"><property name="name" value="name"></property><property name="house"><bean class="com.escore.beans.House"><property name="hName" value="houseName"></property><property name="area" value="0"></property></bean></property></bean><bean id="person6" class="com.escore.beans.Person" parent="person5"><property name="name" value="夏娃"></property></bean>
五、依赖bean

        使用depends-on设置依赖的bean的id,如果需要设置多个依赖可以使用逗号、空格的方式。但是依赖的bean不能是抽象的bean。

        设置的依赖属性,只有依赖作用个。不管所依赖的那个bean是否为本类自己的成员,都不会将依赖的备案注入到自己的属性中。

<!-- 依赖bean:使用depends-on设置依赖的前置bean。-如果前置依赖于多个 Bean,则可以通过逗号,空格或的方式配置 Bean 的名称  -使用depends-on属性关联依赖的bean,不能关联抽象的bean。  -只是依赖作用,该类有没有所依赖的对象的成员都无关,即使有也不会为其初始化。 --><bean id="person7" class="com.escore.beans.Person"depends-on="house2"></bean><bean id="house2" class="com.escore.beans.House"><property name="hName" value="安置房"></property><property name="area" value="50"></property></bean>

-------------------------------本节涉及到的完整代码--------------------------------------------------
House.java

public class House {private String hName;private double area;public double getArea() {return area;}public void setArea(double area) {this.area = area;}public String gethName() {return hName;}public void sethName(String hName) {this.hName = hName;}@Overridepublic String toString() {return "House [hName=" + hName + ", area=" + area + "]";}}
Person.java

public class Person {private String name;private House house;public String getName() {return name;}public void setName(String name) {this.name = name;}public House getHouse() {return house;}public void setHouse(House house) {this.house = house;}@Overridepublic String toString() {return "Person [name=" + name + ", house=" + house + "]";}}

MainTest.java

import org.springframework.context.support.ClassPathXmlApplicationContext;import com.escore.beans.Person;public class MainTest {public static void main(String[] args) {ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");Person person = (Person) ctx.getBean("person6");System.out.println(person);ctx.close();}}
spring.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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">    <!-- 引用Bean --><bean id="person" class="com.escore.beans.Person"><property name="name" value="齐天大圣"></property><property name="house" ref="house"></property><property name="house.area" value="200"></property><!-- 设置引用类型的null值,一般不设置默认也是null。<property name="house"><null/></property> --></bean><bean id="house" class="com.escore.beans.House"><property name="hName" value="水帘洞"></property></bean><!-- 内部Bean --><bean id="person2" class="com.escore.beans.Person"><property name="name" value="托塔李天王"></property><property name="house"><bean class="com.escore.beans.House"><property name="hName" value="玲珑宝塔"></property><property name="area" value="8"></property></bean></property></bean><!-- 继承Bean: --><bean id="person3" class="com.escore.beans.Person"><property name="name" value="雨蛙娘娘"></property><property name="house" ref="house"></property></bean><bean id="person4" parent="person3" class="com.escore.beans.Man"><property name="name" value="亚当"></property><property name="gender" value="male"></property></bean><!-- 继承抽象Bean --><bean id="person5" abstract="true"><property name="name" value="name"></property><property name="house"><bean class="com.escore.beans.House"><property name="hName" value="houseName"></property><property name="area" value="0"></property></bean></property></bean><bean id="person6" class="com.escore.beans.Person"  parent="person5"><property name="name" value="夏娃"></property></bean><!-- 依赖bean --><bean id="person7" class="com.escore.beans.Person"depends-on="house2"></bean><bean id="house2" class="com.escore.beans.House"><property name="hName" value="安置房"></property><property name="area" value="50"></property></bean></beans>





0 0