在Spring容器中为一个bean配置依赖注入有三种方式:

  • 使用属性的setter方法注入
  • 使用构造器注入;
  • 注解方式

1. 使用属性的setter方法注入

也就是基于xml的开发
spring依赖注入(IOC)
spring依赖注入(IOC)

2. 使用字段(Filed)注入(用注解方式)

spring依赖注入(IOC)

spring依赖注入(IOC)

spring依赖注入(IOC)

3. 构造器注入:

通过将@Autowired注解放在构造器上来完成构造器注入,默认构造器参数通过类型自动装配,

spring依赖注入(IOC)

相关文章: