当前位置: 首页 > news >正文

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来创建,管理,装配!

http://www.zskr.cn/news/170238.html

相关文章:

  • 推荐阅读:Java多线程编程中的CAS与JUC组件深度解析
  • YOLOv10模型结构创新:无需后处理的真正端到端
  • 题解:P14913 「QFOI R3」难度评分
  • gorm - 查询为 或 为 null 的数据
  • YOLOv8-Transformer解码器实验版发布
  • 今年冬天,这些儿童羽绒服承包宝贝的温暖与时尚 - 品牌测评鉴赏家
  • YOLO目标检测API支持OCR后处理,多模态结果输出
  • 软件工程实践课程总结
  • YOLO目标检测API支持多区域部署,就近调用GPU资源
  • 专业照明如何通过核心参数提升商业空间氛围与体验
  • YOLO模型镜像支持Slurm作业调度,高校GPU集群适用
  • 英语_阅读_making us smarter or just helping us_待读
  • YOLOv10模型支持Tensor Cores,充分利用Ampere架构GPU
  • 工业视觉圈子里最近总有人问,怎么把Halcon的3D点云处理能力塞进C#开发的系统里。今天咱们就聊聊这个实战场景,直接上硬菜
  • YOLO目标检测在野生动物保护中的应用:红外相机识别
  • YOLOv8-NAS网络架构搜索技术应用,找到最优GPU结构
  • 自动驾驶核心技能:这本路径规划书,让算法从“调用”到“掌控”
  • YOLO目标检测API支持Webhook事件推送
  • AI如何重塑编程工作?我的氛围编程转型经验分享_程序员转行产品经理的心路历程之一
  • 漏洞猎手的CSP绕过指南:打破“安全”头部的幻象(第一部分)
  • 自考人必看!9个降AI率工具高效避坑指南
  • LLM Agent训练新范式!阿里 AgentEvolver三协同机制,攻克任务稀缺与探索低效难题,性能碾压传统 RL 方法
  • Java毕设项目推荐-基于SpringBoot的攻防靶场实验室平台的设计与实现环境部署 - 攻防实训 - 效果评估” 的全链路平台【附源码+文档,调试定制服务】
  • 基于SpringBoot + Vue的篮球俱乐部管理系统
  • 5个实施YashanDB的关键成功因素
  • 【计算机毕业设计案例】基于vue和springboot框架开发的攻防靶场实验室平台的设计与实现基于SpringBoot的攻防靶场实验室平台的设计与实现(程序+文档+讲解+定制)
  • 5个实现YashanDB价值的关键策略
  • YOLOv9-Ghost轻量主干网络解析:减少GPU计算量
  • AI智能体框架选型实战指南:需求匹配、技术趋势与分阶段验证(建议收藏)
  • YOLO模型训练验证集划分工具集成,GPU任务准备更快