【Java EE进阶 --- SpringBoot】Spring DI详解

发布于:2025-09-10 ⋅ 阅读:(25) ⋅ 点赞:(0)

乐观学习,乐观生活,才能不断前进啊!!!

我的主页:optimistic_chen

我的专栏:c语言Java,
Java EE初阶Java数据结构

欢迎大家访问~
创作不易,大佬们点赞鼓励下吧~

DI详解

DI(Dependency Injection)依赖注入:容器在运⾏期间,动态的为应⽤程序提供运⾏时所依赖的资源,称之为依赖注⼊。

依赖注入是一个过程,当IoC容器创建Bean时,提供运⾏时所依赖的资源,⽽资源指的就是对象。我们使用@Autowired注解完成依赖注入。换句话说:就说把对象取出来放到某个类的属性中。前面我们知道@Controller等注解实现把对象交给Spring管理(IoC容器),而现在我们使用@Autowired注解把对象拿出来。

注入

关于依赖的注入,Spring给我们提供三种方法:

  1. 属性注入
  2. 构造方法注入
  3. Setter注入

属性注入

属性注入使用@Autowired实现的,将Service类注入到Controller类中,

Service类

@Service
public class UserService {
    public void m1(){
        System.out.println("UserService");
    }
}

Controller类

@ResponseBody
@Controller
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/m1")
    public void m1(){
        System.out.println("UserController");
        userService.m1();
    }
}

实现方法

@SpringBootApplication
public class DemoApplication {
	public static void main(String[] args) {
		//获取上下文对象
		ApplicationContext context = SpringApplication.run(DemoApplication.class, args);
		UserController userController=(UserController) context.getBean("userController");
		userController.m1();
	}
}

在这里插入图片描述
如果删掉@Autowired注解,那么只会执行Controller类
在这里插入图片描述

构造方法注入

构造⽅法注⼊是在类的构造⽅法中实现注⼊

@ResponseBody
@Controller
public class UserController {
    private UserService userService;
    public UserController(UserService userService) {
        this.userService = userService;
    }

    public void m1(){
        System.out.println("UserController");
        userService.m1();
    }
}

注意:如果类中只有一个构造方法,那么@Autowired注解就可以省略;如果类中有多个构造⽅法,那么需要添加上@Autowired 来明确指定到底使⽤哪个构造⽅法

@ResponseBody
@Controller
public class UserController {
    private UserService userService;
    private UserService2 userService2;

    public UserController(UserService userService) {
        this.userService = userService;
    }
    @Autowired
    public UserController(UserService2 userService2) {
        this.userService2 = userService2;
    }

    public void m1(){
        System.out.println("UserController");
        userService.m1();
    }

    public void m2(){
        System.out.println("UserController2");
        userService2.m2();
    }
}

Service

@Service
public class UserService {
    public void m1(){
        System.out.println("UserService");
    }
}

Service2

@Service
public class UserService2 {
    public void m2(){
        System.out.println("UserService2");
    }
}

在这里插入图片描述

Setter注入

Setter注入和属性的Setter方法实现类似,只是在设置set方法时需要加上@Autowired注解

@ResponseBody
@Controller
public class UserController {
    private UserService userService;
    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public void m1(){
        System.out.println("UserController");
        userService.m1();
    }

}

在这里插入图片描述

优势与缺点

属性注入
  优点:简洁,使用方便
  缺点:只能用于IoC容器,不能注⼊⼀个Final修饰的属性
构造方法注入
  优点:可以注入Final修饰的属性;注入的对象不会被修改;通用性好,构造方法是                JDK支持的,适合任何框架
  缺点:注入多个对象时,代码比较繁琐
Setter注入
  优点:方便在类实例之后,重新对该对象进行配置或者注入
  缺点:不能注入一个Final修饰的属性;注入对象可能会被改变

Autowired存在问题

上篇博客最后提到,当同一类型有多个Bean时,Spring不知道注入哪一个,所以使用@Primary注解,确定默认的注解。现在还有两个注解可以解决这种问题。

@Qualifier注解

@Qualifier注解:指定当前要注⼊的bean对象。在@Qualifier的value属性中,指定注⼊的bean的名称
Controller类

@ResponseBody
@Controller
public class UserController {
    @Qualifier("student2")
    @Autowired
    private Student student;

    public void hello(){
        System.out.println("hello UserController");
        System.out.println(student);
    }
}

StudentComponent类

@Component
public class StudentComponent {
    @Bean
    public Student student() {
        Student student=new Student();
        student.setName("zhangsan");
        student.setAge(12);
        return student;
    }

    @Bean
    public Student student2() {
        return new Student("da",13);
    }
}

在这里插入图片描述

@Resource注解

使⽤@Resource注解:是按照bean的名称进⾏注⼊。通过name属性指定要注⼊的bean的名称

Controller类

@ResponseBody
@Controller
public class UserController {
    @Resource(name="student")
    private Student student;

    public void hello(){
        System.out.println("hello UserController");
        System.out.println(student);
    }
}

在这里插入图片描述
总结:
@Autowired是spring框架提供的注解,⽽@Resource是JDK提供的注解
@Autowired默认是按照类型注⼊,⽽@Resource是按照名称注⼊.相⽐于@Autowired 来说,@Resource ⽀持更多的参数设置,例如name设置,根据名称获取Bean

完结

点一个免费的赞并收藏起来~
点点关注,避免找不到我~
我的主页:optimistic_chen我们下期不见不散 ~ ~ ~


网站公告

今日签到

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