DI 依赖注入(Dependency Injection)【spring基础】

2021-03-05 08:29

阅读:419

标签:lan   type   array   com   方式   中国   use   import   环境搭建   

1、构造器注入

    

2、Set方式注入(重点)

  • 依赖注入:Set注入

  • 依赖: bean对象的创建依赖于容器

  • 注入:bean对象中的所有属性,由容器来注入

【环境搭建】

  1. 复杂类型

  2. 真实测试对象

  3. 测试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.wang.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 hobbys;
         private Map card;
         private Set 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 hobbys) {
             this.hobbys = hobbys;
        }
     
         public void setCard(Map card) {
             this.card = card;
        }
     
         public void setGames(Set 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(">\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、常量注入

     

    测试:

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

    2、Bean注入

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

     

    3、数组注入

     西游记红楼梦水浒传

    4、List注入

     听歌看电影爬山

    5、Map注入

     

    6、set注入

     LOLBOBCOC

    7、Null注入

     

    8、Properties注入

     20234343小fd

3、其他方式

记得有参构造和无参构造器写上

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

 导入约束 : xmlns:p="http://www.springframework.org/schema/p"
 
 
 

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

 导入约束 : xmlns:c="http://www.springframework.org/schema/c"
 
 

Idea可以自动导入约束

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

DI 依赖注入(Dependency Injection)【spring基础】

标签:lan   type   array   com   方式   中国   use   import   环境搭建   

原文地址:https://www.cnblogs.com/IanIan/p/14325263.html


评论


亲,登录后才可以留言!