news 2026/3/22 9:11:10

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/3/20 6:50:33

如何快速掌握Mousecape:macOS光标定制完整入门指南

如何快速掌握Mousecape&#xff1a;macOS光标定制完整入门指南 【免费下载链接】Mousecape Cursor Manager for OSX 项目地址: https://gitcode.com/gh_mirrors/mo/Mousecape 想要让你的Mac电脑拥有与众不同的光标体验吗&#xff1f;Mousecape作为macOS平台上专业的光标…

作者头像 李华
网站建设 2026/3/17 0:14:33

YOLO目标检测API支持OCR后处理,多模态结果输出

YOLO目标检测API支持OCR后处理&#xff0c;多模态结果输出 在智能制造车间的质检流水线上&#xff0c;一台摄像头扫过刚下线的工业设备——它不仅要识别出“铭牌区域”是否存在&#xff0c;还要准确读取上面印制的序列号、生产日期和电压参数。过去&#xff0c;这需要两个独立系…

作者头像 李华
网站建设 2026/3/19 0:59:14

YOLO模型镜像提供多种CUDA版本选择,兼容老旧GPU

YOLO模型镜像提供多种CUDA版本选择&#xff0c;兼容老旧GPU 在智能制造车间的边缘服务器上&#xff0c;一台搭载 Tesla K80 的工控机正试图加载最新的 YOLOv8 推理服务——结果却因 libcudart.so.12 缺失而崩溃。这并非个例&#xff1a;大量企业仍在使用基于 Kepler、Maxwell 或…

作者头像 李华
网站建设 2026/3/21 17:25:05

YOLOv9-Tiny极致压缩,可在Jetson Nano上运行

YOLOv9-Tiny极致压缩&#xff0c;可在Jetson Nano上运行 在智能制造车间的流水线上&#xff0c;一个微型摄像头正实时扫描着快速移动的零件——没有连接云端服务器&#xff0c;也没有依赖昂贵的GPU集群&#xff0c;它的“大脑”仅仅是一块售价不足百美元的NVIDIA Jetson Nano开…

作者头像 李华
网站建设 2026/3/20 1:55:56

学长亲荐10个AI论文平台,自考毕业论文格式规范必备!

学长亲荐10个AI论文平台&#xff0c;自考毕业论文格式规范必备&#xff01; AI工具助力论文写作&#xff0c;轻松应对自考挑战 在自考论文写作过程中&#xff0c;许多学生常常面临格式不规范、内容重复率高、逻辑不清晰等难题。而随着AI技术的不断发展&#xff0c;越来越多的智…

作者头像 李华
网站建设 2026/3/20 1:16:03

三家公司融资最高超3亿元;央视罕见近10分钟聚焦3D打印;2030年规模有望达1500亿元|库周报

2025年12月28日&#xff0c;以下是过去一周内3D打印行业发生的事情&#xff0c;包括“大事件” 、“行业动态” 、“趣制造” 、“融资/并购” 、“新产品”等内容。—— 大事件 ——央视罕见近10分钟专题聚焦3D打印2025年12月24日&#xff0c;CCTV-13新闻频道《科技推动力》用…

作者头像 李华