代码改变世界

spring的DI注入和bean的继承

2018-09-12 18:05  kill-9  阅读(325)  评论(0编辑  收藏  举报
 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的配置文件 - --注入

<?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的配置文件 - --继承的实现

<?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>