news 2026/4/18 10:04:20

Spring5HelloSpring

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Spring5HelloSpring

HelloSpring

第一个Spring程序

创建一个Maven项目

  1. 创建一个Hello JavaBean
publicclassHello{privateStringstr;publicStringgetStr(){returnstr;}publicvoidsetStr(Stringstr){this.str=str;}@OverridepublicStringtoString(){return"Hello{"+"str='"+str+'\''+'}';}}
  1. 基于 XML 的配置元数据

官方配置文件:

https://docs.spring.io/spring-framework/reference/core/beans/basics.html#beans-factory-xml https://docs.spring.io/spring-framework/docs/5.2.0.RELEASE/spring-framework-reference/core.html#beans-basics

一个感觉的XML配置文件是这样的

<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"></beans>

配置元数据

<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"><!--使用Spring来创建对象,在Spring这些都被称为Bean、Bean等于对象 类型 变量名 = new 类型(); Hello hello = new Hello(); id = 变量名 class = new 的对象 property 相当于给对象中的属性设置一个值! --><beanid="hello"class="com.cike.pojo.Hello"><!--str的赋值为value的内容--><propertyname="str"value="Spring"/></bean></beans>
  1. 在测试方法中实例化容器
publicclassMyTest{@Testpublicvoidtest(){// 获取Spring的上下文对象ApplicationContextcontext=newClassPathXmlApplicationContext("beans.xml");// 我们的对象现在都在Spring中管理了,要使用,直接去里面取出来就可以了Hellohello=(Hello)context.getBean("hello");System.out.println(hello.toString());}}

注意这个对象必须是这个,因为官方也是这样写的

  • 控制:谁来控制对象的创建,传统应用程序的对象是创建者自己,使用new关键字,Spring就是使用new关键字,但是现在对象创建权交给了Spring,程序员不再使用new关键字来创建对象了
  • 反转:程序本身不创建对象,而变为被动的接受对象
  • 依赖注入:就是利用set方法来进行注入的,才能利用Spring来给对象设置属性
  • Ioc是一种编程思想,由主动的编程变为被动的接收对象

有这个叶子说明被Spring使用了

底层分析

可以通过newClassPathXmlApplicationContext 去浏览底层源码

将传统开发的改写为Spring的容器管理

  1. UserDao接口
publicinterfaceUserDao{voidgetUser();}
  1. UserDaoImpl Dao层实现类
publicclassUserDaoImplimplementsUserDao{@OverridepublicvoidgetUser(){System.out.printf("获取用户Dao层");}}
  1. UserDaoMysqlImp业务实现类
publicclassUserDaoMysqlImplimplementsUserDao{@OverridepublicvoidgetUser(){System.out.println("Mysql获取用户数据");}}
  1. UserDaoSqlserver业务实现类
publicclassUserDaoSqlserverimplementsUserDao{@OverridepublicvoidgetUser(){System.out.println("SqlServer获取用户数据");}}
  1. 业务层接口
publicinterfaceUserService{voidgetUser();}
  1. 业务层JavaBean
publicclassUserServiceImplimplementsUserService{// 面向接口编程privateUserDaouserdao;// 利用set进行动态实现值的注入!publicvoidsetUserdao(UserDaouserdao){this.userdao=userdao;}// 业务层就做一个事情,调用dao层@OverridepublicvoidgetUser(){userdao.getUser();}
  1. 基于XML配置Spring容器的元数据
<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"><!-- 相当于 UserDaoSqlserver daoSqlserver = new com.cike1.dao.UserDaoSqlserver(); --><beanclass="com.cike1.dao.UserDaoSqlserver"id="daoSqlserver"/><beanclass="com.cike1.dao.UserDaoMysqlImpl"id="daoMysql"/><beanclass="com.cike1.service.UserServiceImpl"id="userServiceImpl"><!-- ref:引用Spring容器中创建好的对象 value:具体的值,基本数据类型 --><propertyname="userdao"ref="daoSqlserver"/></bean></beans>

  1. 测试方法中实例化容器&使用
publicvoidhello2(){// 获取ApplicationContext;拿到Spring的容器ApplicationContextcontext=newClassPathXmlApplicationContext("beans.xml");// 容器在手,天下我有,需要什么,就直接get什么UserServiceImpluserServiceImpl=(UserServiceImpl)context.getBean("userServiceImpl");userServiceImpl.getUser();}

OK,到了现在我们彻底不用在程序中去改动了,要实现不同的操作,只需要在XML配置文件中修改,所谓的IoC,一句话搞定:对象由Spring来创建,管理,装配!

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/18 8:30:12

YOLOv8-DyHead动态头机制解析:自适应空间特征校准

YOLOv8-DyHead动态头机制解析&#xff1a;自适应空间特征校准 在工业质检、智能交通和无人机巡检等现实场景中&#xff0c;目标的尺度变化剧烈、背景干扰复杂&#xff0c;且常伴随遮挡与密集排列。传统目标检测模型在这些条件下往往表现受限——小目标漏检、定位模糊、分类混淆…

作者头像 李华
网站建设 2026/4/16 13:29:51

(10-2)基于MCP实现的多智能体协同系统:通用模块

10.3 通用模块在本项目中&#xff0c;“utils ”目录是Agent-MCP项目的通用模块&#xff0c;提供了跨模块复用的辅助功能&#xff0c;为框架其他组件提供基础支撑。其核心作用包括封装 Git 操作&#xff08;如代码提交、分支管理&#xff09;以支持代理的版本控制需求&#xf…

作者头像 李华
网站建设 2026/4/18 8:48:49

基于SpringBoot + Vue的高校校园智能考勤系统

文章目录 前言一、详细操作演示视频二、具体实现截图三、技术栈1.前端-Vue.js2.后端-SpringBoot3.数据库-MySQL4.系统架构-B/S 四、系统测试1.系统测试概述2.系统功能测试3.系统测试结论 五、项目代码参考六、数据库代码参考七、项目论文示例结语 前言 &#x1f49b;博主介绍&a…

作者头像 李华
网站建设 2026/4/9 21:49:10

YOLOv9-SwinTransformer混合架构测试中,GPU显存压力大

YOLOv9-SwinTransformer混合架构测试中&#xff0c;GPU显存压力大 在当前智能视觉系统不断向高精度、强泛化能力演进的背景下&#xff0c;工业界对目标检测模型的要求早已不再局限于“快”或“准”的单一维度。以YOLO系列为代表的高效单阶段检测器&#xff0c;正越来越多地与Sw…

作者头像 李华
网站建设 2026/4/3 21:31:57

YOLO目标检测在停车场管理中的应用:车位占用识别

YOLO目标检测在停车场管理中的应用&#xff1a;车位占用识别 在城市楼宇林立、车流如织的今天&#xff0c;找一个空车位常常比开车本身更让人焦虑。传统停车场依赖人工巡查或地磁传感器判断车位状态&#xff0c;不仅成本高、维护难&#xff0c;还经常因遮挡、误判导致“有位难寻…

作者头像 李华
网站建设 2026/4/18 8:28:09

【课程设计/毕业设计】基于SpringBoot的攻防靶场实验室平台的设计与实现Springboot和Vue的攻防靶场实验室平台【附源码、数据库、万字文档】

博主介绍&#xff1a;✌️码农一枚 &#xff0c;专注于大学生项目实战开发、讲解和毕业&#x1f6a2;文撰写修改等。全栈领域优质创作者&#xff0c;博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java、小程序技术领域和毕业项目实战 ✌️技术范围&#xff1a;&am…

作者头像 李华