Spring Framework
前⾔
Spring 是当前 Java 开发的⾏业标准,第⼀框架。
Spring 概念诞⽣于 2002 年,于 2003 年正式发布第⼀个版本 Spring Framework 0.9。
经过⼗⼏年的优化迭代,Spring Framework 已经从最初的取代 EJB 的框架逐步发展为⼀套完整的⽣
态,最新的版本是 5.X。
Spring 架构体系图
Spring 两⼤核⼼机制
- IoC:⼯⼚模式
- AOP:代理模式
IoC
IoC 是 Spring 框架的灵魂,控制反转。
lombok 可以帮助开发者⾃动⽣成实体类相关⽅法,在 IDEA 中使⽤,必须预先安装插件。
开发步骤
1、创建 Maven ⼯程,pom.xml 导⼊依赖。
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>
</dependencies>
2、在 resources 路径下创建 spring.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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
">
<bean id="student" class="com.southwind.entity.Student"></bean>
</beans>
3、IoC 容器通过读取 spring.xml 配置⽂件,加载 bean 标签来创建对象
4、调⽤ API 获取 IoC 容器中已经创建的对象
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
Student student = (Student) applicationContext.getBean("student");
System.out.println(student);
IoC 容器创建 bean 的两种⽅式
1.⽆参构造函数
<bean id="student" class="com.southwind.entity.Student"></bean>
给成员变量赋值
<bean id="student" class="com.southwind.entity.Student">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
<property name="age" value="22"></property>
</bean>
2.有参构造函数
<bean id="student3" class="com.southwind.entity.Student">
<constructor-arg name="name" value="王五"></constructor-arg>
<constructor-arg name="id" value="3"></constructor-arg>
<constructor-arg name="age" value="18"></constructor-arg>
</bean>
从 IoC 容器中取 bean
通过 id 取值
Student student = (Student) applicationContext.getBean("student");
通过类型取值
Student student = (Student) applicationContext.getBean(Student.class);
当 IoC 容器中同时存在两个以上 Student Bean 的时候就会抛出异常,因为此时没有唯⼀的 bean
bean 的属性中如果包含特殊字符,如下处理即可
<bean id="classes" class="com.southwind.entity.Classes">
<property name="id" value="1"></property>
<property name="name">
<value><![CDATA[<⼀班>]]></value>
</property>
</bean>
IoC DI
DI 指 bean 之间的依赖注⼊,设置对象之间的级联关系。
Classes
@Data
public class Classes {
private Integer id;
private String name;
private List<Student> studentList;
}
Student
@Data
@NoArgsConstructor
public class Student {
private Integer id;
private String name;
private Integer age;
public Student(Integer id, String name, Integer age) {
System.out.println("通过有参构造创建对象");
this.id = id;
this.name = name;
this.age = age;
}
public Student(Integer id, String name) {
this.id = id;
this.name = name;
}
}
spring-di.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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
">
<!-- Classes -->
<bean id="classes" class="com.southwind.entity.Classes">
<property name="id" value="1"></property>
<property name="name" value="一班"></property>
<property name="studentList">
<list>
<ref bean="student"></ref>
<ref bean="student2"></ref>
</list>
</property>
</bean>
<!-- Student -->
<bean id="student" class="com.southwind.entity.Student">
<property name="id" value="100"></property>
<property name="name" value="张三"></property>
<property name="age" value="22"></property>
<property name="classes" ref="classes"></property>
</bean>
<bean id="student2" class="com.southwind.entity.Student">
<property name="id" value="200"></property>
<property name="name" value="李四"></property>
<property name="age" value="18"></property>
<property name="classes" ref="classes"></property>
</bean>
</beans>
Spring 中的 bean
bean 是根据 scope 来⽣成,表示 bean 的作⽤域,scope 有 4 种类型:
- singleton,单例,表示通过 Spring 容器获取的对象是唯⼀的,默认值。
- prototype,原型,表示通过 Spring 容器获取的对象是不同的。
- request,请求,表示在⼀次 HTTP 请求内有效。
- session,会话,表示在⼀个⽤户会话内有效。
requset,session 适⽤于 Web 项⽬。
singleton 模式下,只要加载 IoC 容器,⽆论是否从 IoC 中取出 bean,配置⽂件中的 bean 都会被创
建。
prototype 模式下,如果不从 IoC 中取 bean,则不创建对象,取⼀次 bean,就会创建⼀个对象。
Spring 的继承
Spring 继承不同于 Java 中的继承,区别:Java 中的继承是针对于类的,Spring 的继承是针对于对象
(bean)。
Spring 的继承中,⼦ bean 可以继承⽗ bean 中的所有成员变量的值
<bean id="user1" class="com.southwind.entity.User">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
</bean>
<bean id="user2" class="com.southwind.entity.User" parent="user1">
<property name="name" value="李四"></property>
</bean>
通过设置 bean 标签的 parent 属性建⽴继承关系,同时⼦ bean 可以覆盖⽗ bean 的属性值。
Spring 的继承是针对对象的,所以⼦ bean 和 ⽗ bean 并不需要属于同⼀个数据类型,只要其成员变量
列表⼀致即可。