_bean 的自动装配| 学习笔记

简介: 快速学习 _bean 的自动装配

开发者学堂课程【Java Web 开发系列课程:Spring 框架入门_bean 的自动装配】学习笔记,与课程紧密联系,让用户快速学习知识。

课程地址:https://developer.aliyun.com/learning/course/538/detail/7340


_bean 的自动装配

 

内容简介:

一、自动装配的介绍

二、自动装配---简化 spring 配置文件


 一、自动装配的介绍

Autowiring collaborators

The Spring container can autowire relationships between collaborating beans. You can allow Spring to resolve collaborators (other beans)automatically for your bean by inspecting the contents of the ApplicationContext.Autowiring has the following advantages:

Autowiring can significantly reduce the need to specify properties or constructor arguments.(Other mechanisms such as a bean template discussed elsewhere in this chapter are also valuable in this regard.)

Autowiring can update a configuration as your objects evolve. For example, if you need to add a dependency to a class, that dependency can be satisfied automatically without you needing to modify the configuration. Thus autowiring can be especially useful during development, without negating the option of switching to explicit wiring when the code base becomes more stable.

When using XML-based configuration metadata 10,you specify autowire mode for a bean definition with the autowire attribute of the  element. The autowiring functionality has five modes. You specify autowiring per bean and thus can choose which ones to autowire.

译文:自动装配

Spring 容器可以自动创建协作 bean 之间的关系。通过检查ApplicationContext 的内容,您可以允许 Spring 自动解析 bean的协作者(其他 bean )。

自动装配具有以下优点:

自动装配可以大大减少指定属性或构造函数参数的需要(本章其他部分讨论的 bean 模板等其他机制在这方面也很有价值)。自动装配可以随着对象的发展而更新配置。

例如,如果需要添加依赖于类,则可以自动满足该依赖关系,而无需修改配置。

因此,自动装配在开发过程中可能特别有用,而不会在代码库变得更稳定时拒绝切换到显式连接的选项。

当使用基于 xml 的配置元数据10时,可以使用 bean 的 autowire 属性为 bean 定义指定 autoire 模式。

 

二、自动装配---简化 spring 配置文件

在配置 bean 时,可以配置 bean 的 autowire 属性,用于指定装配类型

相关代码

class="cn.sxt.service.impl.UserServiceImpl"

autowire="constructor"/>

可以配害全局的自动装配类型。在头部 default-autowire

代码如下

xmIns="http://www.springframework.org/schen a/beans’’

xmIns;xsi="http://www.w3.org/2001/XMLSchema -instance''

xsi:schemaLocation="http://www.springframew ork.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd"default- autowire=*byName"

推荐不使用自动装配,而使用 annotation 。

相关文章
|
3天前
|
云安全 人工智能 自然语言处理
|
7天前
|
人工智能 Java API
Java 正式进入 Agentic AI 时代:Spring AI Alibaba 1.1 发布背后的技术演进
Spring AI Alibaba 1.1 正式发布,提供极简方式构建企业级AI智能体。基于ReactAgent核心,支持多智能体协作、上下文工程与生产级管控,助力开发者快速打造可靠、可扩展的智能应用。
753 17
|
11天前
|
数据采集 人工智能 自然语言处理
Meta SAM3开源:让图像分割,听懂你的话
Meta发布并开源SAM 3,首个支持文本或视觉提示的统一图像视频分割模型,可精准分割“红色条纹伞”等开放词汇概念,覆盖400万独特概念,性能达人类水平75%–80%,推动视觉分割新突破。
782 59
Meta SAM3开源:让图像分割,听懂你的话
|
1天前
|
人工智能 安全 小程序
阿里云无影云电脑是什么?最新收费价格个人版、企业版和商业版无影云电脑收费价格
阿里云无影云电脑是运行在云端的虚拟电脑,分企业版和个人版。企业版适用于办公、设计等场景,4核8G配置低至199元/年;个人版适合游戏、娱乐,黄金款14元/月起。支持多端接入,灵活按需使用。
231 164
|
8天前
|
搜索推荐 编译器 Linux
一个可用于企业开发及通用跨平台的Makefile文件
一款适用于企业级开发的通用跨平台Makefile,支持C/C++混合编译、多目标输出(可执行文件、静态/动态库)、Release/Debug版本管理。配置简洁,仅需修改带`MF_CONFIGURE_`前缀的变量,支持脚本化配置与子Makefile管理,具备完善日志、错误提示和跨平台兼容性,附详细文档与示例,便于学习与集成。
330 116
|
2天前
|
机器学习/深度学习 人工智能 自然语言处理
Z-Image:冲击体验上限的下一代图像生成模型
通义实验室推出全新文生图模型Z-Image,以6B参数实现“快、稳、轻、准”突破。Turbo版本仅需8步亚秒级生成,支持16GB显存设备,中英双语理解与文字渲染尤为出色,真实感和美学表现媲美国际顶尖模型,被誉为“最值得关注的开源生图模型之一”。
296 3
|
5天前
|
弹性计算 搜索推荐 应用服务中间件
阿里云服务器租用价格:一年、1小时及一个月收费标准及优惠活动参考
阿里云服务器优惠汇总:轻量应用服务器200M带宽38元/年起,ECS云服务器2核2G 99元/年、2核4G 199元/年,4核16G 89元/月,8核32G 160元/月,香港轻量服务器25元/月起,支持按小时计费,新老用户同享,续费同价,限时秒杀低至1折。
399 166

热门文章

最新文章