一、Spring概述
1.1 web项目开发中的耦合度问题
1.2 面向接口编程
面向接口编程 |
 |
解决方案
:在Servlet中定义Service接口的对象变量,不使用new关键字创建实现类对象,在servlet的实例化的时候,通过反射动态的给Service对象变量赋值。
如何实现
:Spring可以做到!!!
1.3 Spring介绍
Spring是一个轻量级的控制反转和面向切面的容器
框架,用来解决企业项目开发的复杂度问题—解耦
- 轻量级:体积小,对代码没有侵入性
- 控制反转:IoC(Inverse of Control),把创建对象的工作交由Spring完成,Spring在创建对象的时候同时可以完成对象属性赋值(DI)
- 面向切面:AOP(Aspect Oriented Programming)面向切面编程,可以在不改变原有业务逻辑的情况下实现对业务的增强
- 容器:实例的容器,管理创建的对象
1.4 Spring架构
1.4.1 Core Container
Spring容器组件,用于完成实例的创建和管理
- core
- beans 实例管理
- context 容器上下文
1.4.2 AOP、Aspects
Spring AOP组件,实现面向切面编程
1.4.3 web
Spring web组件实际指的是SpringMVC框架,实现web项目的MVC控制
- web (Spring对web项目的支持)
- webmvc (SpringMVC组件)
1.4.4 Data Access
Spring数据访问组件,也是一个基于JDBC封装的持久层框架(即使没有mybatis,Spring也可以完成持久化操作)
1.4.5 Test
Spring的单元测试组件,提供了Spring环境下的单元测试支持
二、Spring IoC — 基于XML
Spring IoC 容器组件,可以完成对象的创建、对象属性赋值、对象管理
2.1 Spring框架部署(IoC)
2.1.1 创建Maven工程
2.1.2 添加SpringIoC依赖
- core
- beans
- aop
- expression
context
1 2 3 4 5
| <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.13.RELEASE</version> </dependency>
|
2.1.3 创建Spring配置文件
通过配置文件”告诉”Spring容器创建什么对象,给对象属性赋什么值
- 在resources目录下创建名为
appicationContext.xml
的文件(文件名是可以自定义的)
1 2 3 4 5 6 7 8 9
| <?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 http://www.springframework.org/schema/beans/spring-beans.xsd"> </beans>
|
2.2 SpringIoC使用
使用 SpringIoC组件创建并管理对象
2.2.1 创建一个实体类
1 2 3 4 5 6 7 8 9
| public class Student {
private String stuNum; private String stuName; private String stuGender; private int stuAge; private Date enterenceTime;
}
|
2.2.2 在Spring配置文件中配置实体类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <?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 http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="stu" class="com.qfedu.ioc.bean.Student"> <property name="stuNum" value="10002"/> <property name="stuName" value="李斯"/> <property name="stuGender" value="女"/> <property name="stuAge" value="20"/> </bean>
</beans>
|
2.2.3 初始化Spring对象工厂,获取对象
- ClassPathXMLApplicationContext
1 2 3 4
| ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Student student2 = (Student) context.getBean("stu");
|
2.3 IoC和DI
- IoC (Inverse of Control) 控制反转,通过Spring对象工厂完成对象的创建
- DI (Dependency Injection)依赖注入,在Spring完成对象创建的同时依赖Spring容器完成对象属性的赋值
2.3.1 IoC
当我们需要通过Spring对象工厂创建某个类的对象时候,需要将这个交给Spring管理——通过bean标签配置
1 2 3 4
| <bean id="stu" class="com.qfedu.ioc.bean.Student"></bean>
<bean id="book" class="com.qfedu.ioc.bean.Book"></bean>
|
2.3.2 DI
通过Spring容器给创建的对象属性赋值
1 2 3 4 5 6
| <bean id="clazz" class="com.qfedu.ioc.bean.Clazz"></bean>
<bean id="stu" class="com.qfedu.ioc.bean.Student" autowire="byName"> <property name="stuNum" value="10001"/> </bean>
|
2.4 DI依赖注入
2.4.1 依赖注入三种方式
Spring容器加载配置文件之后,通过反射
创建类的对象,并给属性赋值;
Spring容器通过反射实现属性注入有三种方式:
2.4.2 set方法注入
在bean标签中通过配置property标签给属性属性赋值,实际上就是通过反射调用set方法完成属性的注入
简单类型及字符串
1 2 3 4 5 6 7 8
| <bean id="stu" class="com.qfedu.ioc.bean.Student" autowire="byName"> <property name="stuNum" value="10001"/> <property name="stuAge" value="12"/> <property name="weight" value="62.3"/> </bean>
|
日期类型
- 方式1:在property标签中通过ref引用Spring容器中的一个对象
1 2 3 4 5 6
| <bean id="date" class="java.util.Date"></bean>
<bean id="stu" class="com.qfedu.ioc.bean.Student" > <property name="enterenceTime" ref="date"/> </bean>
|
- 方式2:在property标签中添加子标签bean来指定对象
1 2 3 4 5 6
| <bean id="stu" class="com.qfedu.ioc.bean.Student" > <property name="enterenceTime"> <bean class="java.util.Date"/> </property> </bean>
|
自定义类对象属性
1 2 3 4 5 6 7 8 9
| <bean id="cla" class="com.qfedu.ioc.bean.Clazz"> <property name="classId" value="2010"/> <property name="className" value="Java2010"/> </bean>
<bean id="stu" class="com.qfedu.ioc.bean.Student"> <property name="clazz" ref="cla"/> </bean>
|
1 2 3 4 5 6 7 8 9
| <bean id="stu" class="com.qfedu.ioc.bean.Student"> <property name="clazz"> <bean class="com.qfedu.ioc.bean.Clazz"> <property name="classId" value="2010"/> <property name="className" value="Java2010"/> </bean> </property> </bean>
|
集合类型
List
- List List中的元素是字符串或者简单类型的封装类
1
| <property name="hobbies" value="旅游,电影"/>
|
1 2 3 4 5 6 7
| <property name="hobbies" > <list> <value>旅游</value> <value>电影</value> <value>Java</value> </list> </property>
|
- List