(四)Spring从入门到入土——依赖注入(DI)

Dependency Injection

概念

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

注入方式

一共有三种:分别是构造器注入;Set注入;P命名和C命名注入

构造器注入

Spring从入门到入土——快速上手Spring中Beans.xml中有详细介绍,即通过有参构造来创建



        
    
            
        
        
    
    
    

    

        
    

    
    

    
        
    

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) {
        Address = address;
    }

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

Student.java

public class Student {
    private String name;
    private Address address;
    private String[] books;
    private List hobbies;
    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 getHobbies() {
        return hobbies;
    }

    public void setHobbies(List hobbies) {
        this.hobbies = hobbies;
    }

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

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", address=" + address.toString() +
                ", books=" + Arrays.toString(books) +
                ", hobbies=" + hobbies +
                ", card=" + card +
                ", games=" + games +
                ", wife='" + wife + '\'' +
                ", info=" + info +
                '}';
    }
}
Bean注入:

beans.xml




    
        
    
    
    
        
        


        
        
        
        
            
                红楼梦
                三国演义
                水浒传
                西游记
            
        
        
        
            
                打游戏
                看电影
                码代码
            
        
        
        
            
                
                
                
            
        
        
        
            
                CSGO
                COC
            
        
        
        
            
        
        
        
            
                https://blog.csdn.net/issunmingzhi
                root
                123456
            
        
    

测试:

public class MyTest {
    public static void main(String[] args) {
      ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Student student = (Student) context.getBean("student");
        System.out.println(student.toString());
    }
}
结果:

image

扩展方式注入(p、c命名注入)

user.java:【注意,这里没有有参构造器】

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

    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命名空间注入:
  • 实体类中必须有set方法;
  • 实体类中必须有无参构造器(默认存在);
  • 必须导入p命名空间注入方式依赖。
 导入约束 : xmlns:p="http://www.springframework.org/schema/p"
 
 
 
c命名空间注入

​ 就是构造器注入、需要在头文件下加入约束文件

 导入约束 : xmlns:c="http://www.springframework.org/schema/c"
 
 
此时会发现出问题:因为没有写有参构造函数

C命名空间注入就是所谓的构造器注入

最后

  • 如果觉得看完有收获,希望能给我点个赞,这将会是我更新的最大动力,感谢各位的支持
  • 欢迎各位关注我的公众号【java冢狐】,专注于java和计算机基础知识,保证让你看完有所收获,不信你打我
  • 如果看完有不同的意见或者建议,欢迎多多评论一起交流。感谢各位的支持以及厚爱。

image

欢迎关注公众号“ Java冢狐”,获取最新消息

你可能感兴趣的:(java)