框架导学

  1. 市场占有率极高,是互联网开发微服务架构的基础
  2. 是传统开发主流应用技术

Spring介绍

  1. Spring技术是JavaEE开发必备技能
  2. 简化开发,降低企业级开发的复杂性
  3. 框架整合,高效整合其他技术,提高企业级应用与运行效率

Spring并不是一个单一技术而是一个大家族,包含若干项目,每个项目用于完成特定的功能。

  • Spring Framework 底层框架,所有东西都依赖它执行
  • Spring Boot 在简化开发的同时提高速度
  • Spring Cloud 分布式

Spring Framework系统架构

Spring Framework

  • AOP:面向切面编程 教你程序怎么做,设计型
  • Aspects:AOP思想实现
  • Core Container:核心容器
  • Data Access:数据访问
  • Data Integration:数据集成
  • Web:Web开发
  • Test:单元测试与集成测试

核心概念

IoC控制反转

代码书写现状:耦合度高

解决方案:使用对象时,在程序中不要主动使用new产生对象,转换为由外部提供对象

IoC(Inversion of Control)控制反转:

  • 对象的创建控制器由程序转移到外部,这种思想称为控制反转-解耦

Spring技术对IoC思想进行了实现

  • Spring提供了一个容器,称为IoC容器,用来充当IoC思想中的外部
  • IoC容器负责对象的创建,初始化等一系列工作,被创建或被管理的对象在IoC容器中统称为Bean

DI(Dependency Injection)依赖注入

  • 在容器中建立bean与bean之间的依赖关系的整个过程,称为依赖注入

充分解耦:

  • 使用IoC容器管理bean(IoC)
  • 在IoC容器内将有依赖关系的bean进行关系绑定(DI)

最终效果:

  • 使用对象时不仅可以之间从IoC容器中获取,并且获取到bean已经绑定了所有的依赖关系

IOC底层原理

xml解析、工厂模式、反射

IOC

  1. IOC思想基于IOC容器完成,IOC容器底层就是对象工厂
  2. Spring提供IOC容器实现两种方法:
    1. BeanFactroy:IOC容器基本实现,是Spring内部的使用接口,不提供开发人员进行使用
    2. ApplicationContext:BeanFatory接口的子接口,提供更多更强大的功能,一般由开发人员进行使用

IOC操作Bean管理

什么是Bean管理

  1. Bean管理指的是两个操作
  2. Spring创建对象
  3. Spring注入属性

Bean管理操作有两种方式

  1. 基于xml配置文件方式实现
  2. 基于注解方式实现

IOC操作Bean管理(基于xml方式)

基于xml方式创建对象

1
2
<!--配置User对象创建-->
<bean id="user" class="com.spring5.User"></bean>
  • 在spring配置文件中,使用bean标签,标签里面添加对应属性,就可以实现对象创建
  • 在bean标签有很多属性,介绍常用的属性:
  • id属性:唯一标识
  • class属性:类全路径(包类路径)
  • 创建对象时候,默认也是执行无参数构造方法完成对象创建
  • name 别名配置;可以定义多个,使用逗号分号空格分隔

基于xml方式注入属性

DI:依赖注入,就是注入属性

第一种注入方式:使用set方法进行注入

创建类,定义属性和对应的set方法

1
2
3
4
5
6
7
8
9
10
11
12
public class Book{
//创建属性
private String bname;
private String bauthor;
//创建属性对应的set方法
public void setBname(String bname) {
this.bname = bname;
}
public void setBauthor(String bauthor) {
this.bauthor = bauthor;
}
}

在spring配置文件配置对象创建,配置属性注入

1
2
3
4
5
6
7
8
9
<!--set方法注入属性-->
<bean id="book" class="com.spring5.Book">
<!--使用property完成属性注入
name:类里面属性名称
value:向属性注入的值
-->
<property name="bname" value="数据结构"></property>
<property name="bauthor" value="作者"></property>
</bean>

第二种注入方式:使用有参构造进行注入

创建类,定义属性,创建属性对应有参数构造方法

1
2
3
4
5
6
7
8
9
10
public class Orders {
//属性
private String oname;
private String address;
//有参数构造
public Orders(String oname,String address) {
this.oname = oname;
this.address = address;
}
}

在 spring 配置文件中进行配置

1
2
3
4
5
<!--3 有参数构造注入属性-->
<bean id="orders" class="com.spring5.Orders">
<constructor-arg name="oname" value="电脑"></constructor-arg>
<constructor-arg name="address" value="China"></constructor-arg>
</bean>

p 名称空间注入(了解)

使用 p 名称空间注入,可以简化基于 xml 配置方式

添加 p 名称空间在配置文件中

1
2
3
<beans 
xmlns:p="http://www.springframework.org/schema/p"
></beans>

进行属性注入,在 bean 标签里面进行操作

1
<bean id="book" class="com.spring.Book" p:banme="数据结构" p:bauthor="作者"></bean>

IOC操作Bean管理(xml注入其他类型属性)

字面量

1
2
3
4
<!--null 值-->
<property name="address">
<null/>
</property>

属性值包含特殊符号

<![CDATA[]]>

1
2
3
4
5
6
7
<!--属性值包含特殊符号 
1 把<>进行转义 &lt; &gt;
2 把带特殊符号内容写到 CDATA
-->
<property name="address">
<value><![CDATA[<<南京>>]]></value>
</property

注入属性外部 bean

  1. 创建两个类service类和dao类
  2. 在service调用dao里面的方法
  3. 在spring配置文件中进行配置
1
2
3
4
5
6
7
8
9
10
public class UserService {
//创建 UserDao 类型属性,生成 set 方法
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void add() {
System.out.println("service add...............");
userDao.update();
} }
1
2
3
4
5
6
7
<!--1 service 和 dao 对象创建--> <bean id="userService" class="com.atguigu.spring5.service.UserService">
<!--注入 userDao 对象
name 属性:类里面属性名称
ref 属性:创建 userDao 对象 bean 标签 id 值
-->
<property name="userDao" ref="userDaoImpl"></property>
</bean> <bean id="userDaoImpl" class="com.atguigu.spring5.dao.UserDaoImpl"></bean>

注入属性内部bean

一对多关系:部门和员工

一个部门有多个员工,一个员工属于一个部门

部门是一,员工是多

在实体类之间表示一对多关系,员工表示所属部门,使用对象类型属性进行表示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//部门类
public class Dept {
private String dname;
public void setDname(String dname) {
this.dname = dname;
} }
//员工类
public class Emp {
private String ename;
private String gender;
//员工属于某一个部门,使用对象形式表示
private Dept dept;
public void setDept(Dept dept) {
this.dept = dept;
}
public void setEname(String ename) {
this.ename = ename;
}
public void setGender(String gender) {
this.gender = gender;
} }

在 spring 配置文件中进行配置

1
2
3
4
5
6
7
8
9
10
11
<!--内部 bean--> <bean id="emp" class="com.atguigu.spring5.bean.Emp">
<!--设置两个普通属性-->
<property name="ename" value="lucy"></property>
<property name="gender" value="女"></property>
<!--设置对象类型属性-->
<property name="dept">
<bean id="dept" class="com.atguigu.spring5.bean.Dept">
<property name="dname" value="安保部"></property>
</bean>
</property>
</bean>

注入属性-级联赋值

第一种写法

1
2
3
4
5
6
7
8
9
10
<!--级联赋值--> 
<bean id="emp" class="com.atguigu.spring5.bean.Emp">
<!--设置两个普通属性-->
<property name="ename" value="lucy"></property>
<property name="gender" value="女"></property>
<!--级联赋值-->
<property name="dept" ref="dept"></property>
</bean> <bean id="dept" class="com.atguigu.spring5.bean.Dept">
<property name="dname" value="财务部"></property>
</bean>

第二种写法

员工属于某一个部门,使用对象形式表示

1
2
3
4
private Dept dept;
public Dept getDept(){
return dept;
}
1
2
3
4
5
6
7
8
9
10
<bean id="emp" class="com.atguigu.spring5.bean.Emp">
<!--设置两个普通属性-->
<property name="ename" value="lucy"></property>
<property name="gender" value="女"></property>
<!--级联赋值-->
<property name="dept" ref="dept"></property>
<property name="dept.dname" value="技术部"></property>
</bean> <bean id="dept" class="com.atguigu.spring5.bean.Dept">
<property name="dname" value="财务部"></property>
</bean>

IOC操作Bean管理(xml注入集合属性)

  1. 注入数组类型属性
  2. 注入List 集合类型属性
  3. 注入Map集合类型属性

创建类,定义数组、list、map、set 类型属性,生成对应 set 方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class Stu {
//1 数组类型属性
private String[] courses;
//2 list 集合类型属性
private List<String> list;
//3 map 集合类型属性
private Map<String,String> maps;
//4 set 集合类型属性
private Set<String> sets;
public void setSets(Set<String> sets) {
this.sets = sets;
}
public void setCourses(String[] courses) {
this.courses = courses;
}
public void setList(List<String> list) {
this.list = list;
}
public void setMaps(Map<String, String> maps) {
this.maps = maps;
} }

在 spring 配置文件进行配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<!--1 集合类型属性注入--> <bean id="stu" class="com.atguigu.spring5.collectiontype.Stu">
<!--数组类型属性注入-->
<property name="courses">
<array>
<value>java 课程</value>
<value>数据库课程</value>
</array>
</property>
<!--list 类型属性注入-->
<property name="list">
<list>
<value>张三</value>
<value>小三</value>
</list>
</property>
<!--map 类型属性注入-->
<property name="maps">
<map>
<entry key="JAVA" value="java"></entry>
<entry key="PHP" value="php"></entry>
</map>
</property>
<!--set 类型属性注入-->
<property name="sets">
<set>
<value>MySQL</value>
<value>Redis</value>
</set>
</property>
</bean>

4.在集合里面设置对象类型值

1
2
3
4
5
6
7
8
9
10
11
<!--创建多个 course 对象--> <bean id="course1" class="com.atguigu.spring5.collectiontype.Course">
<property name="cname" value="Spring5 框架"></property>
</bean> <bean id="course2" class="com.atguigu.spring5.collectiontype.Course">
<property name="cname" value="MyBatis 框架"></property>
</bean>
<!--注入 list 集合类型,值是对象--> <property name="courseList">
<list>
<ref bean="course1"></ref>
<ref bean="course2"></ref>
</list>
</property>

5.把集合注入部分提取出来

在 spring 配置文件中引入名称空间 util

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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">

使用 util 标签完成 list 集合注入提取

1
2
3
4
5
6
7
8
<!--1 提取 list 集合类型属性注入--> <util:list id="bookList">
<value>易筋经</value>
<value>九阴真经</value>
<value>九阳神功</value>
</util:list>
<!--2 提取 list 集合类型属性注入使用--> <bean id="book" class="com.atguigu.spring5.collectiontype.Book">
<property name="list" ref="bookList"></property>
</bean>

IOC 操作 Bean 管理(FactoryBean)

Spring*有两种类型 bean,一种普通 bean,另外一种工厂 bean(FactoryBean)

普通bean:在配置文件中定义 bean 类型就是返回类型

工厂bean:在配置文件定义 bean 类型可以和返回类型不一样

第一步 创建类,让这个类作为工厂 bean,实现接口 FactoryBean

第二步 实现接口里面的方法,在实现的方法中定义返回的 bean 类型

Bean作用范围配置

spring创建的bean默认是一个单例实例。类似于new的对象都是一个对象,内存地址都一样

用scope可以配置 是否单例

  • singleton:单例(默认)
  • prototype:非单例

Bean实例化(3种)

构造方法

无参构造方法如果不存在,将会抛出异常BeanCreationException

静态工厂

实例工程

Bean的生命周期

  • 生命周期:从创建到消亡的完整过程
  • bean生命周期:bean从创建到销毁的整体过程
  • bean声明周期控制:在bean创建后到销毁前做的一些事情

初始化容器

  1. 创建对象(内存分配)
  2. 执行构造方法
  3. 执行属性注入
  4. 执行bean初始化方法

使用bean

  1. 执行业务操作

关闭、销毁容器

  1. 执行bean销毁方法

容器关闭前触发bean的销毁

关闭容器方式:

手动关闭容器

注册关闭钩子,在虚拟机退出前先关闭容器再推出虚拟机

依赖自动装配