栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

何时在Spring中使用自动装配

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

何时在Spring中使用自动装配

我相信这里有两件事令人困惑。在本章中,“自动装配”的含义是标记Bean,以自动检测和注入依赖项。这可以通过设置“ autowire” bean属性来实现。

实际上,这与

@Autowired
在显式指示字段或设置器的位置进行依赖注入相反。

在这里看看:http :
//static.springsource.org/spring/docs/3.1.x/spring-framework-
reference/html/beans.html#beans-factory-
autowire


解释一下,假设你有

public class SnippetService {    private TestService testService;    public Snippet getSnippet() {        return testService.getSnippet();    }    public void setTestService(TestService testService) {      this.testService = testService;    }}

如果定义了bean:

<bean  autowire="byType"/>

TestService
在这种情况下,spring将通过调用setTestService setter 尝试注入匹配类型的bean
。即使您没有使用
@Autowired
。这确实很危险,因为某些二传手可能并不意味着在春季被召唤。

如果设置自动装配=“否”,什么都不会被注入,除非标记,以便有

@Autowired
@Resource
@Inject



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/516472.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号