spring的DI注入和bean的继承

2021-07-01 20:03

阅读:490

标签:ons   构造方法   tin   lap   rop   art   instance   必须   不同的   

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


    
    bean id="demo" class="spring.Demo">

        property name="price" value="24"/>
        
        property name="name">
            value>value=""/]]>value>
        property>
        
        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>

    
    bean id="info2" class="spring.Info"  p:name="kity" p:weight="60"/>

    
    
    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 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 id="parent" class="spring.Info">
        property name="name" value="wuHan"/>
        property name="weight" value="4000"/>
        property name="height" value="2000"/>
    bean>
    
    bean id="guDao" parent="parent" p:name="guDao"/>
    

    
    
    
    
    bean id="abstract" abstract="true">
        property name="name" value="wuHan"/>
        property name="weight" value="4000"/>
        property name="height" value="2000"/>
    bean>
    
    bean id="imp" class="spring.Info" parent="abstract" p:name="guDao"/>
    


beans>

 

spring的DI注入和bean的继承

标签:ons   构造方法   tin   lap   rop   art   instance   必须   不同的   

原文地址:https://www.cnblogs.com/kill-9/p/9636409.html

上一篇:Servlet API

下一篇:python调用exe程序


评论


亲,登录后才可以留言!