栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

从零开始学习Spring5架构(五)——依赖注入(DI)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

从零开始学习Spring5架构(五)——依赖注入(DI)

依赖注入(Dependency Injection,DI)
  • 依赖 : 指Bean对象的创建依赖于容器 . Bean对象的依赖资源

  • 注入 : 指Bean对象所依赖的资源 , 由容器来设置和装配

构造器注入

有参构造器注入,前文有具体的讲解,此处给出代码实例

    
    
        
    


    
    
        
    


    
        
    
set注入(重点)

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

package com.pag.pojo;

public class Address {
    private String address;

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

Student.java

package com.pag.pojo;

import java.util.List;
import java.util.*;

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 String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public String[] getBooks() {
        return books;
    }

    public void setBooks(String[] books) {
        this.books = books;
    }

    public List getHobbys() {
        return hobbys;
    }

    public void setHobbys(List hobbys) {
        this.hobbys = hobbys;
    }

    public Map getCard() {
        return card;
    }

    public void setCard(Map card) {
        this.card = card;
    }

    public Set getGames() {
        return games;
    }

    public void setGames(Set games) {
        this.games = games;
    }

    public String getWife() {
        return wife;
    }

    public void setWife(String wife) {
        this.wife = wife;
    }

    public Properties getInfo() {
        return info;
    }

    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);
    }

}

常量注入
    
        
    

测试类:

    @Test
    public void test1(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student student = context.getBean("student", Student.class);
        System.out.println(student.getName());
    }
Bean注入
    
        
    
    
        
    

测试类:

    @Test
    public void test2(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student student = context.getBean("student", Student.class);
        System.out.println(student.getAddress().getAddress());
    }
数组注入
 
        
            
                spring
                java
                c
            
        
    

测试类:

    @Test
    public void test3(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student student = context.getBean("student", Student.class);
        for (String s : student.getBooks()) {
            System.out.println(s);
        }
    }
List注入
    
        
            
                sing song
                read book
            
        
    

测试类:

    @Test
    public void test4(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student student = context.getBean("student", Student.class);
        for (String s : student.getHobbys()) {
            System.out.println(s);
        }
    }
Map注入
    
        
            
                
                
            
        
    

测试类:

    @Test
    public void test5(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student student = context.getBean("student", Student.class);
        System.out.println(student.getCard());
    }
Set注入
    
        
            
                LOL
                DND
                COD
            
        
    

测试类:

    @Test
    public void test6(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student student = context.getBean("student", Student.class);
        System.out.println(student.getGames());
    }
Null注入
    
        
    

测试类:

    @Test
    public void test7(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student student = context.getBean("student", Student.class);
        System.out.println(student.getWife());
    }
Properties注入
    
        
            
                20210101
                林而异
                
            
        
    

测试类:

    @Test
    public void test8(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student student = context.getBean("student", Student.class);
        System.out.println(student.getInfo());
    }
拓展注入实现

创建一个User实体类:

package com.pag.pojo;

public class User {
    private String name;
    private int age;

     public User() {
        System.out.println("无参");
    }

    public User(String name, int age) {
        this.name = name;
        this.age = age;
        System.out.println("有参");
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + ''' +
                ", age=" + age +
                '}';
    }
}

p命名空间注入

需要在头文件中假如约束文件

 xmlns:p="http://www.springframework.org/schema/p"

测试类:

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

运行结果:

结果证明,p命名空间注入调用的是无参构造方法,使用set方法将参数传输到对象中

c命名空间注入
 xmlns:c="http://www.springframework.org/schema/c"

运行结果:

结果证明,c命名空间注入调用的是有参构造方法,c 就是所谓的构造器注入

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/285271.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号