news 2026/6/13 15:27:41

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/6/10 19:41:15

6G显存跑2K生图:腾讯混元Image-2.1轻量化部署实战指南

6G显存跑2K生图&#xff1a;腾讯混元Image-2.1轻量化部署实战指南 【免费下载链接】hunyuanimage-gguf 项目地址: https://ai.gitcode.com/hf_mirrors/calcuis/hunyuanimage-gguf 还在为AI绘画的高门槛而烦恼吗&#xff1f;现在&#xff0c;只需6G显存的普通显卡&#…

作者头像 李华
网站建设 2026/6/5 2:39:22

多模态大模型评估新突破:M3STR基准带你探索抽象视觉知识理解奥秘

本文提出M3STR新基准评估多模态大模型对抽象结构化知识的视觉理解能力。设计计数、检测和补全三种任务&#xff0c;评估26个主流MLLMs。发现当前模型在抽象视觉理解上存在显著缺陷&#xff0c;小模型表现接近随机猜测&#xff0c;开源模型整体优于闭源API。研究表明模型缩放定律…

作者头像 李华
网站建设 2026/6/10 16:52:12

如何突破115云盘下载限制?Aria2加速导出终极方案

你是否曾经为115云盘中大量文件的下载效率而苦恼&#xff1f;面对几百GB的照片、视频或工作文档&#xff0c;传统的浏览器下载方式不仅速度慢&#xff0c;还经常因为网络中断而前功尽弃。现在&#xff0c;一个名为115Exporter的Chrome扩展工具彻底改变了这一局面。 【免费下载链…

作者头像 李华
网站建设 2026/6/9 15:27:29

基于SpringBoot + Vue的足球训练营系统的设计与实现

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

作者头像 李华
网站建设 2026/6/10 11:09:00

YOLO模型训练超参搜索工具上线,自动化GPU实验

YOLO模型训练超参搜索工具上线&#xff0c;自动化GPU实验 在智能制造、自动驾驶和工业质检等场景中&#xff0c;实时目标检测早已不再是“有没有”的问题&#xff0c;而是“快不快、准不准、稳不稳”的工程较量。面对每秒上百帧的视频流输入&#xff0c;传统两阶段检测器如Fast…

作者头像 李华