文章目录
学框架前的说明
思想的学习不同于代码的学习,思想的学习要多去练习,练习的时候要带入问题去练习,不然很难成长。
前期准备
下载
-
官网:https://spring.io/projects/spring-framework#overview
-
官方下载地址:https://repo.spring.io/ui/native/release/org/springframework/spring
-
Github: https://github.com/spring-projects/spring-framework/releases
-
Maven: 导入 MVC
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.0.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.2.0.RELEASE</version> </dependency>
官方文档
Spring 七大模块
3. hellospring
3.1 导入依赖
新建 Maven 模块
此时由于父依赖已导入包 -> 此项目无需再导
3.2 编写相关代码
3.2.1 编写一个 Hello POJO 类
3.2.2 编写 spring 文件
便于理解先命名为 beans.xml(官方为 ApplicationContext.xml)
基本结构
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="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">
<bean id="..." class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>
<bean id="..." class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>
<!-- more bean definitions go here -->
</beans>
配置文件上下文 -> 此后才会有小叶子
补充:
ref:引用 Spring 容器中创建好的对象
value:具体的值,基本数据类型
3.2.3 测试
new CPX -> 拿到 Spring 的容器
工厂模式
POJO 类被 spring 托管后 左边会有叶子图标,点击可以跳转到 bean 配置容器中
若没有图标说明还未被托管,不可用
至此, 用户只需修改配置文件即可;