Spring DI 详解

发布于:2025-04-18 ⋅ 阅读:(18) ⋅ 点赞:(0)

学习过 IoC 后,就知道我们可以将对象交给 Spring 进行管理,但是我们在一个类会有若干属性,也就是这个类依赖于这若干个属性,那么我们就可以将交给 Spring 管理的对象注入到这个类中,这也就是依赖注入。

依赖注入有三种方式,分别为属性注入、构造方法注入、setter方法注入。下面一一介绍。

一、属性注入

@Autowired

有如下代码:

@Controller
public class TestController {

    //属性注入
    @Autowired
    private TestService service;

    public void print() {
        System.out.println("controller");
        service.print();
    }
}


@Service
public class TestService {

    public void print() {
        System.out.println("service");
    }
}


//SpringBoot 启动类
@SpringBootApplication
public class SpringBootDemo2025417Application {

	public static void main(String[] args) {
		ApplicationContext context = SpringApplication.run(SpringBootDemo2025417Application.class, args);

		//获取 TestController 对象
		TestController controller = context.getBean(TestController.class);
		controller.print();
	}

}

在 TestController 类中有属性 service,我们通过 @Autowired 注解将 TestService 输入到属性 service 中,这样我们就可以使用 TestService 中的相关方法了,代码运行结果如下:

代码先执行了 TestController 的print 方法,然后又执行了 TestService 的 print 方法。

 现有下面两种情况,一种没加 @Service,另一种是没加 @Autowired,下面我们来分别看看这两种情况。

没加 @Service

运行代码后,发现报错了,错误代码如下:

Description:

Field service in com.gjm.demo.controller.TestController required a bean of type 'com.gjm.demo.service.TestService' that could not be found.

由于我们没有加 @Service,就代表我们没有将 TestService 类交给 Spring 进行管理,那么我们在使用 @Autowired 在 Spring 容器中获取对象时就找不到这个对象,就会报这个错。

没加 @Autowired

运行代码后,发现报错了,错误代码如下:

controller
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "com.gjm.demo.service.TestService.print()" because "this.service" is null
	at com.gjm.demo.controller.TestController.print(TestController.java:16)
	at com.gjm.demo.SpringBootDemo2025417Application.main(SpringBootDemo2025417Application.java:19)

我们发现了,这里报了一个空指针异常,这时为什么呢?

原因是虽然我们加了 @Service 注解,将 TestService 交给 Spring 进行管理,但是由于我们没有加 @Autowired 注解,就使得我们并没有将 TestService 的对象注入给 service,也即是并没有初始化 service,当我们在吊桶TestService 的 print 方法时,就会报空指针异常。这也就是 “controller” 可以显示出来,但 “service” 显示不出来的原因。

二、构造方法注入

顾名思义,就是将属性放到构造方法中在进行注入,代码如下:

@Controller
public class TestController {

    private TestService service;

    //构造方法
    public TestController(TestService service) {
        this.service = service;
    }

    public void print() {
        System.out.println("controller");
        service.print();
    }
}


@Service
public class TestService {

    public void print() {
        System.out.println("service");
    }
}


@SpringBootApplication
public class SpringBootDemo2025417Application {

	public static void main(String[] args) {
		ApplicationContext context = SpringApplication.run(SpringBootDemo2025417Application.class, args);

		//获取 TestController 对象
		TestController controller = context.getBean(TestController.class);
		controller.print();
	}

}

代码运行结果如下:

在上面的构造方法中没有加任何注解,就将 TestService 注入给了 TestController。

但在日常开发中,加上了有参的构造函数,无参的构造函数就默认没有了,为了避免出现问题,我们还会将无参的构造函数也加上,代码如下:

@Controller
public class TestController {

    private TestService service;
    
    //无参构造函数
    public TestController() {
        
    }

    //有参构造函数
    public TestController(TestService service) {
        this.service = service;
    }

    public void print() {
        System.out.println("controller");
        service.print();
    }
}

 代码运行结果如下:

controller
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "com.gjm.demo.service.TestService.print()" because "this.service" is null
	at com.gjm.demo.controller.TestController.print(TestController.java:27)
	at com.gjm.demo.SpringBootDemo2025417Application.main(SpringBootDemo2025417Application.java:19)

又报错了, 是空指针异常。这是因为我们加上了无参的构造函数,在进行构造方法注入的时候,默认的构造方法为无参的构造方法,若没有无参的给构造方法就会使用我们自己写的构造方法。这里我们把无参的构造方法加上了,就会使用无参的构造方法,这时就不会给 service 进行注入,也就是 service 没有斤西瓜初始化,在调用 print 方法时就会出现空指针异常。

上面时只有一个属性的情况,那如果 testController 中有多个属性呢?下面我们介绍有两个属性的情况。代码如下:

package com.gjm.demo.controller;

import com.gjm.demo.service.TestService1;
import com.gjm.demo.service.TestService2;
import org.springframework.stereotype.Controller;

@Controller
public class TestController {

    private TestService1 service1;

    private TestService2 service2;

    public TestController() {
        
    }

    public TestController(TestService1 service1) {
        this.service1 = service1;
    }

    public TestController(TestService2 service2) {
        this.service2 = service2;
    }

    public TestController(TestService1 service1, TestService2 service2) {
        this.service1 = service1;
        this.service2 = service2;
    }

    public void print() {
        System.out.println("controller");
        service1.print();
        service2.print();
    }
}


@Service
public class TestService1 {

    public void print() {
        System.out.println("service1");
    }
}


@Service
public class TestService2 {

    public void print() {
        System.out.println("service2");
    }
}


@SpringBootApplication
public class SpringBootDemo2025417Application {

	public static void main(String[] args) {
		ApplicationContext context = SpringApplication.run(SpringBootDemo2025417Application.class, args);

		//获取 TestController 对象
		TestController controller = context.getBean(TestController.class);
		controller.print();
	}

}

在 TestController 中有两个属性,分别为 TestService1 和 TestService2,代码运行结果如下:

controller
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "com.gjm.demo.service.TestService1.print()" because "this.service1" is null
	at com.gjm.demo.controller.TestController.print(TestController.java:49)
	at com.gjm.demo.SpringBootDemo2025417Application.main(SpringBootDemo2025417Application.java:17)

发现这个报错信息与前一个报错信息是一样的,也是空指针异常。这是因为无论有几个属性,构造方法注入的默认构造方法都是无参构造方法,就相当于 service1 和 service2 书香都没有初始化,在调用 print 方法就会报空指针异常。

那如果将无参的构造方法去掉呢?代码如下:

package com.gjm.demo.controller;

import com.gjm.demo.service.TestService1;
import com.gjm.demo.service.TestService2;
import org.springframework.stereotype.Controller;

@Controller
public class TestController {

    private TestService1 service1;

    private TestService2 service2;

    public TestController(TestService1 service1) {
        this.service1 = service1;
    }

    public TestController(TestService2 service2) {
        this.service2 = service2;
    }

    public TestController(TestService1 service1, TestService2 service2) {
        this.service1 = service1;
        this.service2 = service2;
    }

    public void print() {
        System.out.println("controller");
        service1.print();
        service2.print();
    }
}

代码运行结果如下:

controller
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "com.gjm.demo.service.TestService1.print()" because "this.service1" is null
	at com.gjm.demo.controller.TestController.print(TestController.java:49)
	at com.gjm.demo.SpringBootDemo2025417Application.main(SpringBootDemo2025417Application.java:17)

同样的,也是空指针异常,那应该怎么办呢?

在构造方法注入中,也可以使用 @Autowired 注解,当在构造方法上使用该注解时,表示的含义就是修改默认的构造函数,于是,可以在全参的构造函数上加上 @Autowired 注解,代码如下:

package com.gjm.demo.controller;

import com.gjm.demo.service.TestService1;
import com.gjm.demo.service.TestService2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller
public class TestController {

    private TestService1 service1;

    private TestService2 service2;

    public TestController() {

    }

    public TestController(TestService1 service1) {
        this.service1 = service1;
    }

    public TestController(TestService2 service2) {
        this.service2 = service2;
    }

    @Autowired
    public TestController(TestService1 service1, TestService2 service2) {
        this.service1 = service1;
        this.service2 = service2;
    }

    public void print() {
        System.out.println("controller");
        service1.print();
        service2.print();
    }
}

运行结果如下:

这次就运行成功了。

 三、setter 方法注入

setter 方法注入即使用 setter方法进行属性注入,在使用时需要在对应的 setter 方法上加上 @Autowired 注解,如果不加,就会报错。代码如下:

package com.gjm.demo.controller;

import com.gjm.demo.service.TestService1;
import com.gjm.demo.service.TestService2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller
public class TestController {

    private TestService1 service1;

    private TestService2 service2;

    @Autowired
    public void setService1(TestService1 service1) {
        this.service1 = service1;
    }

    @Autowired
    public void setService2(TestService2 service2) {
        this.service2 = service2;
    }

    public void print() {
        System.out.println("controller");
        service1.print();
        service2.print();
    }
}


@Service
public class TestService1 {

    public void print() {
        System.out.println("service1");
    }
}


@Service
public class TestService2 {

    public void print() {
        System.out.println("service2");
    }
}


@SpringBootApplication
public class SpringBootDemo2025417Application {

	public static void main(String[] args) {
		ApplicationContext context = SpringApplication.run(SpringBootDemo2025417Application.class, args);

		//获取 TestController 对象
		TestController controller = context.getBean(TestController.class);
		controller.print();
	}

}

运行结果如下:

 


网站公告

今日签到

点亮在社区的每一天
去签到