1. 注解注入
@Controller
public class TestController {
@Autowired
private TestService testService;
}
注解注入,一般是注入另一个bean
2. 构造器注入
@Controller
public class TestController {
private final TestService testService;
@Autowired
public FooController(TestService testService) {
this.testService = testService;
}
}
构造器注入,配置bean一并配置constructor
3. setter注入
@Controller
public class TestController {
private TestService testService;
@Autowired
public void setFooService(TestService testService) {
this.testService = testService;
}
}
Setter注入,配置property
推荐使用构造器注入