查看“@Autowired”的源代码
←
@Autowired
跳到导航
跳到搜索
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
[[category:Spring]] == 关于 == <span style="color: blue; font-size: 150%">'''@Autowired'''</span> 是属于 Spring 的容器配置的一个注解,与它同属容器配置的注解还有:@Required、@Primary、@Qualifier 等等。 @Autowired:字面意义即“'''自动装配'''”。在 Spring 的世界当中,自动装配指的就是“'''将 Spring 容器中的 bean 自动的和我们需要这个 bean 的类组装在一起'''”。 === 与 @Resource 区别 === 简单来说:<span style="color: blue; font-size: 120%">'''@Resource'''</span> 相当于“@Autowired”和“@Qualifier” 一起使用。 比较: ::{| class="wikitable" ! !! @Autowired !! @Resource |- | '''相同''' | colspan="2" style="text-align:center;" | 都是通过注解实现依赖注入。 |- | rowspan="3" | '''不同''' | 由 '''Spring''' 定义 | 由 '''JSR-250'''(Java 注解规范)定义 |- | 默认方式:'''ByType''',可以配合“@Qualifier”使用 ByName 方式 | 默认方式:'''ByName''',如果找不到则使用 ByType 方式 |- | 可以对:'''构造器'''、'''方法'''、'''参数'''、'''字段'''使用 | 只能对:'''方法'''、'''字段'''使用 |} === 相关注解 === # <span style="color: blue">'''@Required'''</span>:与 @Autowired 配合使用,用于指定是否必须有 Bean 候选者。 #* 与“@Autowired(required=true)”作用一样; # <span style="color: blue">'''@Qualifier'''</span>:与 @Autowired 配合使用,用于指定 Bean Name 来强制使用名称装配。 #* 可以用于解决“<span style="color: green">'''依赖对象冲突'''</span>”(即,具有多个符合条件的依赖对象); # <span style="color: blue">'''@Primary'''</span>:与 '''@Component''' 配合使用,用于指定该类型(ByType)下的“默认注入对象”。 #* 可以用于解决“<span style="color: green">'''依赖对象冲突'''</span>”(即,具有多个符合条件的依赖对象); # <span style="color: blue">'''@Lazy'''</span>:与 @Autowired、'''@Component''' 配合使用,用于“推迟‘带注解的 bean’和‘带注释的 @Autowired 位置’的初始化”。 #* 可以用于解决“<span style="color: green">'''循环依赖'''</span>”(即,A 依赖 B 的同时 B 依赖 A); == 常见方式 == @Autowired 相对于 xml 方式的注入,特点仅仅是“自动装配”而无需通过 xml 配置类间关系,它与 xml 方式同样有'''不同的注入方式'''。 === 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注入 === '''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 注入的一个引人注目的用例。 == 其他方式 == == 附:其他注入 == DI(Dependency Injection):依赖注入,使用 IOC 创建类的过程中,将类所依赖的属性设置进去。 注入的内容是“类所依赖的属性”:“属性”并一定是“类”,也可以是“'''基本类型'''”。
返回至“
@Autowired
”。
导航菜单
个人工具
登录
命名空间
页面
讨论
大陆简体
已展开
已折叠
查看
阅读
查看源代码
查看历史
更多
已展开
已折叠
搜索
导航
首页
最近更改
随机页面
MediaWiki帮助
笔记
服务器
数据库
后端
前端
工具
《To do list》
日常
阅读
电影
摄影
其他
Software
Windows
WIKIOE
所有分类
所有页面
侧边栏
站点日志
工具
链入页面
相关更改
特殊页面
页面信息