spring的DI注入和bean的繼承

 1 package spring;  2 
 3 public class Info {  4 
 5     private int weight;  6     private int height;  7     private String name;  8 
 9     public int getWeight() { 10         return weight; 11  } 12 
13     public void setWeight(int weight) { 14         this.weight = weight; 15  } 16 
17     public int getHeight() { 18         return height; 19  } 20 
21     public void setHeight(int height) { 22         this.height = height; 23  } 24 
25     public String getName() { 26         return name; 27  } 28 
29     public void setName(String name) { 30         this.name = name; 31  } 32 
33  @Override 34     public String toString() { 35         return "Info{" +
36                 "weight=" + weight +
37                 ", height=" + height +
38                 ", name='" + name + '\'' +
39                 '}'; 40  } 41 }

 1 package spring;  2 
 3 
 4 import org.springframework.context.ApplicationContext;  5 import org.springframework.context.support.ClassPathXmlApplicationContext;  6 
 7 public class Demo {  8 
 9 
10     private String name; 11     private Double price; 12     private Info info; 13 
14     public Info getInfo() { 15         return info; 16  } 17 
18     public void setInfo(Info info) { 19         this.info = info; 20  } 21 
22     public Demo(String name, Double price) { 23         this.name = name; 24         this.price = price; 25  } 26 
27     public Demo() { 28 
29  } 30 
31     public String getName() { 32         return name; 33  } 34 
35     public void setName(String name) { 36         this.name = name; 37  } 38 
39     public Double getPrice() { 40         return price; 41  } 42 
43     public void setPrice(Double price) { 44         this.price = price; 45  } 46 
47  @Override 48     public String toString() { 49         return "Demo{" +
50                 "name='" + name + '\'' +
51                 ", price=" + price +
52                 ", info=" + info +
53                 '}'; 54  } 55 // @Test 56 // public void run(){ 57 //
58 // ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); 59 // System.out.println(ac); 60 // }
61 
62     public static void main(String[] args){ 63         ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); 64         Demo demo = ac.getBean("demo",Demo.class); 65  System.out.println(demo); 66 
67         Demo demo2 = ac.getBean("demo2",Demo.class); 68  System.out.println(demo2); 69 
70         Info info = ac.getBean("info2",Info.class); 71  System.out.println(info); 72 
73         System.out.println("-------------------"); 74         Info info2 = ac.getBean("parent",Info.class); 75  System.out.println(info2); 76  } 77 }

spring的配置文件 - --注入java

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


    <!--DI 普通方法的屬性值注入-->
    <bean id="demo" class="spring.Demo">

        <property name="price" value="24"/>
        <!--特殊值注入-->
        <property name="name">
            <value><![CDATA[value="<林小曼>"/]]></value>
        </property>
        <!--引用外部bean-->
        <property name="info" ref="info"/>
    </bean>

    <bean id="info" class="spring.Info">
        <property name="name" value="Tom"/>
        <property name="height" value="10"/>
        <property name="weight" value="10"/>
    </bean>

    <!--使用p命名空間配置-->
    <bean id="info2" class="spring.Info"  p:name="kity" p:weight="60"/>

    <!--DI 構造方法的屬性值注入-->
    <!--index和type用來區分不一樣的構造方法-->
    <bean id="demo2" class="spring.Demo">
        <constructor-arg name="name" value="封起來" index="0" type="java.lang.String"/>
        <constructor-arg name="price" value="20" index="1" type="java.lang.Double"/>
        <property name="info">
            <!--內部bean-->
            <bean class="spring.Info">
                <property name="name" value="Lucy"/>
                <property name="weight" value="20"/>
            </bean>
        </property>
    </bean>

</beans>

 spring的配置文件 - --繼承的實現spring

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

    <!--實例bean的繼承-->
    <!--start-->
    <bean id="parent" class="spring.Info">
        <property name="name" value="wuHan"/>
        <property name="weight" value="4000"/>
        <property name="height" value="2000"/>
    </bean>
    <!--繼承了一個實例bean,只須要修改須要改動的配置就能夠了-->
    <bean id="guDao" parent="parent" p:name="guDao"/>
    <!--end-->

    <!--抽象bean的繼承-->
    <!--抽象bean的abstract必須是true,能夠不配置class屬性,固然,若是須要,也能夠配置class-->
    <!--抽象bean在spring容器中不會被實例化-->
    <!--start-->
    <bean id="abstract" abstract="true">
        <property name="name" value="wuHan"/>
        <property name="weight" value="4000"/>
        <property name="height" value="2000"/>
    </bean>
    <!--繼承了一個抽象bean,修改須要改動的配置,指定class-->
    <bean id="imp" class="spring.Info" parent="abstract" p:name="guDao"/>
    <!--end-->


</beans>
相關文章
相關標籤/搜索