Spring入门案例-基于XML管理bean
1. 入门案例--hello spring
-
创建Maven Module
-
在pom.xml中引入依赖
<dependencies> <!-- 基于Maven依赖传递性,导入spring-context依赖即可导入当前所需所有jar包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.3.1</version> </dependency> <!-- junit测试 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies>
-
创建类HelloWorld
-
创建Spring的配置文件
-
在Spring的配置文件中配置bean
配置HelloWorld所对应的bean,即将HelloWorld的对象交给Spring的IOC容器管理。
通过bean标签配置IOC容器所管理的bean。
属性:- id:设置bean的唯一标识
- class:设置bean所对应类型的全类名
-
创建测试类测试
2. 总结
思路:
- 创建HelloWorld类
- 配置Spring配置文件applicationContext.xml,并配置bean
- IOC容器对象ClassPathXmlApplicationContext读取配置文件,创建helloWorld对象
部分内容来自 B站尚硅谷课程 。老师讲得很好,有兴趣的可以听听。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 快收藏!一个技巧从此不再搞混缓存穿透和缓存击穿
· Blazor Hybrid适配到HarmonyOS系统
· 支付宝 IoT 设备入门宝典(下)设备经营篇
· 万字调研——AI生成内容检测
· 解决跨域问题的这6种方案,真香!