1. Spring 简介
2002年,推出 interface21框架;Spring框架雏形基于interface21框架,于2004年发布 by Rod Johnson (音乐学博士)。
Spring下载路径:
- Spring 各个版本下路径:@ https://repo.spring.io/ui/native/release/org/springframework/spring
- Spring GitHub源代码下载路径: @ GitHub - spring-projects/spring-framework: Spring Framework
SSH&SSM:
- SSH: Structs + Sping + Hibernate
- SSM: Spring MVC + Spring + Mybatis
依赖包: Spring Web MVC, (必须是5.3.28或者更低版本,6.xx系列需要JDK17) Spring JDBC (必须是xx版本,6.xx系列需要JDK17)
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.28</version> </dependency>
Spring是一个开源免费的框架/容器;轻量级的、非入侵式的框架;控制反转(IoC) 面向切面(AOP)。其有七大组件构成,
2. IOC 控制反转
通过用于的set设置调用的最终接口,从而实现用户控制(控制反转到用户这边)
- 控制:谁来控制对象的创建,传统引用程序的对象是有程序本身控制创建的,使用Spring后,对象是由Spring来创建的。
- 反转:程序本身不创建对象,而变成被动的接收对象
- 依赖注入:就是利用set方法来进行注入的
IOC是一种编程思想,由主动的编程变成被动的接收。我们彻底不用在程序中去改动了,要实现不同的操作,只需要在xml配置文件中进行修改。
所谓的IOC,一句话就是:对象由Spring来创建、管理、装配。
IoC创建对象的方式
- 默认使用无参构造创建对象;如果通过有参构造,有三种方式(下标赋值、类型赋值、直接参数名赋值)
3. Spring 配置
别名、配置、导入
<!--name 也是别名,而且更加高级--> <bean id="usert" class="com.crevew.pojo.UserT" name="usertAlias别名 usertAlias别名2, usertAlias别名3"/> <alias name="user" alias="userAlias别名"/>
4. DI 依赖注入
4.1 构造器注入
等同于上述的 “IoC创建对象的方式”
4.2 Set方式注入(重点)
- 依赖:bean对象的创建依赖于容器
- 注入:bean对象中的所有属性,由容器来注入
<bean id="student" class="com.crevew.pojo.Student"> <!--<property name="name" value="zjfun"/>--> <!--1. 下标赋值--> <!--<constructor-arg index="0" value="zjfun1"/>--> <!--2. 类型赋值--> <!--<constructor-arg type="java.lang.String" value="zjfun2"/>--> <!--3. 直接通过参数名--> <!--第一种:普通值注入--> <property name="name" value="zjfun"/> <!--第二种:Bean注入--> <property name="address" ref="address"/> <!--第三种:数组注入--> <property name="books"> <array> <value>红楼梦</value> <value>西游记</value> <value>水浒传</value> <value>三国演义</value> </array> </property> <!--List注入--> <property name="hobbys"> <list> <value>听歌</value> <value>敲代码</value> <value>看电影</value> </list> </property> <!--Map注入--> <property name="card"> <map> <entry key="身份证" value="123123123123"/> <entry key="银行卡" value="111111111111"/> </map> </property> <!--Set注入--> <property name="games"> <set> <value>LOL</value> <value>COC</value> <value>BOB</value> </set> </property> <!--NULL注入--> <property name="wife"> <null/> </property> <!--Properties注入--> <property name="info"> <props> <prop key="学号">04970314</prop> <prop key="性别">男</prop> <prop key="姓名">曳明</prop> </props> </property> </bean>
4.3 扩展方式注入
可以使用p (property)命名空间 (等同于set注入)和c (contructor)命名空间 (等同于constructor-arg)实现注入。
<?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:c="http://www.springframework.org/schema/c" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> <!--p命名空间注入,可以直接注入属性的值:Property--> <bean id="user" class="com.crevew.pojo.User" p:name="ZZZ" p:age="18"/> <!--c命名空间注入,通过构造器注入: construct-args --> <bean id="user2" class="com.crevew.pojo.User" c:age="19" c:name="JJJ"/> </beans>
4.4 bean的作用域
Scope | Description |
---|---|
(Default) Scopes a single bean definition to a single object instance for each Spring IoC container. |
|
Scopes a single bean definition to any number of object instances. |
|
Scopes a single bean definition to the lifecycle of a single HTTP request. That is, each HTTP request has its own instance of a bean created off the back of a single bean definition. Only valid in the context of a web-aware Spring |
|
Scopes a single bean definition to the lifecycle of an HTTP |
|
Scopes a single bean definition to the lifecycle of a |
|
Scopes a single bean definition to the lifecycle of a |
单例模式: 默认
原型模式: 每次从容器中get的时候,产生一个新的对象
其余的request,session, application,这些只是在web开发中使用
<bean id="user2" class="com.crevew.pojo.User" c:age="18" c:name="ZZZ" scope="prototype"/>
5. bean的自动装配
自动装配是Spring满足bean依赖一种方式
Spring会在上下文中自动寻找,并自动给bean装配属性
在Spring中有三种装配的方式:
1. 在xml中显式的配置
2. 在java中显式配置
3. 隐式的自动装配bean【重要】,隐式有三种:
5.1 byName
需要保证所有bean的id唯一,并且这个bean需要和自动注入的属性的set方法一致
<!--byName: 会自动在容器上下文中查找,和自己对象set方法后面的值对应的beanid byType: 会自动在容器上下文中查找,和自己对象类型相同的bean--> <bean id="cat" class="com.crevew.pojo.Cat"/> <bean id="dog" class="com.crevew.pojo.Dog"/> <bean id="people" class="com.crevew.pojo.People" autowire="byName"> <property name="name" value="ZJ"/> </bean>
5.2 byType
需要保证所有bean的class唯一,并且这个bean需要和自动注入的属性类型一致
<bean class="com.crevew.pojo.Cat"/> <bean class="com.crevew.pojo.Dog"/> <bean id="people" class="com.crevew.pojo.People" autowire="byType"> <property name="name" value="ZJ"/> </bean>
5.3 注解 annotation
导入约束 , 且 配置注解的支持
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> <context:annotation-config/>
</beans>