框架--Spring框架之依赖注入和自动装配

Spring框架依赖注入和类的自动装配

依赖注入

  • 依赖注入的方式有3种:----你去注入属性的时候有这3种注入方式;

    • 接口注入(调用和实现分离):不常用
    • setter注入(就是使用set方法为属性赋值):set的注入,在你的这个bean(实体类中)文件,一定要有的set方法
      注:如果实体类中的属性值是数组,集合,xml文件注入的时候使用< list >标签
    • 构造器注入(通过构造方法赋值):要进行构造方法注入的时候,一定要保证你的这个bean文件中有先关的这个构造方法,通过标签中的value为元素的构造方法赋值
  • 代码示例:

  • 依赖jar包
    框架--Spring框架之依赖注入和自动装配_第1张图片

  • 接口注入

/*
这是一个语言转换的接口
*/
public interface ChangeLetter {
	
	public String change() throws Exception;
}

实现类

/*
这是语言转换接口的一个实现类将字符串转换成小写
*/
public class ChangetoLowerCase implements ChangeLetter{
	
	private String str;

	public String getStr() {
		return str;
	}

	public void setStr(String str) {
		this.str = str;
	}

	@Override
	public String change() throws Exception {
		
		return str.toLowerCase();
	}	
}

bean.xml



    
    
    
        
    

测试类

public class Test {
	public static void main(String[] args) throws Exception {
		
		  ApplicationContext applicationContext=new
		  ClassPathXmlApplicationContext("com/hfc/intface/bean.xml"); 
		  ChangeLetter changeLetter=applicationContext.getBean("ChangetoLowerCase",
		  ChangetoLowerCase.class);
		  System.out.println( changeLetter.change());
		  }
}
  • 使用set和构造器注入
    这是student类
package com.wdhcr.bean;

public class Student {
    private String name;
    private int age;
    private String address;

    public Student() {

    }

    public Student(String name, int age, String address) {
        this.name = name;
        this.age = age;
        this.address = address;
    }

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

    public String getAddress() {
        return address;
    }

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

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

Person类

package com.wdhcr.bean;

/*
* 这是实体类,有它的相关的成员变量,并生成了它的get set方法 ,
* 还有带参构造器,和重写了它的tostring方法
* */
public class Person {
    private String name;
    private int age;
    private Student student;

    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }

    public Person() {
    }

    public Person(String name, int age, Student student) {
        this.name = name;
        this.age = age;
        this.student = student;
    }

    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 "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
               ", student=" + student +
                '}';
    }
}


bean.xml文件依赖注入




    
        
        
        
    

    
        张三
        13
        
    
    
    
    
    

    

测试类代码

package com.wdhcr.test;

import com.wdhcr.bean.Person;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/*
* 这是person的测试类
* 使用spring框架创建person的一个对象
* */
public class PersonTest {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("bean.xml");
        Person person = classPathXmlApplicationContext.getBean("person", Person.class);
        System.out.println(person);
        System.out.println(person.getName()+"\t"+person.getAge());
        System.out.println("-------------");
        Person person1 = classPathXmlApplicationContext.getBean("person1", Person.class);
        System.out.println(person1);
        System.out.println(person1.getName()+"\t"+person1.getAge());
    }
}

运行结果
在这里插入图片描述

自动装配

  • autowire byName (按名称自动装配)

    • 由于在手动配置xml过程中,常常发生字母缺漏和大小写等错误,而无法对其进行检查,使得开发效率降低。采用自动装配将避免这些错误,并且使配置简单化。

    • 当一个bean节点带有 autowire byName的属性时。

      • 将查找其类中所有的set方法名
      • 去spring容器中寻找是否有此字符串名称id的对象,如果有,就取出注入;如果没有,就报空指针异常
  • autowire byType (按类型自动装配)

    • 使用autowire byType首先需要保证:同一类型的对象,在spring容器中唯一。如果不唯一,会报不唯一的异常。
  • constructor

    • 根据构造器自动装配
  • 代码示例

  • 依赖jar包
    框架--Spring框架之依赖注入和自动装配_第2张图片

  • Dog类

package com.wdhcr.auto;
/*
 *
 *这是dog的实体类使用xml文件注入
 * 生成它的set get方法和带参和无参构造
 * 重写它的tostring方法
 * */
public class Dog {
    private String name;
    private Integer age;
    private String address;

    public Dog() {
    }

    public Dog(String name, Integer age, String address) {
        this.name = name;
        this.age = age;
        this.address = address;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    @Override
    public String toString() {
        return "Dog [name=" + name + ", age=" + age + ", address=" + address + "]";
    }
}


  • Cat类
package com.wdhcr.auto;
/*
*
*这是cat的实体类使用xml文件注入
* 生成它的set get方法和带参和无参构造
* 重写它的tostring方法
 * */
public class Cat {
    private String name;
    private Integer age;
    private String address;

    public Cat() {
    }

    public Cat(String name, Integer age, String address) {
        this.name = name;
        this.age = age;
        this.address = address;
    }

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

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

    public String getAddress() {
        return address;
    }

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

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

  • User类
package com.wdhcr.auto;
/*
 *
 *这是user的实体类使用xml文件注入
 * 生成它的set get方法和带参和无参构造
 * 重写它的tostring方法
 * */
public class User {
    private String name;
    private Integer age;
    private String address;
    private Dog dog;
    private Cat cat;

    public User() {
    }

    public User(String name, Integer age, String address, Dog dog, Cat cat) {
        this.name = name;
        this.age = age;
        this.address = address;
        this.dog = dog;
        this.cat = cat;
    }

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

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

    public String getAddress() {
        return address;
    }

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

    public Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }

    public Cat getCat() {
        return cat;
    }

    public void setCat(Cat cat) {
        this.cat = cat;
    }

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

bean.xml文件



































  • 测试类
package com.wdhcr.auto;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext classPathXmlApplicationContext = 
                new ClassPathXmlApplicationContext("com/wdhcr/auto/bean.xml");
        User user = classPathXmlApplicationContext.getBean("user", User.class);
        System.out.println(user);
    }
}

  • 以上就简单的介绍了Spring框架的依赖注入和自动装配。

你可能感兴趣的:(框架,Spring,di,依赖注入,自动装配)