1. 简介
Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。
雏形:
interface21
Spring理念:
使现有的技术更加容易,本身是一个大杂烩,整合了现有的技术框架。
优点:
- Spring是一个开源的免费的框架(容器);
- Spring是一个轻量级的(本身很小)、非入侵式(不会对原来项目有影响)的框架;
- 控制反转(IoC)和面向切面(AOP)编程;
- 支持事务的处理,对框架整合的支持。
SSH:Struct2+Spring+Hibernate
SSM:SpringMvc+Spring+MyBatis
官方下载地址:
https://repo.spring.io/release/org/springframework/spring/
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.5</version>
</dependency>
组成:
Spring Boot构建一切(快速开发的脚手架,可以快速开发单个微服务,约定大于配置);Spring Cloud协调一切(基于Spring Boot实现);Spring Cloud Data Flow连接一切。
弊端:发展了太久,违背了原来的理念——配置地狱。
2. IoC
2.1 IoC理论推导
利用set进行动态实现值的注入:从程序主动创建对象到用户创建对象——控制反转
控制反转IoC(Inversion of Control)是一种思想,DI(依赖注入)只是实现IoC的一种方法。
Spring容器在初始化时先读取配置文件,根据配置文件或元数据创建与组织对象存入容器中,程序使用时再从ioc容器中取出需要的对象。(XML或注解)
控制:谁来控制对象的创建,传统应用程序的对象是由程序本身控制创建的,使用spring后,对象是由spring来创建的。
反转:程序本身不创建对象,而变成被动的接收对象。
依赖注入:就是利用set方法来进行注入的。
浏览底层源码:newClassPathXmlApplicationContext
2.2 IoC创建对象的方式
- 使用无参构造创建对象,默认。
<!--id:bean的唯一标识符;class:bean对象对应的全限定名,包名+类型;name:别名,可多个-->
<!--使用spring来创建对象,在spring中都称为bean-->
<!--value:获取具体的值,基本数据类型;ref:引用spring容器中创建好的对象-->
<bean id="hello" class="com.example.pojo.Hello" name="a aa;aaa,aaaa">
<property name="str" value="Spring"/>
</bean>
- 使用有参构造创建对象:
<!--下标赋值-->
<bean id="user" class="com.example.pojo.User">
<constructor-arg index="0" value="ZZ"/>
</bean>
<!--类型匹配,不建议使用——类型相同-->
<bean id="user" class="com.example.pojo.User">
<constructor-arg type="java.lang.String" value="ZZ"/>
</bean>
<!--直接通过参数名-->
<bean id="user" class="com.example.pojo.User">
<constructor-arg name="name" value="ZZ"/>
</bean>
2.3 Spring配置
别名:
通过别名获取对象:
<alias name="user" alias="userNew"/>
import:
一般用于团队开发使用,可以将多个配置文件导入合并为一个,内容相同会被合并,使用的时候直接使用总配置文件applicationContext.xml。
<import resource="beans.xml"/>
3. 依赖注入
依赖注入(DI,Dependency Injection),本质是set注入。依赖:bean对象的创建依赖于容器;注入:bean对象中的所有属性,由容器来注入。
3.1 构造器注入
参考2.2
3.2 set方式注入
<bean id="address" class="com.example.pojo.Address">
<property name="address" value="西安"/>
</bean>
<bean id="student" class="com.example.pojo.Student">
<!--普通值-->
<property name="name" value="张三"/>
<!--ref-->
<property name="address" ref="address"/>
<!--数组-->
<property name="books">
<array>
<value>红楼梦</value>
<value>西游记</value>
<value>水浒传</value>
</array>
</property>
<!--List-->
<property name="hobbys">
<list>
<value>听歌</value>
<value>电影</value>
</list>
</property>
<!--map-->
<property name="card">
<map>
<entry key="身份证" value="123455"/>
<entry key="银行卡" value="99999"/>
</map>
</property>
<!--set-->
<property name="games">
<set>
<value>LOL</value>
<value>COC</value>
</set>
</property>
<!--null-->
<property name="wife">
<null/>
</property>
<!--properties-->
<property name="info">
<props>
<prop key="学号">11234445</prop>
<prop key="性别">男</prop>
<prop key="password">22222</prop>
</props>
</property>
</bean>
3.3 其他注入方式
p-namespace:
p命名空间注入,可以直接注入属性的值:property
<!--依赖于第三方约束-->
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean name="classic" class="com.example.ExampleBean">
<property name="email" value="someone@somewhere.com"/>
</bean>
<bean name="p-namespace" class="com.example.ExampleBean"
p:email="someone@somewhere.com"