三种注入方式

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 

推荐使用构造器注入

上一篇:简易数字基带信号系统仿真设计的matlab实现(带眼图)


下一篇:HBase过滤器及其代码示例