• Spring IoC


    Spring 是一个包含众多工具 IoC 容器

    Spring 两大核心思想 : 1. IoC  2.AOP (重要面试题)

    容器 : List/map 是装数据的容器

              Tomcat 是装web的容器

    Spring 容器装的是对象

    IoC 控制反转=>控制权反转=>创建对象的控制权交给了Spring

    Spring 是 IoC 容器,也就是控制反转的容器,创建对象的控制权交给了 Spring ,由Spring 来帮我们创建管理这些对象

    现在我们写一个案例,一辆车的生产,这是方法1,分成五个类

    1. package com.example.ioc.ioc.v1;
    2. public class Main {
    3. public static void main(String[] args) {
    4. Car car = new Car(17);
    5. car.run();
    6. Car car2 = new Car(19);
    7. car.run();
    8. }
    9. }
    10. package com.example.ioc.ioc.v1;
    11. public class Car {
    12. private Framework framework;
    13. public Car(int size) {
    14. framework = new Framework(size);
    15. System.out.println("car init...");
    16. }
    17. public void run(){
    18. System.out.println("car run...");
    19. }
    20. }
    21. package com.example.ioc.ioc.v1;
    22. public class Framework {
    23. private Bottom bottom;
    24. public Framework(int size) {
    25. bottom = new Bottom(size);
    26. System.out.println("framework init...");
    27. }
    28. }
    29. package com.example.ioc.ioc.v1;
    30. public class Bottom {
    31. private Tire tire;
    32. public Bottom(int size){
    33. tire = new Tire(size);
    34. System.out.println("bottom init...");
    35. }
    36. }
    37. package com.example.ioc.ioc.v1;
    38. public class Tire {
    39. private int size ;
    40. public Tire(int size){
    41. this.size = size;
    42. System.out.println("tire init...size:"+size);
    43. }
    44. }

     下面是方法2:

    1. package com.example.ioc.v2;
    2. public class Main {
    3. public static void main(String[] args) {
    4. Tire tire = new Tire(17,"red");
    5. Bottom bottom = new Bottom(tire);
    6. Framework framework = new Framework(bottom);
    7. Car car = new Car(framework);
    8. car.run();
    9. }
    10. }
    11. package com.example.ioc.v2;
    12. public class Car {
    13. private Framework framework;
    14. public Car(Framework framework) {
    15. this.framework = framework;
    16. System.out.println("car init...");
    17. }
    18. public void run(){
    19. System.out.println("car run...");
    20. }
    21. }
    22. package com.example.ioc.v2;
    23. public class Framework {
    24. private Bottom bottom;
    25. public Framework(Bottom bottom) {
    26. this.bottom = bottom;
    27. System.out.println("framework init...");
    28. }
    29. }
    30. package com.example.ioc.v2;
    31. public class Bottom {
    32. private Tire tire;
    33. public Bottom(Tire tire) {
    34. this.tire = tire;
    35. System.out.println("bottom init...");
    36. }
    37. }
    38. package com.example.ioc.v2;
    39. public class Tire {
    40. private int size ;
    41. private String color;
    42. public Tire(int size,String color){
    43. this.size = size;
    44. this.color = color;
    45. System.out.println("tire init...size:"+size);
    46. System.out.println("tire init...color:"+color);
    47. }
    48. }

    方法二的耦合度更低,相互之间的影响更小

    IoC 是一种思想,DI(依赖注入)是一种实现方式,比如我们对上一篇图书管理系统的代码进行修改

  • 相关阅读:
    Prometheus的remotewrite for java
    神经网络的优化器
    HMS Core Discovery第17期直播预告|音随我动,秒变音色造型师
    中级软件设计师考试(软考中级)标准化和软件知识产权
    SpringBoot集成kubernetes-client升级k8s后初始化失败问题
    16-CSS3
    MAUI android连接sqlserver
    Java中单链表的创建
    主动人机交互与被动人机交互
    python开发之个人微信机器人的开发
  • 原文地址:https://blog.csdn.net/qq_40841463/article/details/134422562