DI依赖注入
DI依赖注入
- 依赖:bean对象的创建依赖于容器。
- 注入:bean对象中的所有属性由容器来注入。
1. 构造器注入
2. Set方式注入
要求被注入的属性 , 必须有set方法 , set方法的方法名由set + 属性首字母大写 , 如果属性是boolean类型 , 没有set方法 , 是 is .
测试pojo类 :
Address.java
 public class Address {
 
     private String address;
 
     public String getAddress() {
         return address;
    }
 
     public void setAddress(String address) {
         this.address = address;
    }
 }
Student.java
public class Student {
 
     private String name;
     private Address address;
     private String[] books;
     private List<String> hobbys;
     private Map<String,String> card;
     private Set<String> games;
     private String wife;
     private Properties info;
 
     public void setName(String name) {
         this.name = name;
    }
 
     public void setAddress(Address address) {
         this.address = address;
    }
 
     public void setBooks(String[] books) {
         this.books = books;
    }
 
     public void setHobbys(List<String> hobbys) {
         this.hobbys = hobbys;
    }
 
     public void setCard(Map<String, String> card) {
         this.card = card;
    }
 
     public void setGames(Set<String> games) {
         this.games = games;
    }
 
     public void setWife(String wife) {
         this.wife = wife;
    }
 
     public void setInfo(Properties info) {
         this.info = info;
    }
 
     public void show(){
         System.out.println("name="+ name
                 + ",address="+ address.getAddress()
                 + ",books="
        );
         for (String book:books){
             System.out.print(book+"\t");
        }
         System.out.println("\n爱好:"+hobbys);
 
         System.out.println("card:"+card);
 
         System.out.println("games:"+games);
 
         System.out.println("wife:"+wife);
 
         System.out.println("info:"+info);
 
    }
 }
1、常量注入
 <bean id="student" class="com.maple.pojo.Student">
     <property name="name" value="杜甫"/>
 </bean>
测试:
 @Test
 public void test01(){
     ApplicationContext context = newClassPathXmlApplicationContext("applicationContext.xml");
 
     Student student = (Student) context.getBean("student");
 
     System.out.println(student.getName());
 
 }
2、Bean注入
注意点:这里的值是一个引用,ref
 <bean id="addr" class="com.maple.pojo.Address">
     <property name="address" value="成都"/>
 </bean>
 
 <bean id="student" class="com.maple.pojo.Student">
     <property name="name" value="杜甫"/>
     <property name="address" ref="addr"/>
 </bean>
3、数组注入
	<bean id="student" class="com.maple.pojo.Student">
        <property name="name" value="李白"/>
        <!--这里的属性是另一个bean所以要使用ref-->
        <property name="address" ref="addr"/>
        <!--数组注入-->
        <property name="books">
            <!--基本数据类型可以省略value-type-->
            <array value-type="java.lang.String">
                <value>《西游记》</value>
                <value>《红楼梦》</value>
                <value>《水浒传》</value>
                <value>《三国演义》</value>
            </array>
        </property>
    </bean>
4、List注入
        <!--List注入-->
        <property name="hobbys">
            <list>
                <value>蹦迪</value>
                <value>蹦极</value>
                <value>酷跑</value>
                <value>攀岩</value>
                <value>饮酒</value>
                <value>作诗</value>
            </list>
        </property>
5、Map注入
 <property name="card">
     <map>
         <entry key="中国工商" value="1547896548462566"/>
         <entry key="中国邮政" value="1564774656164467"/>
     </map>
 </property>
6、set注入
 <property name="games">
     <set>
         <value>LOL</value>
         <value>COD16</value>
         <value>CS GO</value>
     </set>
 </property>
7、Null注入
 <property name="wife"><null/></property>
8、Properties注入
常用于数据库连接配置等的注入
 <property name="info">
     <props>
         <!--这里的值是写在<prop></prop>之间的而不是用value来指定-->
         <prop key="学号">20200110</prop>
         <prop key="性别">男</prop>
         <prop key="姓名">杜甫</prop>
     </props>
 </property>
测试结果
name=李白,address=成都,books=
《西游记》	《红楼梦》	《水浒传》	《三国演义》	
爱好:[蹦迪, 蹦极, 酷跑, 攀岩, 饮酒, 作诗]
card:{中国工商=1547896548462566, 中国邮政=1564774656164467}
games:[LOL, COD16, CS GO]
wife:null
info:{学号=20200110, 性别=男, 姓名=杜甫}
3. 拓展方式注入
p命名和c命名注入
User.java :【注意:这里没有有参构造器!】
 public class User {
     private String name;
     private int age;
 
     public void setName(String name) {
         this.name = name;
    }
 
     public void setAge(int age) {
         this.age = age;
    }
 
     @Override
     public String toString() {
         return "User{" +
                 "name='" + name + '\'' +
                 ", age=" + age +
                 '}';
    }
 }
1、P命名空间注入 : 需要在头文件中加入约束文件
导入约束 : xmlns:p="http://www.springframework.org/schema/p"
 <!--P(属性: properties)命名空间 , 属性依然要设置set方法-->
 <bean id="user" class="com.maple.pojo.User" p:name="枫叶" p:age="20"/>
2、c 命名空间注入 : 需要在头文件中加入约束文件
导入约束 : xmlns:c="http://www.springframework.org/schema/c"
<!--C(构造: Constructor)命名空间 , 属性依然要设置set方法-->
<bean id="user" class="com.maple.pojo.User" c:name="枫叶" c:age="18"/>
发现问题:爆红了,刚才我们没有写有参构造!
解决:把有参构造器加上,这里也能知道,c 就是所谓的构造器注入!
测试代码:
 @Test
 public void test02(){
     ApplicationContext context =newClassPathXmlApplicationContext("applicationContext.xml");
     User user = (User) context.getBean("user");
     System.out.println(user);
 }
 
                    
                
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号