Spring——@Autowired注解使用说明

系统管理员 2024-04-19 12:11 165阅读 0赞

方式一,放在构造方法上使用

代码示例

  1. private StudentService studentService;
  2. @Autowired
  3. public StudentDemoService(StudentService studentService){
  4. this.studentService = studentService;
  5. }

注意:当类中只有一个构造方法是可以省略@Autowired注解,但是有多个构造方法的时候,必须在多个构造方法中的其中一个标明@Autowired注解

官方文档连接

官方文档说明截图:

20190918104116134.png

方式二、放在set方法上使用

  1. private StudentService studentService;
  2. @Autowired
  3. public void setStudentDemoService(StudentService studentService){
  4. this.studentService = studentService;
  5. }

官网说明截图

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM3MzA2MDQx_size_16_color_FFFFFF_t_70

方式三、放在普通方法上使用

  1. private StudentService studentService;
  2. @Autowired
  3. public void InjectTest(StudentService studentService){
  4. this.studentService = studentService;
  5. }

官网说明截图

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM3MzA2MDQx_size_16_color_FFFFFF_t_70 1

方式四、放在类中的属性使用

  1. @Autowired
  2. private StudentService studentService;

发表评论

表情:
评论列表 (有 0 条评论,165人围观)

还没有评论,来说两句吧...

相关阅读

    相关 注解说明

    因为对于一些注解不太了解,做下记录说明 ===================================================================

    相关 SpringBoot 注解说明

    1, @Configuration 注解: >      加在类上的注解,将某个类A定义成配置类,这样SpringBoot在启动main方法后就会自动实例化一个类A的对象。常