spring5(6) -DI 通过xml配置装配 -1

一. 通过XML配置装配的方式
 1.XML自动装配(不推荐)
 2.Setter注入
 3.构造器注入
 4.bean元素继承
 5.Property place holder
 6.注册案例
代码演示

(1)XML自动装配(不推荐)

使用autowire modes:no ,default, byName,byType,constructor







(2)Setter注入(常用)

1.简单类型(常量值)注入
  使用property,name ,value;





    
    
    
 
    

Employee类

package com.keen.xml.setter;

public class Employee {
   private String name;
   private int age;
   private double salary;
   public void setName(String name) {
       this.name = name;
   }
   public void setAge(int age) {
       this.age = age;
   }
   public void setSalary(double salary) {
       this.salary = salary;
   }
   @Override
   public String toString() {
       return "Employee [name=" + name + ", age=" + age + ", salary=" + salary + "]";
   }
   
}
2.对象类型注入(使用property 的ref 关联对象)
    


   


⚠️
 3.集合类型注入(使用对应集合元素 ,list>, ...)




  
     set1
     set2
  


  
    
      list1
      list2
    
  
 
 
   
     array1
     array2
   
 
 
 
 
 
 

 
 




   p1 = v1;
   p2 = v2;
   p3 = v3;





CollectionBean1类

package com.keen.xml.setter;

import java.util.*;

public class CollectionBean1 {
    private Set set;
    private List list;
    private String[] array ;
    private Map map;
    private Properties prop;
    public void setArray(String[] array) {
        this.array = array;
    }
    public void setMap(Map map) {
        this.map = map;
    }
    public void setProp(Properties prop) {
        this.prop = prop;
    }
    public void setSet(Set set) {
        this.set = set;
    }
    public void setList(List list) {
        this.list = list;
    }
    @Override
    public String toString() {
        return "CollectionBean1 [set=" + set + ", list=" + list + ", array=" + Arrays.toString(array) + ", map=" + map
                + ", prop=" + prop + "]";
    }
   
}

(3)构造器注入(了解)

<1>常量类型注入








Employee1类

public class Employee1 {
    private String name;
    private int age;
    private double salary;
    
    public Employee1(String name, int age, double salary) {
        super();
        this.name = name;
        this.age = age;
        this.salary = salary;
    }

    @Override
    public String toString() {
        return "Employee [name=" + name + ", age=" + age + ", salary=" + salary + "]";
    }

}

<2>对象类型注入(了解)


  



人类

package com.keen.xml.constructor;

public class Person1 {
    private Cat1 cat1;

    public Person1(Cat1 cat1) {
        super();
        this.cat1 = cat1;
    }

    @Override
    public String toString() {
        return "Person [cat=" + cat1 + "]";
    }

}

猫类

package com.keen.xml.constructor;

public class Cat1 {

}

<3>集合类型注入:

(1)将seeter注入的集合注入类型中的property改为constructor-arg即可
(2)将employee的setter方法改为带参数构造器即可





  
     set1
     set2
  


  
    
      list1
      list2
    
  
 
 
   
     array1
     array2
   
 
 
 
 
 
 

 
 




   p1 = v1;
   p2 = v2;
   p3 = v3;





添加:内部bean,外面无法访问


 

   




(4)bean元素的继承

 多个bean元素的共同配置部分的抽取,和Java的继承不同
 
 Java继承:把多个类共同的代码抽取到父类中
 bean元素的继承(inheritance):把多个bean元素共同的属性配置抽取到另外一个公用的bean元素中。

xml原本的配置











bean元素的继承后配置



   
   











你可能感兴趣的:(spring5(6) -DI 通过xml配置装配 -1)