Spring——@Autowired注解使用说明 系统管理员 2024-04-19 12:11 16阅读 0赞 ### 方式一,放在构造方法上使用 ### **代码示例** private StudentService studentService; @Autowired public StudentDemoService(StudentService studentService){ this.studentService = studentService; } 注意:当类中只有一个构造方法是可以省略@Autowired注解,但是有多个构造方法的时候,必须在多个构造方法中的其中一个标明@Autowired注解 [官方文档连接][Link 1] **官方文档说明截图:** ![20190918104116134.png][] ### 方式二、放在set方法上使用 ### private StudentService studentService; @Autowired public void setStudentDemoService(StudentService studentService){ this.studentService = studentService; } ** 官网说明截图** ![watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM3MzA2MDQx_size_16_color_FFFFFF_t_70][] ### 方式三、放在普通方法上使用 ### private StudentService studentService; @Autowired public void InjectTest(StudentService studentService){ this.studentService = studentService; } ** 官网说明截图** ![watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM3MzA2MDQx_size_16_color_FFFFFF_t_70 1][] ### 方式四、放在类中的属性使用 ### @Autowired private StudentService studentService; [Link 1]: https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#beans-autowired-annotation [20190918104116134.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/04/18/ab5fed4a14144b4bac90f6cf50360c5a.png [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM3MzA2MDQx_size_16_color_FFFFFF_t_70]: https://image.dandelioncloud.cn/pgy_files/images/2024/04/18/46fcc9fbe3914af3afd01addae93c643.png [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM3MzA2MDQx_size_16_color_FFFFFF_t_70 1]: https://image.dandelioncloud.cn/pgy_files/images/2024/04/18/529202d69ddc4306844b1249f237db9a.png
还没有评论,来说两句吧...