构造函数的引入会造成循环依赖

    1. import org.springframework.stereotype.Component;
    2. /**
    3. * @author tn
    4. * @version 1
    5. * @ClassName Sa
    6. * @description 测试A
    7. * @date 2020/11/13 11:23
    8. */
    9. @Component
    10. public class Sa {
    11. private Sb sb;
    12. public Sa(Sb sb) {
    13. this.sb = sb;
    14. }
    15. }
    16. import org.springframework.stereotype.Component;
    17. /**
    18. * @author tn
    19. * @version 1
    20. * @ClassName Sb
    21. * @description 测试B
    22. * @date 2020/11/13 11:23
    23. */
    24. @Component
    25. public class Sb {
    26. private Sc sc;
    27. public Sb(Sc sc) {
    28. this.sc = sc;
    29. }
    30. }
    31. import org.springframework.stereotype.Component;
    32. /**
    33. * @author tn
    34. * @version 1
    35. * @ClassName Sc
    36. * @description 测试C
    37. * @date 2020/11/13 11:23
    38. */
    39. @Component
    40. public class Sc {
    41. private Sa sa;
    42. public Sc(Sa sa) {
    43. this.sa = sa;
    44. }
    45. }
    46. Description:
    47. The dependencies of some of the beans in the application context form a cycle:
    48. ┌─────┐
    49. | sa defined in file [D:\project\java\my\test\stest\loop_1\target\classes\com\tn\loop_1\test\Sa.class]
    50. | sb defined in file [D:\project\java\my\test\stest\loop_1\target\classes\com\tn\loop_1\test\Sb.class]
    51. | sc defined in file [D:\project\java\my\test\stest\loop_1\target\classes\com\tn\loop_1\test\Sc.class]
    52. └─────┘
    53. Process finished with exit code 1

    使用 注解 就不会出现循环依赖,但是使用时可能会报错(下面的例子,会疯狂的互相调用)

    1. package com.tn.loop_1.test;
    2. import org.springframework.beans.factory.annotation.Autowired;
    3. import org.springframework.stereotype.Component;
    4. /**
    5. * @author tn
    6. * @version 1
    7. * @ClassName Sa
    8. * @description 测试A
    9. * @date 2020/11/13 11:23
    10. */
    11. @Component
    12. public class Sa1 {
    13. @Autowired
    14. private Sb1 sb1;
    15. public void print(){
    16. System.out.println("测试A");
    17. sb1.print();
    18. }
    19. }
    20. package com.tn.loop_1.test;
    21. import org.springframework.beans.factory.annotation.Autowired;
    22. import org.springframework.stereotype.Component;
    23. /**
    24. * @author tn
    25. * @version 1
    26. * @ClassName Sb
    27. * @description 测试B
    28. * @date 2020/11/13 11:23
    29. */
    30. @Component
    31. public class Sb1 {
    32. @Autowired
    33. private Sc1 sc1;
    34. public void print(){
    35. System.out.println("测试B");
    36. sc1.print();
    37. }
    38. }
    39. package com.tn.loop_1.test;
    40. import org.springframework.beans.factory.annotation.Autowired;
    41. import org.springframework.stereotype.Component;
    42. /**
    43. * @author tn
    44. * @version 1
    45. * @ClassName Sc
    46. * @description 测试C
    47. * @date 2020/11/13 11:23
    48. */
    49. @Component
    50. public class Sc1 {
    51. @Autowired
    52. private Sa1 sa1;
    53. public void print(){
    54. System.out.println("测试C");
    55. sa1.print();
    56. }
    57. }