“@Autowired”的版本间差异
跳到导航
跳到搜索
(创建页面,内容为“category:Spring == 关于 == <span style="color: blue; font-size: 150%">'''@Autowired'''</span> 是属于 Spring 的容器配置的一个注解,与它同属容器配置的注解还有:@Required、@Primary、@Qualifier 等等。 @Autowired:字面意义即“'''自动装配'''”。在 Spring 的世界当中,自动装配指的就是“'''将 Spring 容器中的 bean 自动的和我们需要这个 bean 的类组装在一起'''”。 === 与 @Reso…”) |
(→常见方式) |
||
第41行: | 第41行: | ||
=== Field 注入 === | === Field 注入 === | ||
最常见的注入方式。 | |||
示例: | |||
: <syntaxhighlight lang="Java" highlight=""> | |||
Controller | |||
public class FooController { | |||
@Autowired | |||
private FooService fooService; | |||
} | |||
</syntaxhighlight> | |||
'''优点:'''代码简洁 | |||
'''缺点:''' | |||
=== 构造器注入 === | === 构造器注入 === | ||
'''Spring 4.x''' 版本中推荐的注入方式。 | |||
示例: | |||
: <syntaxhighlight lang="Java" highlight=""> | |||
Controller | |||
public class FooController { | |||
private final FooService fooService; | |||
@Autowired | |||
public FooController(FooService fooService) { | |||
this.fooService = fooService; | |||
} | |||
} | |||
</syntaxhighlight> | |||
'''优点:''' | |||
'''缺点:''' | |||
=== Setter注入 === | === Setter注入 === | ||
'''Spring 3.x''' 版本中推荐的注入方式。 | |||
示例: | |||
: <syntaxhighlight lang="Java" highlight=""> | |||
@Controller | |||
public class FooController { | |||
private FooService fooService; | |||
@Autowired | |||
public void setFooService(FooService fooService) { | |||
this.fooService = fooService; | |||
} | |||
} | |||
</syntaxhighlight> | |||
'''优点:'''可以让类在之后重新配置和重新注入。 | |||
'''缺点:''' | |||
== 构造器注入 or Setter 注入? == | |||
The Spring team generally advocates constructor injection as it enables one to implement application components as immutable objects and to ensure that required dependencies are not null. Furthermore constructor-injected components are always returned to client (calling) code in a fully initialized state. As a side note, a large number of constructor arguments is a bad code smell, implying that the class likely has too many responsibilities and should be refactored to better address proper separation of concerns. | |||
Spring 团队通常提倡构造函数注入,因为它允许将应用程序组件实现为不可变对象,并确保所需的依赖项不为空。此外,构造函数注入的组件总是以完全初始化的状态返回给客户端(调用)代码。顺便说一句,大量的构造函数参数是一种糟糕的代码气味,这意味着类可能有太多的责任,应该进行重构以更好地解决适当的关注点分离问题。 | |||
Setter injection should primarily only be used for optional dependencies that can be assigned reasonable default values within the class. Otherwise, not-null checks must be performed everywhere the code uses the dependency. One benefit of setter injection is that setter methods make objects of that class amenable to reconfiguration or re-injection later. Management through JMX MBeans is therefore a compelling use case for setter injection. | |||
Setter注入应该主要用于可选依赖项,这些依赖项可以在类中分配合理的默认值。否则,在代码使用依赖项的任何地方都必须执行非空检查。setter 注入的一个好处是 setter 方法使该类的对象可以稍后重新配置或重新注入。因此,通过 JMX MBean 进行管理是 setter 注入的一个引人注目的用例。 | |||
== 其他方式 == | == 其他方式 == |
2022年12月8日 (四) 15:55的版本
关于
@Autowired 是属于 Spring 的容器配置的一个注解,与它同属容器配置的注解还有:@Required、@Primary、@Qualifier 等等。
@Autowired:字面意义即“自动装配”。在 Spring 的世界当中,自动装配指的就是“将 Spring 容器中的 bean 自动的和我们需要这个 bean 的类组装在一起”。
与 @Resource 区别
简单来说:@Resource 相当于“@Autowired”和“@Qualifier” 一起使用。
比较:
@Autowired @Resource 相同 都是通过注解实现依赖注入。 不同 由 Spring 定义 由 JSR-250(Java 注解规范)定义 默认方式:ByType,可以配合“@Qualifier”使用 ByName 方式 默认方式:ByName,如果找不到则使用 ByType 方式 可以对:构造器、方法、参数、字段使用 只能对:方法、字段使用
相关注解
- @Required:与 @Autowired 配合使用,用于指定是否必须有 Bean 候选者。
- 与“@Autowired(required=true)”作用一样;
- @Qualifier:与 @Autowired 配合使用,用于指定 Bean Name 来强制使用名称装配。
- 可以用于解决“依赖对象冲突”(即,具有多个符合条件的依赖对象);
- @Primary:与 @Component 配合使用,用于指定该类型(ByType)下的“默认注入对象”。
- 可以用于解决“依赖对象冲突”(即,具有多个符合条件的依赖对象);
- @Lazy:与 @Autowired、@Component 配合使用,用于“推迟‘带注解的 bean’和‘带注释的 @Autowired 位置’的初始化”。
- 可以用于解决“循环依赖”(即,A 依赖 B 的同时 B 依赖 A);
常见方式
@Autowired 相对于 xml 方式的注入,特点仅仅是“自动装配”而无需通过 xml 配置类间关系,它与 xml 方式同样有不同的注入方式。
Field 注入
最常见的注入方式。
示例:
Controller public class FooController { @Autowired private FooService fooService; }
优点:代码简洁 缺点:
构造器注入
Spring 4.x 版本中推荐的注入方式。
示例:
Controller public class FooController { private final FooService fooService; @Autowired public FooController(FooService fooService) { this.fooService = fooService; } }
优点: 缺点:
Setter注入
Spring 3.x 版本中推荐的注入方式。
示例:
@Controller public class FooController { private FooService fooService; @Autowired public void setFooService(FooService fooService) { this.fooService = fooService; } }
优点:可以让类在之后重新配置和重新注入。 缺点:
构造器注入 or Setter 注入?
The Spring team generally advocates constructor injection as it enables one to implement application components as immutable objects and to ensure that required dependencies are not null. Furthermore constructor-injected components are always returned to client (calling) code in a fully initialized state. As a side note, a large number of constructor arguments is a bad code smell, implying that the class likely has too many responsibilities and should be refactored to better address proper separation of concerns. Spring 团队通常提倡构造函数注入,因为它允许将应用程序组件实现为不可变对象,并确保所需的依赖项不为空。此外,构造函数注入的组件总是以完全初始化的状态返回给客户端(调用)代码。顺便说一句,大量的构造函数参数是一种糟糕的代码气味,这意味着类可能有太多的责任,应该进行重构以更好地解决适当的关注点分离问题。
Setter injection should primarily only be used for optional dependencies that can be assigned reasonable default values within the class. Otherwise, not-null checks must be performed everywhere the code uses the dependency. One benefit of setter injection is that setter methods make objects of that class amenable to reconfiguration or re-injection later. Management through JMX MBeans is therefore a compelling use case for setter injection. Setter注入应该主要用于可选依赖项,这些依赖项可以在类中分配合理的默认值。否则,在代码使用依赖项的任何地方都必须执行非空检查。setter 注入的一个好处是 setter 方法使该类的对象可以稍后重新配置或重新注入。因此,通过 JMX MBean 进行管理是 setter 注入的一个引人注目的用例。
其他方式
附:其他注入
DI(Dependency Injection):依赖注入,使用 IOC 创建类的过程中,将类所依赖的属性设置进去。 注入的内容是“类所依赖的属性”:“属性”并一定是“类”,也可以是“基本类型”。