依赖注入的实现方式

1,527 阅读3分钟

开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第3天

依赖注入的实现方式

依赖注入指的是使用Spring框架创建对象时,动态的将其所依赖的对象注入到Bean组件中。(说得很高级,个人理解就是在创建对象时的传参操作)

依赖注入通常有两种实现方式:

  1. 构造方法注入
  2. 属性setter方法注入

构造方法注入

构造方法注入是指Spring容器调用构造方法注入被依赖的实例,构造方法可以是有参的或无参的。

Spring在读取配置信息后,会通过反射方式调用实例的构造方法,如果是有参构造方法,可以在构造方法中传入所需的参数值,最后创建类对象。

实现:

第一步,编写用户类,定义id、name以及password三个属性,还有有参构造方法

public class User {
    private int id;
    private String name;
    private String password;
    public User(int id, String name, String password){
        this.id = id;
        this.name = name;
        this.password = password;
    }
    public String toString(){
        return "id = " + id + "name = " + name + ",password = " + password;
    }
}

第二步,获取Bean的配置信息:创建applicationContext-User.xml文件,在该文件中添加User类的配置信息。

<bean id="user" class="User">
    <constructor-arg name="id" value="1"></constructor-arg>
    <constructor-arg name="name" value="何小幸"></constructor-arg> 
    <constructor-arg name="password" value="123"></constructor-arg>
</bean>

constructor-arg用于给User类构造方法的参数注入值,还可以有type属性以指定参数类型

Spring通过User类的构造方法获取constructor-arg元素定义的值,最总这些值会被赋值给spring创建的User对象

注意class要写全类名

第三步,编写测试类:创建测试类TestUser,用于测试依赖注入的结果

public class TestUser {
    public static void main(String[] args)throws Exception{
        //加载applicationContext.xml配置
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext-User.xml");
        //获取配置中的User实例
        User user = applicationContext.getBean("user",User.class);//通过id+类型获取对象
        System.out.println(user1);
    }
}

最后打印输出id = 1,name = 何小幸,password = 123

属性setter方法注入

属性setter方法注入是Spring最主流的注入方法,这种注入方法简单、直观,它是在被注入的类中声明一个setter方法,通过setter方法的参数注入对应的值

实现:

第一步,编写用户类,定义id、name以及password三个属性,还有对应的setter方法

public class User {
    private int id;
    private String name;
    private String password;
    public void setId(int id){
        this.id = id;
    }
    public void setName(int name){
        this.name = name;
    }
    public void setPassword(int password){
        this.password = password;
    }
    public String toString(){
        return "id = " + id + "name = " + name + ",password = " + password;
    }
}

第二步,获取Bean的配置信息:创建applicationContext-User.xml文件,在该文件中添加User类的配置信息。

<bean id="user" class="User">
	<property name="id" value="1"></property>
	<property name="name" value="何小幸"></property>	
	<property name="password" value="123"></property>
</bean>

property元素就是给成员变量注入值的,name属性指定该类的成员变量名称,value属性提供对应的注入值

注意class要写全类名

第三步,编写测试类:创建测试类TestUser,用于测试依赖注入的结果

public class TestUser {
    public static void main(String[] args)throws Exception{
        //加载applicationContext.xml配置
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext-User.xml");
        //获取配置中的User实例
        User user = applicationContext.getBean("user",User.class);//通过id+类型获取对象
        System.out.println(user1);
    }
}

最后打印输出id = 1,name = 何小幸,password = 123

总结: 以上我们演示了依赖注入的两种方式,可以看出,我们完成的就是为对象传参的操作,参数包括但不限于基本数据类型,也可以是类