_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 。

相关文章
|
2天前
|
数据采集 人工智能 安全
|
11天前
|
云安全 监控 安全
|
3天前
|
自然语言处理 API
万相 Wan2.6 全新升级发布!人人都能当导演的时代来了
通义万相2.6全新升级,支持文生图、图生视频、文生视频,打造电影级创作体验。智能分镜、角色扮演、音画同步,让创意一键成片,大众也能轻松制作高质量短视频。
1020 151
|
3天前
|
编解码 人工智能 机器人
通义万相2.6,模型使用指南
智能分镜 | 多镜头叙事 | 支持15秒视频生成 | 高品质声音生成 | 多人稳定对话
|
16天前
|
机器学习/深度学习 人工智能 自然语言处理
Z-Image:冲击体验上限的下一代图像生成模型
通义实验室推出全新文生图模型Z-Image,以6B参数实现“快、稳、轻、准”突破。Turbo版本仅需8步亚秒级生成,支持16GB显存设备,中英双语理解与文字渲染尤为出色,真实感和美学表现媲美国际顶尖模型,被誉为“最值得关注的开源生图模型之一”。
1714 9
|
8天前
|
人工智能 自然语言处理 API
一句话生成拓扑图!AI+Draw.io 封神开源组合,工具让你的效率爆炸
一句话生成拓扑图!next-ai-draw-io 结合 AI 与 Draw.io,通过自然语言秒出架构图,支持私有部署、免费大模型接口,彻底解放生产力,绘图效率直接爆炸。
658 152
|
10天前
|
人工智能 安全 前端开发
AgentScope Java v1.0 发布,让 Java 开发者轻松构建企业级 Agentic 应用
AgentScope 重磅发布 Java 版本,拥抱企业开发主流技术栈。
622 12
|
10天前
|
人工智能 自然语言处理 API
Next AI Draw.io:当AI遇见Draw.io图表绘制
Next AI Draw.io 是一款融合AI与图表绘制的开源工具,基于Next.js实现,支持自然语言生成架构图、流程图等专业图表。集成多款主流大模型,提供智能绘图、图像识别优化、版本管理等功能,部署简单,安全可控,助力技术文档与系统设计高效创作。
692 151