Spring 5 1.1、 简介 Spring: 春-----给软件行业带来了春天! 2002, 首次推出了Spring框架的雏形: interface21框架! Spring框架即以interface21框架为基础,经过重新设计 ,并不断丰富其内涵,于2004年月24日
Spring 5
1.1、 简介
- Spring: 春----->给软件行业带来了春天!
- 2002, 首次推出了Spring框架的雏形: interface21框架!
- Spring框架即以interface21框架为基础,经过重新设计 ,并不断丰富其内涵,于2004年月24日发布了1.0正式版。
- Rod Johnson,, Spring Framework创始人, 著名作者。他是悉尼大学的博士,然而他的专业不是计算机,而是音乐学。
- SSH : Struct2 + Spring + Hibernate!
- SSM : SpringMvc + Spring + Mybatis!
官网
下载地址:https://repo.spring.io/ui/native/release/org/springframework/spring
GitHub:https://github.com/spring-projects/spring-framework
1.2、优点
- Spring是一个开源的免费的框架(容器) !
- Spring是一个轻量级的、 非入侵式的框架!
- 控制反转(IOC) ,面向切面编程(AOP) !
- 支持事务的处理,对框架整合的支持!
<u>总结一-句话: Spring就是一 一个轻量级的控制反转(IOC)和面向切面编程(AOP)的框架!</u>
1.3、组成

1.4、拓展

●Spring Boot
。一个快速开发的脚手架。
。基于SpringBoot可以快速的开发单个微服务。
。约定大于配置!
●Spring Cloud
。SpringCloud 是基于SpringBoot实现的。
因为现在大多数公司都在使用SpringBoot进行快速开发,学习SpringBoot的前提, 需要完全掌握Spring及
SpringMVC!承上启下的作用!|
弊端:发展了太久之后,违背了原来的理念!配置十分繁琐,人称:”配置地狱!
2、IOC理论推导
UserDao接口UserDaolmpl实现类UserService业务接口UserServicelmpl业务实现类在我们之前的业务中,用户的需求可能会影响我们原来的代码,我们需要根据用户的需求去修改原代码!如果程序
代码量十分大,修改一-次的成本代价十分昂贵!
我们使用一个Set接口实现.已经发生了革命性的变化!
//set注入 public void setUserDao(UserDao userDao) { this.userDao = userDao; }
beans.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" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="..." class="..."> <!-- collaborators and configuration for this bean go here --> </bean> <bean id="..." class="..."> <!-- collaborators and configuration for this bean go here --> </bean> <!-- more bean definitions go here --></beans>
●之前,程序是主动创建对象!控制权在程序猿手.上!
●使用了set注入后,程序不再具有主动性,而是变成了被动的接受对象!
这种思想,从本质上解决了问题,我们程序猿不用再去管理对象的创建了。系统的耦合性大大降低~,可以更加专
注的在业务的实现上!这是IOC的原型!
IoC本质
控制反转IoC(Inversion of Control),是一种设计思想,DI(依赖注入)是实现loC的一种方法,也有人认为DI只是IOC的另一种说法。 没有IoC的程序中,我们使用面向对象编程, 对象的创建与对象间的依赖关系完全硬编码在程序中,对象的创建由程序自己控制,控制反转后将对象的创建转移给第三方,个人认为所谓控制反转就是:获得依赖对象的方式反转了。
采用XML方式配置Bean的时候,Bean的定义信息是和实现分离的,而采用注解的方式可以把两者合为一-体,Bean的定义信息直接以注解的形式定义在实现类中,从而达到了零配置的目的。
控制反转是一种通过描述(XML或注解)并通过第三方去生产或获取特定对象的方式。在Spring中实现控制反转的是IoC容器,其实现方法是依赖注入(Dependency Injection,DI)。
3、HelloSpring
4、IOC创建对象的方式
使用无参构造创建对象,默认!https://docs.spring.io/spring-framework/docs/5.2.0.RELEASE/spring-framework-reference/core.html#beans-constructor-injection假设我们要使用有参构造创建对象。下标赋值<bean id="user" class="com.mi.pojo.User"> <constructor-arg index="0" value="mink说Java"/> </bean>官网<bean id="exampleBean" class="examples.ExampleBean"> <constructor-arg index="0" value="7500000"/> <constructor-arg index="1" value="42"/></bean>
类型<bean id="exampleBean" class="examples.ExampleBean"> <constructor-arg type="int" value="7500000"/> <constructor-arg type="java.lang.String" value="42"/></bean>
第三种 直接通过参数名<!-- 第三种 直接通过参数名--> <bean id="user" class="com.mi.pojo.User"> <constructor-arg name="name" value="哈哈哈哈哈哈哈"/> </bean>
总结:在配置文件加载的时候,容器中管理的对象就已经初始化了! 就是在一个容器里

5、spring配置
5.1、别名
<alias name="user" alias="userTest"/>
5.2、Bean的配置
<!--id : bean的唯一 标识符,也就是相当于我们学的对象名class : bean对象所对应的全限定名:包名+类型name :也是别名,而且name可以同时取多个别名--><bean id="UserT" class="com.mi.pojo.UserT" name="othername,un2 u3"> <property name="name" value="秦疆啊秦疆"/></bean>
5.3、import
这个import, - 般用于团队开发使用,他可以将多个配置文件,导入合并为一个
假设,现在项目中有多个人开发,这三个人复制不同的类开发,不同的类需要注册在不同的bean中,我们以利用import将所有人的beans.xml合并为一个总的!
- 张三
- 李四
- 王五
- applicationContext.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" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> <import resource="beans.xml"/> <import resource="beans2.xml"/></beans>
6、DI 依赖注入
6.1、构造器驻入
6.2、Set注入【重点】
- 依赖:bean对象的创建依赖于容器
- 注入bean对象种的所有属性,由容器来注入
【环境搭建】
复杂类型public class Address { private String address; public String getAddress() { return address; } public void setAddress(String address) { this.address = address; }}
真是测试对象public class Student { private String name; private Address address; private String[] books; private List<String> hobby; private Map<String,String> card; private Set<String> dames; private String wife; private Properties info;
beans.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" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> <bean name="student" class="com.mdi.pojo.Student"> <property name="name" value="mink"/> </bean></beans>
测试类public class DiTest { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); Student student = (Student) context.getBean("student"); System.out.println(student.getName()); }}
完善注入信息<?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 https://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 2.bean注入--> <bean id="address" class="com.mdi.pojo.Address"/> <bean id="student" class="com.mdi.pojo.Student"><!-- 1.普通值注入--> <property name="name" value="mink"/><!-- 2.--> <property name="address" ref="address"/><!--3.array--> <property name="books"> <array> <value>西游记</value> <value>红楼梦</value> <value>水浒传</value> <value>三国演义</value> </array> </property><!--4.list--> <property name="hobby"> <list> <value>吃饭</value> <value>睡觉</value> <value>打豆豆</value> </list> </property><!--5.map--> <property name="card"> <map> <entry key="身份证" value="4105261111111"/> <entry key="银行卡" value="888888888888888"/> <entry key="" value=""/> <entry key="" value=""/> </map> </property><!--6.set--> <property name="games"> <set> <value>LOL</value> <value>4399</value> <value>7k7k</value> </set> </property><!--7.null--> <property name="wife"> <null></null> </property><!--8.property--> <property name="info"> <props> <prop key="学号">20200225</prop> <prop key="姓名">mink</prop> <prop key="性别">boy</prop> </props> </property> </bean></beans>public class DiTest { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); Student student = (Student) context.getBean("student"); System.out.println(student.getName()); System.out.println(student.toString()); }}//输出//Student{name='mink', address=Address{address='null'}, books=[西游记, 红楼梦, 水浒传, 三国演义], hobby=[吃饭, 睡觉, 打豆豆], card={身份证=4105261111111, 银行卡=888888888888888, =}, dames=[LOL, 4399, 7k7k], wife='null', info={学号=20200225, 性别=boy, 姓名=mink}}
6.3、拓展方式注入

<?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.mdi.pojo.User" p:name="jack" p:age="18"/> <!--c命名空间注入,通过构造器注入: construct-args--> <bean id="user2" class="com.mdi.pojo.User" c:age="8" c:name="韩明"/></beans>
6.4、bean的作用域
单例模式(Spring默认机制)<bean id="user2" class="com.mdi.pojo.User" c:age="8" c:name="韩明" scope="singleton"/>从一个里面创建的User user = context.getBean("user2", User.class); User user2 = context.getBean("user2", User.class); System.out.println(user==user2);//true
原型模式(每次从容器中get的时候就会产生一个 新对象)
<bean id="user2" class="com.mdi.pojo.User" c:age="8" c:name="韩明" scope="prototype"/>User user = context.getBean("user2", User.class);User user2 = context.getBean("user2", User.class);System.out.println(user==user2);//false
3.其余的request、session、 application、 这些个只能在web开发中使用到!
7、bean的自动装配
- 自动装配是Spring满足bean依赖一种方式!
- Spring会在上下文中自动寻找,并自动给bean装配属性!
在Spring中有三种装配的方式
1.在xml中显示的配置
2.在java中显示配置
3.隐式的自动装配bean [ 重要]
7.1、测试
环境搭建 一个人由两个宠物
7.2、ByName自动装配
<!-- byName:会自动在容器 上下文中查找,和自己对象set方法后面的值对应的beanid!--><!-- byType:会自动在容器上:下文中查找,和自己对象属性类型!相同的bean! 不写id也行--> <bean id="people" class="com.auto.pojo.People" autowire="byName"> <property name="name" value="mink"/> </bean>
7.3、ByType自动装配
<bean id="people" class="com.auto.pojo.People" autowire="byType"> <property name="name" value="mink"/></bean>
\小结:
●byname的时候,需要保证所有bean的id唯一 , 并且这个bean需 要和自动注入的属性的set方法的值-致!
●bytype的时候,需要保证所有bean的class唯一 , 并且这个bean需要和自动注入的属性的类型一致!
7.4、使用注解自动装配
jdk1 .5支持的注解,Spring2.5就支持注解了 !
The introduction of annotation-based configuration raised the question of whether this approach is better"
than XML.
要使用注解须知:
导入约束 context约束配置注解的支持 注解的支持【重点】<?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 http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!-- 注解的支持--> <context:annotation-config/></beans>
@AutoWired
直接在属性上使用即可!也可以在set方式上使用!
使用Autowired我们可以不用编写Set方法了,前提是你这个自动装配的属性在I0C (Spring) 容器中存在,且符
合名字byname!
科普
@Nullable 字段标记了这个注解 说明这个字段可以为nullpublic @interface Autowired { boolean required() default true;}
测试
public class People { //如果显示定义了Autowi red的requi red属性为false,说明这个对象可以为nu11,否则不允许为空 private String name; @Autowired private Dog dog; @Autowired private Cat cat;
如果@Autowired自动装配的环境比较复杂,自动装配无法通过一一个注解[ @Autowired ]完成的时候、我们可以
使用@Qualifier(value="xxx")去配合@Autowired的使用,指定一个唯一 -的bean对象注入!
@Qualifier 注解
@Resource 注解
小结:
@Resource和@ Autowired的区别:
●都是用来自动装配的,都可以放在属性字段上
●@ Autowired通过bytype的方式实现,而且必须要求这个对象存在! [常用]
●@Resource默认通过byname的方式实现,如果找不到名字,则通过byType实现!如果两个都找不到的情况
下,就报错! [常用]
8、使用注解开发
在Spring4之后,要使用注解开发,必须要保证aop的包导入了

使用注解需要导入context约束,增加注解的支持!
<?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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 注解的支持--> <context:annotation-config/></beans>
bean属性如何注入@Componentpublic class User { //相当于<property name= "name" value= "kuangshen "/> @Value("value 注入") public String name="mink";
衍生的注解@Conponent有几个衍生注解,在web开发中 会按照三层mvc架构分- dao【@Repository】
- service【@service】
- controller【@controller】
- 这四个注解功能都是一样的,都代表被这个类装配到spring中 装配bean
自动装配置@Autowired :自动装配通过类型。名字
如果Autowi red不能唯一自 动装配上属性,则需要通过@Qualifier(value="xxx")- @Nu11able
字段标记了这个注解,说明这个字段可以为nu11;
- @Resource :自动装配通过名字。类型。
作用域@Scope小结xml与注解- xml更加万能,适用于任何场合 维护简单方便
- 注解 不是自己的类使用不了 维护相对复杂xml与注解最佳实践:
- xml用来管理bean
- 注解只负责完成属性的注入
- 我们在使用的过程中,只需要注意一个问题:必须要注解生效,开启注解支持
9、使用Java的方式配置Spring
p15
10、代理模式
中介;
这就是SpringAop的底层【SpringAop 和 SPringMVC】
代理模式的分类:

10.1、静态代理
角色分析:
●抽象角色:一般会使用接口或者抽象类来解决
●真实角色:被代理的角色
●代理角色:代理真实角色,代理真实角色后,我们一般会做- -些附属操作
●客户:访问代理对象的人!
代码步骤:
1.接口
2.真实角色
3.代理角色
4.客户端访问代理角色
代理模式的好处:
●可以使真实角色的操作更加纯粹!不用去关注一些公共的业务
●公共也就就交给代理角色!实现了业务的分工! .
●公共业务发生扩展的时候,方便集中管理!
缺点:
●一个真实角色就会产生一个代理角色;代码量会翻倍开发效率会变低
10.2、 加深理解

10.3、动态代理
●动态代理和静态代理角色- -样
●动态代理的代理类是动态生成的,不是我们直接写好的!
动态代理分为两大类:基于接口的动态代理,基于类的动态代理
。基于接口-- JDK动态代理[我们在这里使用]
。基于类: cglib
。java字节码实现: javasist
需要了解两个类: Proxy: 代理,InvocationHandler: 调用处理程序