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

网友投稿 223 2023-05-14


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

1,Set注入    2,构造注入

Set方法注入:

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

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

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

xmlns="http://springframework.org/schema/beans"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:p="http://springframework.org/schema/p"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.0.xsd">

土豪金

xmlns="http://springframework.org/schema/beans"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:p="http://springframework.org/schema/p"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.0.xsd">

土豪金

定义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中配置文件:

xmlns="http://springframework.org/schema/beans"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:p="http://springframework.org/schema/p"

xsi:schemaLocatiocHUcfOaon="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.0.xsd">

白色

xmlns="http://springframework.org/schema/beans"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:p="http://springframework.org/schema/p"

xsi:schemaLocatiocHUcfOaon="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.0.xsd">

白色

定义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);

}

}

http://

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

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

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

Demo的大体结构:

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

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

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

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


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:微信小程序中做用户登录与登录态维护的实现详解
下一篇:BufferedInputStream(缓冲输入流)详解_动力节点Java学院整理
相关文章

 发表评论

暂时没有评论,来抢沙发吧~