Spring框架的详细学习

本文深入讲解Spring框架,包括IOC容器、AOP、JDBCTemplate和事务管理等内容。介绍了Bean的创建、注入属性、作用域、生命周期,以及基于XML和注解的管理方式。此外,还探讨了Spring对JDBC的封装和MyBatis的整合,详细阐述了事务管理的声明式和编程式方法。
摘要由CSDN通过智能技术生成

Spring

1.本章学习内容

1.Spring框架概要

2.IOC容器

3.AOP

4.jdbcTemplate

5.事务管理

6.Spring5新特性

7.Spring整合MyBatis

2.Spring概要

概述:它是轻量级的开源的JavaEE框架

  • 轻量级:他的体积小,依赖的jar包比较少,并且不需要额外依赖其他的组件

  • 开源:免费提供源代码

  • 框架:可以简化我们构建软件的过程

目的:为了解决企业级应用的复杂性

核心:

  • IOC:控制反转- 把创建对象的过程交给Spring进行管理
  • AOP:面向切面编程 - 不修改源码进行功能增强

优点:

  • 方便解耦,简化开发

  • 对AOP编程的支持

  • 方便程序的测试

  • 方便整合其他各种的框架

  • 方便进行事务操作

  • 降低API开发

3.入门案例

4.IOC容器

概述:

  • 控制反转

  • 综上所述:控制反转就是 把创建对象,和对象的调用的过程交给Spring来管理 。目的是为了降低类与类之间的耦合性。

底层原理:

  • XML解析

  • 工厂模式

  • 反射

原理图解:

在这里插入图片描述

重要概念:

  1. IOC 容器:IOC的实现,依赖于IOC容器,而IOC容器的本质就是对象工厂

  2. IOC容器的实现:

    • BeanFactory:是最底层的接口,他只提供了最简单的容器功能:加载配置文件 和 创建对象

      • 当加载配置文件时,不会创建被配置的对象,只有在使用时,对象才会创建。
        • 好处:节省内存
        • 坏处:因为在服务器运行的时候去创建对象,会影响执行效率
    • ApplicationContext:应用上下文,它是继承了BeanFactory。它是Spring更高级的一个容器接口,他提供了更多有用的功能

      • 当配置文件的时候,会同时创建被创建配置的对象
        • 好处:效率高,将复杂的创建过程在服务器启动时完成
        • 坏处:耗费资源

3.ApplicationContext的两个实现类

  • ClassPathXmlApplicationContext:从项目中的resources文件中加载配置文件

  • FileSystemXmlApplicationContext:从文件系统读取配置文件(需要访问权限)

  • AnnotionConfigApplicationContext:读取注解配置

5.IOC操作 - Bean管理

概念:IOC操作 - Bean管理是指两个操作: 1.Spring创建对象 2.Spring注入属性(属性赋值)

实现方式:

​ 1.XML方式

​ 2.注解方式

5.1基于XML方式-创建对象

该方式与入门案列方式相同

bean标签中常用属性

  • id:唯一标识,通过该属性可以找到对应的Bean标签
  • class:类的全限定类名

注意事项

​ 创建对象时,默认执行无参构造方法来完成对象的创建(反射)

5.2基于XML方式-注入属性

DI:依赖注入,它是IOC的一个具体操作

分类

  1. 使用Set方法进行注入
  2. 使用构造器方式经行注入

演示:

1.实体类

public class Book {
   
    private String name;
    private String author;

    public Book(String name, String author) {
   
        this.name = name;
        this.author = author;
    }

    public Book() {
   
    }

    public String getName() {
   
        return name;
    }

    public void setName(String name) {
   
        this.name = name;
    }

    public String getAuthor() {
   
        return author;
    }

    public void setAuthor(String author) {
   
        this.author = author;
    }

    @Override
    public String toString() {
   
        return "Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                '}';
    }
}

2.配置XML文件

Set方法

​ 属性注入通过<property>

name:实体类属性名

value:属性值

<?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
        http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!-- 创建User对象-->
    <bean id="book" class="com.wdzl.pojo.Book">
        <property name="name" value="Java从入门到入土"></property>
        <property name="author" value="詹姆士"></property>
    </bean>
</beans>

构造器方法

​ 属性注入通过<constructor-arg>

name:实体类属性名 index:通过索引

value:属性值

<?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
        http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!-- 创建User对象-->
    <bean id="book" class="com.wdzl.pojo.Book">
<!--        <constructor-arg name="name" value="Java高级数"></constructor-arg>-->
<!--        <constructor-arg name="author" value="赵子龙"></constructor-arg>-->
        <constructor-arg index="0" value="Java高级数"></constructor-arg>
        <constructor-arg index="1" value="赵子龙"></constructor-arg>
    </bean>
</beans>

P命名空间注入

xmlns:p="http://www.springframework.org/schema/p" 引入约束

<bean id="book" class="com.wdzl.pojo.Book" p:name="Java入门宝典" p:author="詹姆士·赵大帅哥"></bean>

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!-- P命名空间注入-->
    <bean id="book" class="com.wdzl.pojo.Book" p:name="Java入门宝典" p:author="詹姆士·赵大帅哥"></bean>


</beans>

3.测试

public class BookTest {
   
    @Test
    public void Test(){
   
        //1.加载配置文件
        ApplicationContext context = new ClassPathXmlApplicationContext("Beans2.xml");
        Book book = context.getBean("book", Book.class);
        System.out.println(book);
    }
}

特殊符号注入

​ 1.null

<constructor-arg name="name" >
           <null></null>
       </constructor-arg>

​ 2.转义字符

<constructor-arg name="author" >
 <value>"&lt;关羽&gt;"</value>
    </constructor-arg>

​ 3.CDATA

<constructor-arg name="author" >
           <value><![CDATA[<詹姆斯·屈波>]]></value>
       </constructor-arg>

外部Bean

1.新建Moule,在pom.xml中添加依赖

2.按照三层架构成绩:Dao,Service,Web,在dao层中添加方法,在Service层中添加对Dao层的依赖(dao成员变量,对应的set方法)

3.配置对象信息

在这里插入图片描述

内部类

1.再上一个演示案例的基础上,创建两个实体类Emp和Dept,其中Emp包含Dept对象属性

2.配置对象信息

在这里插入图片描述

级联操作

1.方式一

在这里插入图片描述

2.方式二

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3vApStu1-1617199855683)(D:\图片\20210320162814.png)]

注意事项:

​ 针对方式2:一定要提供get. set方法,否则配置文件中的某些属性就会报红。

(基本数据类型)数组,集合属性的注入

1.创建一个实体类

在这里插入图片描述

2.配置对象属性

<?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
        http://www.springframework.org/schema/beans/spring-beans.xsd">
   <bean id="demo" class="com.wdzl.pojo.Demo">
       <!-- 字符串数组-->
       <property name="strings">
           <array>
               <value>你好</value>
               <value>小老弟</value>
           </array>
       </property>
       <!-- list集合-->
       <property name="list">
           <list>
               <value>hello</value>
               <value>people</value>
           </list>
       </property>
       <!-- map集合-->
       <property name="map">
           <map>
               <entry key="你好" value="世界"></entry>
               <entry key="哈哈" value="屈波"></entry>
               <entry key="" value="唐康"></entry>
           </map>
       </property>
   </bean>

</beans>
  • 引用数据类型

城市类:

在这里插入图片描述

省类:

在这里插入图片描述

2.配置对象信息

<?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: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
                           https://www.springframework.org/schema/util/spring-util.xsd">
    <util:list id="cityList">
        <value>西安</value>
        <value>宝鸡</value>
        <value>延安</value>
    </util:list>
    <bean id="province" class="com.wdzl.pojo.Province">
        <property name="cities" ref="cityList"></property>
    </bean>


<!--    <bean id="city" class="com.wdzl.pojo.City">-->
<!--        <property name="cityName" value="西安"></property>-->
<!--    </bean>-->

<!--    <bean id="city2" class="com.wdzl.pojo.City">-->
<!--        <property name="cityName" value="宝鸡"></property>-->
<!--    </bean>-->
<!--    <bean id="city3" class="com.wdzl.pojo.City">-->
<!--        <property name="cityName" value="安康"></property>-->
<!--    </bean>-->
<!--    <bean id="city4" class="com.wdzl.pojo.City">-->
<!--        <property name="cityName" value="延安"></property>-->
<!--    </bean>-->

<!--   <bean id="province" class="com.wdzl.pojo.Province">-->
<!--       <property name="cities">-->
<!--           <list>-->
<!--               <ref bean="city"></ref>-->
<!--               <ref bean="city2"></ref>-->
<!--               <ref bean="city3"></ref>-->
<!--               <ref bean="city4"></ref>-->
<!--           </list>-->
<!--       </property>-->
<!--   </bean>-->

</beans>

FactoryBean:

概述:Spring中有两种类型的Bean,一种是普通Bean,一种是工厂Bean

  • 普通Bean:在配置文件中定义的Bean类型就是返回类型。
  • 工厂Bean:在配置文件中配置bean类型与返回类型不同。

演示:

创建一个工厂类

/**
 * 工厂Bean类型
 */
public class MyBean implements FactoryBean<String> {
   
    /**
     * 获取对象:此方法中定义了注入MyBean时,真正返回的对象
     * @return
     * @throws Exception
     */
    @Override
    public String getObject() throws Exception {
   
        return "我是钢铁侠";
    }

    /**
     * 返回对象类型
     * @return
     */
    @Override
    public Class<?> getObjectType() {
   
        return null;
    }

    /**
     * 是否是单例
     * @return
     */
    @Override
    public boolean isSingleton() {
   
        return false;
    }


}

配置对象信息

<?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
        http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="myBean" class="com.wdzl.pojo.MyBean"></bean>
</beans>

测试案例

在这里插入图片描述

5.3 Bean的作用域

**概述:**在Spring中,设置创建Bean实例是单例还是多例。默认情况,Bean是单例。

注意

Singletonprototype 的区别:

  1. Singleton :在加载配置文件时,对象便会创建,并且,只创建一个对象
  2. prototype :在加载配置文件时,并不会创建对象,在调用getBean方法时,才会创建对象,并且每次调用都会创建。

5.4 Bean的生命周期

概述:

​ 一个对象从创建到销毁的过程

过程:

  1. 通过构造器创建Bean实例
  2. 为Bean的属性设置值或引用其他Bean(调用set)
  3. 调用Bean初始化方法
  4. Bean对象获取
  5. 容器关闭,调用销毁Bean的方法

演示

实体类

public class User {
   
    private String name;

    public User(){
   
        System.out.println("第一步:通过构造器创建对象");
    }

    public String getName() {
   
        return name;
    }

    public void setName(String name) {
   
        System.out.println("第二步,为Bean属性设置值");
        this.name = name;
    }
    public void init(){
   
        System.out.println("第三步:调用Bean初始化方法");
    }

    public void destroy(){
   
        System.out.println("第五步:调用Bean销毁方法");
    }


}

配置文件

<?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
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="user" class="com.wdzl.pojo.User" init-method="init" destroy-method="destroy">
        <property name="name" value="周星驰"></property>
    </bean>
<!--    <bean id="myBeanLast" class="com.wdzl.pojo.MyBeanLast"></bean>-->

</beans>

结果

在这里插入图片描述

在Bean的生命周期中,如果配置了后置处理,生命周期会额外增加两步

  1. ​ 通过构造器创建Bean实例
  2. 为Bean的属性设置值或引用其他Bean(调用set)
  3. 执行后置处理
  4. 调用Bean初始化方法
  5. 执行后置处理
  6. Bean对象获取
  7. 容器关闭,调用销毁Bean的方法

演示

​ 在上述演示中再加入一个实体类

/**
 * 后置处理类
 */
public class MyBeanLast implements BeanPostProcessor {
   
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
   
        System.out.println("第三步:执行后置处理+postProcessBeforeInitialization");
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
   
        System.out.println("第五步:执行后置处理+postProcessAfterInitialization");
        return bean;
    }
}

配置文件

<?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
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="user" class="com.wdzl.pojo.User" init-method="init" destroy-method="destroy">
        <property name="name" value="周星驰"></property>
    </bean>
    <bean id="myBeanLast" class="com.wdzl.pojo.MyBeanLast"></bean>

</beans>

结果

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值