首页 > 编程 > Java > 正文

Spring依赖注入的两种方式(根据实例详解)

2019-11-26 12:17:16
字体:
来源:转载
供稿:网友

1,Set注入    2,构造注入

Set方法注入:

原理:通过类的setter方法完成依赖关系的设置

name属性的取值依setter方法名而定,要求这个类里面这个对应的属性必须有setter方法。

Set方法注入时spring中配置文件:

<?xml version="1.0" encoding="UTF-8"?> <beans   xmlns="http://www.springframework.org/schema/beans"   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xmlns:p="http://www.springframework.org/schema/p"   xsi:schemaLocation="http://www.springframework.org/schema/beans    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">    <bean id="car" class="org.spring01.Car">     <constructor-arg value="奔驰"></constructor-arg>     <constructor-arg type="java.lang.String">       <value>土豪金</value>     </constructor-arg>     <constructor-arg value="高级轿车"></constructor-arg>   </bean>      <bean id="person" class="org.spring01.Person">     <property name="name" value="张三"></property>     <property name="age" value="11"></property>     <property name="car" ref="car"></property>   </bean> </beans>

定义Car类:

package org.spring01;  public class Car {   private String name;//车名   private String color;//颜色   private String clas;//等级   public String getName() {     return name;   }   public void setName(String name) {     this.name = name;   }   public String getColor() {     return color;   }   public void setColor(String color) {     this.color = color;   }   public String getClas() {     return clas;   }   public void setClas(String clas) {     this.clas = clas;   }   public Car(String name, String color, String clas) {     super();     this.name = name;     this.color = color;     this.clas = clas;   }    public Car() {     super();     // TODO Auto-generated constructor stub   }   @Override   public String toString() {     return "Car [name=" + name + ", color=" + color + ", clas=" + clas         + "]";   }    } 

定义Person类:

package org.spring01;  public class Person {   private String name;//名字   private int age;//年龄   private Car car;//他的车   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 Car getCar() {     return car;   }   public void setCar(Car car) {     this.car = car;   }   public Person(String name, int age, Car car) {     super();     this.name = name;     this.age = age;     this.car = car;   }   public Person() {     super();     // TODO Auto-generated constructor stub   }   @Override   public String toString() {     return "Person [name=" + name + ", age=" + age + ", car=" + car + "]";   }    } 

测试类:

package org.spring01;  import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;  public class SpringTest{     @Test     public void toGetPerson(){       ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");       Person person = (Person) context.getBean("person");       System.out.println(person);     }     @Test     public void toGetCar(){       ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");       Car car = (Car) context.getBean("car");       System.out.println(car);     }   } 

使用单元测试(JUnit)测试toGetPerson()方法,结果为:

Person [name=张三, age=11, car=Car [name=奔驰, color=土豪金, clas=高级轿车]]

构造方法注入:

原理:通过构造函数完成依赖关系的设定

构造注入指的是在接受注入的类中,定义一个构造方法,并在构造方法的参数中定义需要注入的元素,其中,index表示构造方法中的参数索引(第一个参数索引为0)。

构造方法注入时spring中配置文件:

<?xml version="1.0" encoding="UTF-8"?> <beans   xmlns="http://www.springframework.org/schema/beans"   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xmlns:p="http://www.springframework.org/schema/p"   xsi:schemaLocation="http://www.springframework.org/schema/beans    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">    <bean id="car" class="org.spring02.Car">     <constructor-arg value="大众"></constructor-arg>     <constructor-arg type="java.lang.String">       <value>白色</value>     </constructor-arg>     <constructor-arg value="中级轿车"></constructor-arg>   </bean>         <bean id="person" class="org.spring02.Person">     <constructor-arg index="0" value="李四"></constructor-arg>     <constructor-arg index="1" value="23"></constructor-arg>     <constructor-arg index="2" ref="car"></constructor-arg>   </bean> </beans> 

定义Car类:

package org.spring02;  public class Car {   private String name;//车名   private String color;//颜色   private String clas;//等级      public Car(String name, String color, String clas) {     super();     this.name = name;     this.color = color;     this.clas = clas;   }    public Car() {     super();     // TODO Auto-generated constructor stub   }   @Override   public String toString() {     return "Car [name=" + name + ", color=" + color + ", clas=" + clas         + "]";   }    } 

定义Person类:

package org.spring02;  public class Person {   private String name;//名字   private int age;//年龄   private Car car;//他的车      public Person(String name, int age, Car car) {     super();     this.name = name;     this.age = age;     this.car = car;   }   public Person() {     super();     // TODO Auto-generated constructor stub   }   @Override   public String toString() {     return "Person [name=" + name + ", age=" + age + ", car=" + car + "]";   }    } 

测试类:

package org.spring02;  import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;  public class SpringTest{     @Test     public void toGetPerson(){       ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext01.xml");       Person person = (Person) context.getBean("person");       System.out.println(person);     }     @Test     public void toGetCar(){       ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext01.xml");       Car car = (Car) context.getBean("car");       System.out.println(car);     } } 

使用单元测试(JUnit)测试toGetPerson()方法,结果为:

Person [name=李四, age=23, car=Car [name=大众, color=白色, clas=中级轿车]]

上面的例子都采用了单元测试的方法检测运行结果,需要导库: JUnit

Demo的大体结构:

我们可以看到,set方法和构造方法都可以设值成功, 实际开发中最常用到的是set方法设值。但这两种依赖注入的方式并没有绝对的好坏,只是使用的场合不同。

使用构造注入可以在构建对象的同时完成依赖关系到的建立,所以如果要建立的对象的关系很多,使用构造注入会在构造方法上留下很多参数,可读性极差,所以当对象的关系比较多的时候采用set方法注入。

使用set方法注入是通过类的setter方法完成依赖关系的设置的,所以不能保证相关的数据在执行时不被更改设定。所以如果想使一些数据变为只读或者私有,就要采用构造注入了。

建议采用以set注入为主,构造注入为辅的注入策略。对于依赖关系无须变化的注入,尽量采用构造注入;而其他的依赖关系的注入,则考虑采用set注入。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持武林网。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表