依赖注入

6. 依赖注入

6.1 概念

  • 依赖注入(Dependency Injection,DI)。
  • 依赖 : 指Bean对象的创建依赖于容器 . Bean对象的依赖资源 .
  • 注入 : 指Bean对象所依赖的资源 , 由容器来设置和装配 .

6.2 构造器注入

我们在之前的案例已经讲过了(有参构造)

6.3 Set 注入 (重点)

要求被注入的属性 , 必须有set方法 , set方法的方法名由set + 属性首字母大写 , 如果属性是boolean类型 , 没有set方法 , 是 is Xxx

测试pojo类 :

Address.java

 public class Address {
 
     private String address;
 
     public String getAddress() {
         return address;
    }
 
     public void setAddress(String address) {
         this.address = address;
    }
 }

Student.java

定义多个不同类型的属性 来实现不同的注入方式

 package com.kuang.pojo;
 
 import java.util.List;
 import java.util.Map;
 import java.util.Properties;
 import java.util.Set;
 
 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.yhn.domain.Student">
       <property name="name" value="yhn"></property>
</bean>

测试:

 @Test
 public void test01(){
     ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
 
     Student student = (Student) context.getBean("student");
 
     System.out.println(student.getName());
 
 }

2、Bean注入

注意点:这里的值是一个引用,ref

 <bean id="ads" class="com.yhn.domain.Address">
        <property name="address" value="西宁"></property>
</bean>
 
 <bean id="student" class="com.yhn.domain.Student">
       <property name="name" value="yhn"></property>

   <!--引用值-->
       <property name="address" ref="ads"></property>
 </bean>

3、数组注入

<!--数组-->
       <property name="books">
           <array>
               <value>人性的弱点</value>
               <value>小王子</value>
               <value>傲慢与偏见</value>
           </array>
       </property>

4、List注入

<!--List-->
       <property name="hobbys">
           <list>
               <value>篮球</value>
               <value>足球</value>
               <value>网球</value>
           </list>
       </property>

5、Map注入

 <!--Map-->
       <property name="card">
           <map>
               <entry key="身份证" value="123456"></entry>
               <entry key="校园卡" value="666666"></entry>
           </map>
       </property>

6、set注入

 <!--Set-->
       <property name="games">
           <set>
               <value>CF</value>
               <value>LOL</value>
               <value>DNF</value>
           </set>
       </property>

7、Null注入

<!--null-->
       <property name="wife">
           <null/>
       </property>

8、Properties注入

<!--properties-->
       <property name="info">
           <props>
               <prop key="学号">20200707</prop>
               <prop key="性别">男</prop>
           </props>
       </property>

测试结果:

Student(
         *      name=yhn,
         *      address=Address{address='西宁'},
         *      books=[人性的弱点, 小王子, 傲慢与偏见],
         *      hobbys=[篮球, 足球, 网球],
         *      card={身份证=123456, 校园卡=666666},
         *      games=[CF, LOL, DNF], wife=null,
         *      info={
         *            学号=20200707,
         *            性别=男
         *      }
         * )

6.4 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.kuang.pojo.User" p:name="狂神" p:age="18"/>

2、c 命名空间注入 : 需要在头文件中加入约束文件

 导入约束 : xmlns:c="http://www.springframework.org/schema/c"
 <!--C(构造: Constructor)命名空间 , 属性依然要设置set方法-->
 <bean id="user" class="com.kuang.pojo.User" c:name="狂神" c:age="18"/>

发现问题:爆红了,刚才我们没有写有参构造!

解决:把有参构造器加上,这里也能知道,c 就是所谓的构造器注入!

测试代码:

 @Test
 public void test(){
     ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
     User user = (User) context.getBean("user");
     System.out.println(user);
 }

posted @ 2020-09-10 19:37  YhnCoder  阅读(125)  评论(0编辑  收藏  举报