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

Spring 的依赖注入

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

Spring 的依赖注入

Spring 的依赖注入 通过set 方法进行依赖的注入

当给属性赋值的时候,必须提供该属性的set 方法。
Address.java

package com.momo.entity;

public class Address {
    private String address;

    public Address() {
        
    }

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

    public String getAddress() {
        return address;
    }

   

    @Override
    public String toString() {
        return "Address{" +
                "address='" + address + ''' +
                '}';
    }
}

Students.java

package com.momo.entity;

import java.util.*;

public class Student {
    private String name;
    private Address outBean; // 外部bean
    private Address innerBean;// 内部bean
    private String[] array;
    private List list;
    private Map map;
    private Set set;
    private String emptyValue; // 注入一个 空字符串
    private String nullValue; // 注入一个null 值
    private Properties props;

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + ''' +
                ", outBean=" + outBean +
                ", innerBean=" + innerBean +
                ", array=" + Arrays.toString(array) +
                ", list=" + list +
                ", map=" + map +
                ", set=" + set +
                ", emptyValue='" + emptyValue + ''' +
                ", nullValue='" + nullValue + ''' +
                ", props=" + props +
                '}';
    }
}

beans.xml




    
        
    

    

        

        

        
            
                
            
        

        
            
                Mybatis
                Spring
                SpringMVC
            
        


        
            
                Mybatis
                Spring
                SpringMVC
            
        

        
            
                
                
            
        


        
            
                Mybatis
                Spring
                SpringMVC
            
        

        
            
        


        
            
        

        
            
                1001
                
                张三
            
        
    

测试

package com.momo.test;

import com.momo.entity.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Mytest {

    @Test
    public void test(){
        ApplicationContext context =
                new ClassPathXmlApplicationContext("beans.xml");
        Student s = (Student) context.getBean("s");
        System.out.println(s);

    }
}

通过构造方法进行注入

传统的构造方法可以给属性赋值。
this.xxx = xxx; // 手动赋值。需要手动传参。
实体类:

package com.momo.entity;

import java.util.Date;

public class User {
    private String name;
    private Integer age;
    private Date birthday;
    
    public User(){
        System.out.println("无参数的构造方法");
    }
    
    public User(Integer age){
        System.out.println("有一个Integer类型的参数的构造");
        this.age = age;
    }
    
    public User(String name){
        System.out.println("有一个String类型的参数的构造");
        this.name = name;
    }
    
    public User(String name,Integer age){
        System.out.println("有2个String类型和Integer类型的参数的构造");
        this.age = age;
        this.name = name;
    }

    public User(String name, Integer age, Date birthday) {
        System.out.println("3个参数参数的构造方法");
        this.name = name;
        this.age = age;
        this.birthday = birthday;
    }

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

1.通过下标


        
        
    

index 属性 : 就是第几个属性。  给对应的属性赋值的时候,会默认的找到适合当前赋值的构造。

2.通过参数类型

 
        
        


可以用,但是不推荐使用。

3.通过参数的名称


        
        
        



通过p命名空间来注入

本质是 通过调用 set 方法和构造方法来进行注入

1.通过p命名空间来注入,需要在beans.xml 中导入 p 命名空间。

在文件的开头加入约束:
  xmlns:p="http://www.springframework.org/schema/p"
     
    
    
    
      
      
    总结:
      1.p命名空间注入的时候,利用类的set方法
      2.注入的时候分2种情况
      		1).基本类型和String--->在bean的标签上加上属性 p:需要注入的属性的属性名="值"
      		2).注入外部的对象---->在bean的标签上加上属性 p:需要注入的属性的属性名-ref = "外部bean的id"
    
    通过c命名空间来注入

    本质上还是使用构造方法来注入。

    		导入约束
           xmlns:c="http://www.springframework.org/schema/c"
    
    
    
    注意: 需要提供 name的构造
    
     public User(String name){
            System.out.println("有一个String类型的参数的构造");
            this.name = name;
        }
    
    转载请注明:文章转载自 www.mshxw.com
    本文地址:https://www.mshxw.com/it/723755.html
    我们一直用心在做
    关于我们 文章归档 网站地图 联系我们

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

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