• IoC和DI


    Spring 是包含众多工具的 IoC 容器,存的是对象,对象这个词在 Spring 的范围内,称之为 bean

    IoC 是控制反转

    控制权进行了反转,比如对某一个东西的控制权在 A 手上,结果变成了 B ,Spring 管理的是 bean ,所以这里的控制权指的是 bean 的控制权,也就是对象的控制权进行了反转

    之前我们依赖的一些对象,我们需要自己去创建,通过 new 来进行创建,有了 Spring 之后我们就不再进行创建了,而是由 Spring 帮助我们进行创建,这就叫控制反转

    控制反转的一个优点主要是解耦

    耦合度高就说明两个事务的关联性密切,我们项目开发软件设计的原则是高内聚,低耦合

    高内聚 : 是指一个模块内部的关系(比如一个班级)

    低耦合 : 是指各个模块之间的关系(比如各个班级)

    高内聚低耦合就是,一个班级内需要团结协作,但是各个班级之间的相互影响要小,一个班级的问题对别的班级的影响要降低

    Spring MVC 和 Spring Boot 都属于 Spring ,Spring MVC 是基于 Spring 的一个 MVC 框架 ,而 Spring Boot 是基于 Spring 的一套快速开发整合包

    IoC 是思想, DI(依赖注入) 是是一种实现方式

    IoC 就是依赖的象的创建的控制权交给了 Spring 进行管理(存对象), DI 就是如何将依赖对象取出来,并赋给该对象的属性(取对象)

    IoC 提供了两类注解 : 

    1.类注解 : @Controller(控制器存储) , @Service(服务存储) , @Repository(仓库存储) , @Component(组件存储) , @Configuration(配置存储)

    2.方法注解 : @Bean

    接下来我们使用一下这些注解

    ①  @Controller

    先创建一个 controller 包,在里面创建一个 UserController 类,用 @Controller 告诉 Spring 帮我们管理这个对象

    1. package com.example.ioc.controller;
    2. import org.springframework.stereotype.Controller;
    3. //用 @Controller 告诉 Spring 帮我们管理这个对象
    4. @Controller
    5. public class UserController {
    6. public void doController(){
    7. System.out.println("do Controller...");
    8. }
    9. }

    然后在启动类 DemoApplication 中查看 Spring 有没有把上面这个 Controller 存进来呢

    1. package com.example.ioc;
    2. import com.example.ioc.controller.UserController;
    3. import org.springframework.boot.SpringApplication;
    4. import org.springframework.boot.autoconfigure.SpringBootApplication;
    5. import org.springframework.context.ApplicationContext;
    6. @SpringBootApplication
    7. public class DemoApplication {
    8. public static void main(String[] args) {
    9. //Spring 上下文
    10. ApplicationContext context = SpringApplication.run(DemoApplication.class, args);
    11. //返回的就是 Spring 的运行环境
    12. UserController bean = context.getBean(UserController.class);
    13. bean.doController();
    14. }
    15. }

     运行出来了,说明存进去了

     

    bean 一般使用这三个方法

    ②  @Service

    1. package com.example.ioc.service;
    2. import org.springframework.stereotype.Service;
    3. @Service
    4. public class UserService {
    5. public void doService(){
    6. System.out.println("do service....");
    7. }
    8. }

    依旧在启动类 DemoApplication 中查看

    1. package com.example.ioc;
    2. import com.example.ioc.controller.UserController;
    3. import com.example.ioc.service.UserService;
    4. import org.springframework.boot.SpringApplication;
    5. import org.springframework.boot.autoconfigure.SpringBootApplication;
    6. import org.springframework.context.ApplicationContext;
    7. @SpringBootApplication
    8. public class DemoApplication {
    9. public static void main(String[] args) {
    10. //Spring 上下文
    11. ApplicationContext context = SpringApplication.run(DemoApplication.class, args);
    12. //返回的就是 Spring 的运行环境
    13. UserController bean = context.getBean(UserController.class);
    14. bean.doController();
    15. UserService userService = context.getBean(UserService.class);
    16. userService.doService();
    17. }
    18. }

    成功拿到 

    根据名称来获取 UserService 的 bean

    (名称为类名转换为小驼峰,特殊情况就是如果类名前两位都是大写,那么bean的名称就是类名)

    1. package com.example.ioc;
    2. import com.example.ioc.controller.UserController;
    3. import com.example.ioc.service.UserService;
    4. import org.springframework.boot.SpringApplication;
    5. import org.springframework.boot.autoconfigure.SpringBootApplication;
    6. import org.springframework.context.ApplicationContext;
    7. @SpringBootApplication
    8. public class DemoApplication {
    9. public static void main(String[] args) {
    10. //Spring 上下文
    11. ApplicationContext context = SpringApplication.run(DemoApplication.class, args);
    12. //返回的就是 Spring 的运行环境
    13. UserController bean = context.getBean(UserController.class);
    14. bean.doController();
    15. UserService userService = context.getBean(UserService.class);
    16. userService.doService();
    17. //根据名称来获取bean
    18. UserService userService1 = (UserService) context.getBean("userService");
    19. userService1.doService();
    20. }

    根据名称+类型来拿 bean

    1. package com.example.ioc;
    2. import com.example.ioc.controller.UserController;
    3. import com.example.ioc.service.UserService;
    4. import org.springframework.boot.SpringApplication;
    5. import org.springframework.boot.autoconfigure.SpringBootApplication;
    6. import org.springframework.context.ApplicationContext;
    7. @SpringBootApplication
    8. public class DemoApplication {
    9. public static void main(String[] args) {
    10. //Spring 上下文
    11. ApplicationContext context = SpringApplication.run(DemoApplication.class, args);
    12. //返回的就是 Spring 的运行环境
    13. UserController bean = context.getBean(UserController.class);
    14. bean.doController();
    15. UserService userService = context.getBean(UserService.class);
    16. userService.doService();
    17. //根据名称来获取bean
    18. UserService userService1 = (UserService) context.getBean("userService");
    19. userService1.doService();
    20. //根据名称和类型来获取 bean
    21. UserService userService2 = context.getBean("userService",UserService.class);
    22. userService2.doService();
    23. }

    ③  @Repository

    1. package com.example.ioc.repo;
    2. import org.springframework.stereotype.Repository;
    3. @Repository
    4. public class UserRepository {
    5. public void doRepository(){
    6. System.out.println("do repo...");
    7. }
    8. }
    1. package com.example.ioc;
    2. import com.example.ioc.controller.UserController;
    3. import com.example.ioc.repo.UserRepository;
    4. import com.example.ioc.service.UserService;
    5. import org.springframework.boot.SpringApplication;
    6. import org.springframework.boot.autoconfigure.SpringBootApplication;
    7. import org.springframework.context.ApplicationContext;
    8. @SpringBootApplication
    9. public class DemoApplication {
    10. public static void main(String[] args) {
    11. //Spring 上下文
    12. ApplicationContext context = SpringApplication.run(DemoApplication.class, args);
    13. //返回的就是 Spring 的运行环境
    14. UserController bean = context.getBean(UserController.class);
    15. bean.doController();
    16. UserService userService = context.getBean(UserService.class);
    17. userService.doService();
    18. //根据名称来获取bean
    19. UserService userService1 = (UserService) context.getBean("userService");
    20. userService1.doService();
    21. //根据名称和类型来获取 bean
    22. UserService userService2 = context.getBean("userService",UserService.class);
    23. userService2.doService();
    24. UserRepository userRepository = context.getBean(UserRepository.class);
    25. userRepository.doRepository();
    26. }

    ④  @Component

    1. package com.example.ioc.component;
    2. import org.springframework.stereotype.Component;
    3. @Component
    4. public class UserComponent {
    5. public void doComponent(){
    6. System.out.println("do component");
    7. }
    8. }
    1. package com.example.ioc;
    2. import com.example.ioc.component.UserComponent;
    3. import com.example.ioc.controller.UserController;
    4. import com.example.ioc.repo.UserRepository;
    5. import com.example.ioc.service.UserService;
    6. import org.springframework.boot.SpringApplication;
    7. import org.springframework.boot.autoconfigure.SpringBootApplication;
    8. import org.springframework.context.ApplicationContext;
    9. @SpringBootApplication
    10. public class DemoApplication {
    11. public static void main(String[] args) {
    12. //Spring 上下文
    13. ApplicationContext context = SpringApplication.run(DemoApplication.class, args);
    14. //返回的就是 Spring 的运行环境
    15. UserController bean = context.getBean(UserController.class);
    16. bean.doController();
    17. UserService userService = context.getBean(UserService.class);
    18. userService.doService();
    19. //根据名称来获取bean
    20. UserService userService1 = (UserService) context.getBean("userService");
    21. userService1.doService();
    22. //根据名称和类型来获取 bean
    23. UserService userService2 = context.getBean("userService",UserService.class);
    24. userService2.doService();
    25. UserRepository userRepository = context.getBean(UserRepository.class);
    26. userRepository.doRepository();
    27. UserComponent userComponent = context.getBean(UserComponent.class);
    28. userComponent.doComponent();
    29. }
    30. }

    ⑤  @Configuration

    1. package com.example.ioc.config;
    2. import org.springframework.context.annotation.Configuration;
    3. @Configuration
    4. public class UserConfig {
    5. public void doConfig(){
    6. System.out.println("do configurarion...");
    7. }
    8. }
    1. package com.example.ioc;
    2. import com.example.ioc.component.UserComponent;
    3. import com.example.ioc.config.UserConfig;
    4. import com.example.ioc.controller.UserController;
    5. import com.example.ioc.repo.UserRepository;
    6. import com.example.ioc.service.UserService;
    7. import org.springframework.boot.SpringApplication;
    8. import org.springframework.boot.autoconfigure.SpringBootApplication;
    9. import org.springframework.context.ApplicationContext;
    10. @SpringBootApplication
    11. public class DemoApplication {
    12. public static void main(String[] args) {
    13. //Spring 上下文
    14. ApplicationContext context = SpringApplication.run(DemoApplication.class, args);
    15. //返回的就是 Spring 的运行环境
    16. UserController bean = context.getBean(UserController.class);
    17. bean.doController();
    18. UserService userService = context.getBean(UserService.class);
    19. userService.doService();
    20. //根据名称来获取bean
    21. UserService userService1 = (UserService) context.getBean("userService");
    22. userService1.doService();
    23. //根据名称和类型来获取 bean
    24. UserService userService2 = context.getBean("userService",UserService.class);
    25. userService2.doService();
    26. UserRepository userRepository = context.getBean(UserRepository.class);
    27. userRepository.doRepository();
    28. UserComponent userComponent = context.getBean(UserComponent.class);
    29. userComponent.doComponent();
    30. UserConfig userConfig = context.getBean(UserConfig.class);
    31. userConfig.doConfig();
    32. }
    33. }

  • 相关阅读:
    【二进制部署k8s-1.29.4】八、worker端安装kubelet和cotainerd
    枚举最大值+ds:1887D
    深度学习图像增强和模型微调
    Linux Centos 根目录扩展分区(保级教程)
    k8s部署mysql一主两从
    【超好懂的比赛题解】第 45 届国际大学生程序设计竞赛(ICPC)亚洲区域赛(济南)
    docker的疑难杂症
    c++类相互引用的问题,如何避免头文件相互包含
    vue3语法笔记
    Python用户认证JWT——PyJWT
  • 原文地址:https://blog.csdn.net/qq_40841463/article/details/134441566