Spring——@Autowired注解使用说明
方式一,放在构造方法上使用
代码示例
private StudentService studentService;
@Autowired
public StudentDemoService(StudentService studentService){
this.studentService = studentService;
}
注意:当类中只有一个构造方法是可以省略@Autowired注解,但是有多个构造方法的时候,必须在多个构造方法中的其中一个标明@Autowired注解
官方文档连接
官方文档说明截图:
方式二、放在set方法上使用
private StudentService studentService;
@Autowired
public void setStudentDemoService(StudentService studentService){
this.studentService = studentService;
}
官网说明截图
方式三、放在普通方法上使用
private StudentService studentService;
@Autowired
public void InjectTest(StudentService studentService){
this.studentService = studentService;
}
官网说明截图
方式四、放在类中的属性使用
@Autowired
private StudentService studentService;
还没有评论,来说两句吧...