02-springIOC01-注解方式实现

news/2025/10/4 23:37:45/文章来源:https://www.cnblogs.com/benzhong/p/19126064
02-springIOC01-注解方式实现

使用注解的方式实现IOC

定义两个实体类

/*** @ClassName Address* @Description TODO* @Author zhongge* @Version 1.0*/
public class Address {public String getLoc() {return loc;}public void setLoc(String loc) {this.loc = loc;}@Overridepublic String toString() {return "Address{" +"loc='" + loc + '\'' +'}';}private String loc;public Address(String loc) {this.loc = loc;}public Address() {}
}/*** @ClassName Student* @Description TODO* @Author zhongge* @Version 1.0*/
public class Student {public Student() {}@Overridepublic String toString() {return "Student{" +"address=" + address +", id=" + id +", name='" + name + '\'' +", age=" + age +'}';}public Address getAddress() {return address;}public void setAddress(Address address) {this.address = address;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}private Address address;//地址private Integer id;private String name;private Integer age;}

配置xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="address" class="com.zhongge.entity.Address"><property name="loc" value="贵州"></property></bean><bean id="student" class="com.zhongge.entity.Student"><property name="address" ref="address"></property><property name="id" value="1001"></property><property name="name" value="李四"></property><property name="age" value="19"></property></bean>
</beans>

测试结果

/*** @ClassName Main* @Description TODO* @Author zhongge* @Version 1.0*/
public class Main {public static void main(String[] args) {//获取IOC容器ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-context.xml");//从IOC容器中获取对象System.out.println(applicationContext.getBean("address"));System.out.println(applicationContext.getBean("student"));}
}

结果:
image

补充知识

1️⃣ 构造器注入(不需要无参构造)

如果你想用有参构造直接创建 Student 对象,可以用 <constructor-arg> 标签:

<bean id="address" class="com.zhongge.entity.Address"><property name="loc" value="贵州"/>
</bean><bean id="student" class="com.zhongge.entity.Student"><constructor-arg ref="address"/>         <!-- 第一个参数 --><constructor-arg value="1001"/>          <!-- 第二个参数 --><constructor-arg value="李四"/>          <!-- 第三个参数 --><constructor-arg value="19"/>            <!-- 第四个参数 -->
</bean>

特点:

  • 直接调用你定义的有参构造方法
  • 不需要无参构造
  • 参数顺序必须与构造方法一致

2️⃣ Setter 注入(需要无参构造)

这是你现在的写法,Spring 会:

  1. 调用无参构造创建对象
  2. 调用 setXxx() 方法注入属性
<bean id="address" class="com.zhongge.entity.Address"><property name="loc" value="贵州"/>
</bean><bean id="student" class="com.zhongge.entity.Student"><property name="address" ref="address"/><property name="id" value="1001"/><property name="name" value="李四"/><property name="age" value="19"/>
</bean>

特点:

  • 必须有无参构造
  • 属性名要与 setXxx() 方法对应

3️⃣ p 命名空间注入(简化 Setter 注入)

可以用 p: 前缀简化 <property> 写法:

<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="address" class="com.zhongge.entity.Address" p:loc="贵州"/><bean id="student" class="com.zhongge.entity.Student"p:address-ref="address"p:id="1001"p:name="李四"p:age="19"/>
</beans>

特点:

  • 写法更简洁
  • 本质还是 Setter 注入,所以仍需要无参构造

4️⃣ 集合类型注入(可选)

如果你的类中有集合属性,可以这样配置:

<bean id="student" class="com.zhongge.entity.Student"><property name="address" ref="address"/><property name="id" value="1001"/><property name="name" value="李四"/><property name="age" value="19"/><property name="hobbies"><list><value>篮球</value><value>游泳</value></list></property>
</bean>

总结

  • 构造器注入<constructor-arg>,不要求无参构造
  • Setter 注入<property>p: 命名空间,必须有无参构造
  • 你的 Student 类有有参构造,如果想用 <property> 注入,就必须显式添加无参构造

基于xml的方式的优缺点

  • 缺点:基于 XML 的缺点是过于复杂
  • 优点:基于 XML 的方式一个类可以创建多个对象

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/927685.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

国内买机票的网站建设有啥好玩的网页游戏

简介 TensorFlow是由Google团队开发的一个开源深度学习框架&#xff0c;完全基于Python语言设计。它的初衷是以最简单的方式实现机器学习和深度学习的概念&#xff0c;结合了计算代数的优化技术&#xff0c;使计算许多数学表达式变得简单。 优势&#xff1a; 强大的计算能力…

网站开发的原理西安 房产网站建设

注意&#xff1a;进行实例之前必须完成nginx的源码编译。&#xff08;阅读往期文章完成步骤&#xff09; 1.编辑nginx的配置文件&#xff0c;修改内容 [rootlocalhost ~]# vim /usr/local/nginx/conf/nginx.conf 2.创建新目录/usr/local/nginx/conf.d/&#xff0c;编辑新文件…

Say 题选记(9.28 - 10.4)

P5363 [SDOI2019] 移动金币 Staircase-Nim 加计数。 首先怎么转化成 Staircase-Nim 呢,可以把每个金币右边到下一个金币中间那些空的地方看成这个金币的石子,那么每次金币的向左移动就是把石子从右边金币的堆移到左边…

专题网站建设策划方案浙江省住房和城乡建设厅官方网站

1.IDEA概述 IDEA全称InelliJ IDEA,是用于java语言开发的集成环境&#xff0c;它是业界公认的目前用于Java程序开发最好的工具。 集成环境&#xff1a;把代码编写&#xff0c;编译&#xff0c;执行&#xff0c;调试扽过多种功能综合到一起的开发工具。 下载&#xff1a;https…

做北京会所网站哪个好商城平台

文章目录 网络拓扑安装使用代理服务器设置隐藏者设置 使用古老的ccproxy实现代理服务器&#xff0c;仅做实验用途&#xff0c;禁止做违法犯罪的事情&#xff0c;后果自负。 网络拓扑 均使用Windows Server 2003系统 Router 外网IP&#xff1a;使用NAT模式 IP DHCP自动分配或者…

Excel表设置为细框线

Sub 设置为细框线()边框类型数组 = Array(xlEdgeLeft, xlEdgeTop, xlEdgeBottom, _xlEdgeRight, xlInsideVertical, xlInsideHorizontal)遍历已使用区域的每个单元格For Each 单元格 In ActiveSheet.UsedRangeFor Each…

US$28.5 CG A11DS 3 Buttons Wire Remote Used with CGDI K2 Remote Key Programmer 5pcs/lot

CG A11DS 3 Buttons Wire Remote Used with CGDI K2 Remote Key Programmer 5pcs/lot Package includes:5pc x CG A11DS 3 Buttons Wire Remote Pictures of CG A11DS 3 Buttons Wire Remote Used with CGDI K2 Remote…

延边网站开发depawo怎样提高网站权重

作者&#xff1a;Pablo Samuel Castro、Marc G. Bellemare 来源&#xff1a;Google AI Blog,机器之心摘要&#xff1a;在过去几年里&#xff0c;强化学习研究取得了多方面的显著进展。在过去几年里&#xff0c;强化学习研究取得了多方面的显著进展。这些进展使得智能体能够以超…

前端学习教程-VIte整合ECharts

ECharts 是一个强大的开源数据可视化库,而 Vite 是现代前端构建工具,两者结合可以高效开发数据可视化应用。本教程实现从创建 Vite 项目到使用 ECharts 实现各种图表。 一、环境准备 1. 创建 Vite 项目 首先确保已安…

月牙河做网站公司域名对网站排名的影响

2019独角兽企业重金招聘Python工程师标准>>> 软件的一处功能用到EasyUI的表单提交&#xff0c;返回一串字符串&#xff0c;这串字符串里有一段HTML代码&#xff0c;正常的情况下这段HTML代码里的双引号“ 是用 \ 转义过的。在IE中没问题&#xff0c;但是在Firefox和…

网站开发安全管理爬取漫画数据做网站

在项目管理中&#xff0c;图表作为一种直观的工具&#xff0c;帮助项目经理更有效的规划、监控和控制项目的各个方面&#xff0c;以下是项目经理常用的几张图表&#xff0c;它们在项目管理中发挥着至关重要的作用。 1、甘特图 甘特图&#xff08;Gantt Chart&#xff09;是最…

const不可改变解释

不能对const定义的变量本身重新赋值,但是可以通过其他方式更换变量里面的属性或元素(仅限对象类型和数组类型)。 “不能对const定义的变量本身重新赋值”这指的是 const 创建了一个只读的绑定(read-only binding)…

US$137.75 OTOFIX D1 One Year Update Service (Subsription Only)

OTOFIX D1 One Year Update Service (Subsription Only)1. Please send us the device serial number with picture to our Email 2. No Need Shipping. No refund service3. This is Only for Software Update, Witho…

在哪个平台做网站比较好义务网网站建设方案

工作中的焦虑 帮助团队建立复原力、处理不确定性和完成任务的8项策略 作者&#xff1a;阿德里安-戈斯蒂克、切斯特-埃尔顿和安东尼-戈斯蒂克 Anxiety at Work 8 Strategies to Help Teams Build Resilience, Handle Uncertainty, and Get Stuff Done By Adrian Gostick and…

地方门户网站的特点微信开发者模式在哪

从这个类中得到的类图&#xff0c;构划出了软件的大部分设计。 系统结构视图提供软件和整个系统结构最复杂的也是最优雅的描述。和通常的软件系统相比&#xff0c;在分布式嵌入系统中了解系统组件如何协同工作是非常重要的。毕竟&#xff0c;每个类图仅仅是一个系统的静态设计…

macOS Sequoia 15.7.1安全更新:修复字体解析器内存损坏漏洞

苹果发布了macOS Sequoia 15.7.1安全更新,修复了FontParser组件中的越界写入漏洞。该漏洞可能被恶意字体文件利用,导致应用程序意外终止或进程内存损坏。更新可通过Mac App Store或苹果官网下载获取。APPLE-SA-09-29…

AtCoder Beginner Contest 426 ABCDEF 题目解析

A - OS Versions 题意 有三种操作系统的版本,按发布时间顺序分别为 Ocelot、Serval、Lynx。 给定字符串 \(X, Y\),请判断版本 \(X\) 相比于版本 \(Y\) 的发布时间是否相同或更靠后(版本相同或更新)。 思路 直接判断…

前端学习教程-ElementPlus 教程

Element Plus 是基于 Vue 3 的企业级 UI 组件库,提供了丰富的预置组件,可帮助开发者快速构建高质量的前端界面。 一、安装 Element Plus 1. 环境要求Vue 3.0+ Node.js 14.0+2. 安装方式 (1)使用 npm 或 yarn 安装(…

镇江百度网站排名中交路桥建设有限公司官网

Linux磁盘管理&#xff08;二&#xff09;&#xff1a;LVM的创建、格式化和使用 一、LVM原理回顾 LVM的工作原理进行一个总结&#xff1a; (1)物理磁盘被格式化为PV&#xff0c;空间被划分为一个个的PE (2)不同的PV加入到同一个VG中&#xff0c;不同PV的PE全部进入到了VG的PE…

全网网站建设维护wordpress热门文章 图片

BLEU (Bilingual Evaluation Understudy&#xff0c;双语评估基准&#xff09;是一组度量机器翻译和自然语言生成模型性能的评估指标。BLEU指标是由IBM公司提出的一种模型评估方法,以便在机器翻译领域中开发更好的翻译模型。BLEU指标根据生成的句子与人工参考句子之间的词、短语…