• Spring学习笔记——4


    一、基于AOP的声明式事务控制

    1.1、Spring事务编程概述

    事务是开发中必不可少的东西,使用JDBC开发时,我们使用connnection对事务进行控制,使用MyBatis时,我们使用SqlSession对事务进行控制,缺点显而易见,当我们切换数据库访问技术时,事务控制的方式总会变化,Spring 就将这些技术基础上,提供了统一的控制事务的接口。Spring的事务分为:编程式事务控制和声明式事务控制

    事务控制方式解释
    编程式事务控制Spring提供了事务控制的类和方法,使用编码的方式对业务代码进行事务控制,事务控制代码和业务操作代码耦合到了一起,开发中不使用
    声明式事务控制Spring将事务控制的代码封装,对外提供了Xml和注解配置方式,通过配置的方式完成事务的控制,可以达到事务控制与业务操作代码解耦合,开发中推荐使用

    Spring事务编程相关的类主要有如下三个

    事务控制相关类解释
    平台事务管理器 PlatformTransactionManager是一个接口标准,实现类都具备事务提交、回滚和获得事务对象的功能,不同持久层框架可能会有不同实现方案
    事务定义 TransactionDefinition封装事务的隔离级别、传播行为、过期时间等属性信息
    事务状态 TransactionStatus存储当前事务的状态信息,如果事务是否提交、是否回滚、是否有回滚点等

    虽然编程式事务控制我们不学习,但是编程式事务控制对应的这些类我们需要了解一下,因为我们在通过配置的方式进行声明式事务控制时也会看到这些类的影子

    1.2、搭建测试环境

    搭建一个转账的环境,dao层一个转出钱的方法,一个转入钱的方法service层一个转账业务方法,内部分别调
    用dao层转出钱和转入钱的方法,准备工作如下:

    • 数据库准备一个账户表tb_account;
    • dao层准备一个AccountMapper,包括incrMoney和decrMoney两个方法;
    • service层准备一个transferMoney方法,分别调用incrMoney和decrMoney方法;
    • 在applicationContext文件中进行Bean的管理配置;
    • 测试正常转账与异常转账。

    要点代码

    xml配置文件

    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:cotext="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
    ">
        
        <cotext:component-scan base-package="com.Smulll"/>
        
        <cotext:property-placeholder location="classpath:jdbc.properties"/>
        
        <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
            <property name="driverClassName" value="${jdbc.driver}"/>
            <property name="url" value="${jdbc.url}"/>
            <property name="username" value="${jdbc.username}"/>
            <property name="password" value="${jdbc.password}"/>
        bean>
        
        <bean class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource">property>
        bean>
        
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="com.Smulll.mapper">property>
        bean>
    beans>
    
    • 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
    • 31
    • 32
    • 33

    Mapper映射文件

    public interface accountMapper {
        /*
        *   加钱
        * */
        @Update("update tb_account set money = money+#{money} where account_name = #{accountName}")
        public void incrMoney(@Param("accountName") String accountName,@Param("money") Double money);
        /*
        *   减钱
        * */
        @Update("update tb_account set money = money-#{money} where account_name = #{accountName}")
        public void decrMoney(@Param("accountName") String accountName,@Param("money") Double money);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    service层代码

    @Service("accountService")
    public class AccountServiceImpl implements AccountService {
        @Autowired
        private accountMapper accountMapper;
        public void transferMoney(String outAccount, String inAccount, Double money){
            accountMapper.decrMoney(outAccount,money);
            accountMapper.incrMoney(inAccount,money);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    1.3、基于XML声明式事务控制

    结合上面我们学习的AOP的技术,很容易就可以想到,可以使用AOP对Service的方法进行事务的增强。

    • 目标类:自定义的AccountServicelmpl,内部的方法是切点
    • 通知类: Spring提供的,通知方法已经定义好,只需要配置即可

    我们分析:

    • 通知类是Spring提供的,需要导入Spring事务的相关的坐标;
    • 配置目标类AccountServicelmpl;
    • 使用advisor标签配置切面。
    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:cotext="http://www.springframework.org/schema/context"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:tx="http://www.springframework.org/schema/tx"
           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
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx.xsd
    
    ">
        
        <cotext:component-scan base-package="com.Smulll"/>
        
        <cotext:property-placeholder location="classpath:jdbc.properties"/>
        
        <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
            <property name="driverClassName" value="${jdbc.driver}"/>
            <property name="url" value="${jdbc.url}"/>
            <property name="username" value="${jdbc.username}"/>
            <property name="password" value="${jdbc.password}"/>
        bean>
        
        <bean class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource">property>
        bean>
        
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="com.Smulll.mapper">property>
        bean>
    
        
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"/>
        bean>
    
        
        <tx:advice id="txAdvice" transaction-manager="transactionManager">
            <tx:attributes>
                <tx:method name="*"/>
            tx:attributes>
        tx:advice>
    
        
        <aop:config>
            
            <aop:pointcut id="txPointcut" expression="execution(* com.Smulll.service.Impl.*.*(..))"/>
            
            <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
        aop:config>
    beans>
    
    • 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
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
       <tx:attributes>
           
           <tx:method name="*" isolation="READ_COMMITTED" propagation="" timeout="3" read-only="false"/>
       tx:attributes>
    tx:advice>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    isolation属性:指定事务的隔离级别,事务并发存在三大问题:脏读不可重复读幻读/虚读。可以通过设置事务的隔离级别来保证并发问题的出现,常用的是READ_COMMITTEDREPEATABLE_READ

    isolation属性解释
    DEFAULT默认隔离级别,取决于当前数据库隔离级别,例如MySQL默认隔离级别是REPEATABLE_READ
    READ_UNCOMMITTEDA事务可以读取到B事务尚未提交的事务记录,不能解决任何并发问题,安全性最低,性能最高
    READ_COMMITTEDA事务只能读取到其他事务已经提交的记录,不能读取到未提交的记录。可以解决脏读问题,但是不能解决不可重复读和幻读
    REPEATABLE_READA事务多次从数据库读取某条记录结果一致,可以解决不可重复读,不可以解决幻读
    SERIALIZABLE串行化,可以解决任何并发问题,安全性最高,但是性能最低

    read-only属性:设置当前的只读状态,如果是查询则设置为true,可以提高查询性能,如果是更新(增删改)操作则设置为false

    
    <tx:method name="select*" read-only="true"/>
    <tx:method name="find*" read-only="false"/>
    
    • 1
    • 2
    • 3

    timeout属性:设置事务执行的超时时间,单位是秒,如果超过该时间限制但事务还没有完成,则自动回滚事务,不在继续执行。默认值是-1,即没有超时时间限制

    
    <tx:method name="select*" read-only="true" timeout="3" />
    
    • 1
    • 2

    propagation属性:设置事务的传播行为,主要解决是A方法调用B方法时,事务的传播方式问题的,例如:使用单方的事务,还是A和B都使用自己的事务等。事务的传播行为有如下七种属性值可配置

    事务传播行为解释
    REQUIRED (默认值)A调用B,B需要事务,如果A有事务B就加入A的事务中,如果A没有事务,B就自己创建一个事务
    REQUIRED_NEWA调用B,B需要新事务,如果A有事务就挂起,B自己创建一个新的事务
    SUPPORTSA调用B,B有无事务无所谓,A有事务就加入到A事务中,A无事务B就以非事务方式执行
    NOT_SUPPORTSA调用B,B以无事务方式执行,A如有事务则挂起
    NEVERA调用B,B以无事务方式执行,A如有事务则抛出异常
    MANDATORYA调用B,B要加入A的事务中,如果A无事务就抛出异常
    NESTEDA调用B, B创建一个新事务,A有事务就作为嵌套事务存在,A没事务就以创建的新事务执行

    1.4、基于注解声明式事务控制

    @Configuration
    @ComponentScan("com.Smulll")
    @PropertySource("classpath:jdbc.properties")
    @MapperScan("com.Smulll.mapper")
    @EnableTransactionManagement//相当于
    public class SpringConfig {
    
        @Bean
        public DataSource dataSource(
                @Value("${jdbc.driver}") String driver,
                @Value("${jdbc.url}") String url,
                @Value("${jdbc.username}") String username,
                @Value("${jdbc.password}") String password
        ){
            DruidDataSource dataSource = new DruidDataSource();
            dataSource.setDriverClassName(driver);
            dataSource.setUrl(url);
            dataSource.setUsername(username);
            dataSource.setPassword(password);
    
            return dataSource;
        }
    
        @Bean
        public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){
            SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
            sqlSessionFactoryBean.setDataSource(dataSource);
            return sqlSessionFactoryBean;
        }
    
        @Bean
        public DataSourceTransactionManager transactionManager(DataSource dataSource){
            DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
            dataSourceTransactionManager.setDataSource(dataSource);
            return dataSourceTransactionManager;
        }
    }
    
    • 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
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    其中,name属性名称指定哪个方法要进行哪些事务的属性配置,此处
    有尽要分的是切点表达式指定的方法与此处指定的方法的区别?切点表达式,是过滤哪些方法可以进行事务增强;事务属性信息的name,是指定哪个方法要进行哪些事务属性的配置
    在这里插入图片描述

    二、Spring整合web环境

    2.1、JavaWeb三大组件作用及其特点

    在Java语言范畴内,web层框架都是基于Javaweb基础组件完成的,所以有必要复习一下Javaweb组件的特点

    组件作用特点
    Servlet服务端小程序,负责接收客户端请求并作出响应的单例对象,默认第一次访问创建,可以通过配置指定服务器启动就创建,Servlet创建完毕会执行初始化init方法。每个Servlet有一个service方法,每次访问都会执行service方法,但是缺点是一个业务功能就需要配置一个Servlet
    Filter过滤器,负责对客户端请求进行过滤操作的单例对象,服务器启动时就创建,对象创建完毕执行init方法,对客户端的请求进行过滤,符合要求的放行,不符合要求的直接响应客户端,执行过滤的核心方法doFilter
    Listener监听器,负责对域对象的创建和属性变化进行监听的根据类型和作用不同,又可分为监听域对象创建销毁和域对象属性内容变化的,根据监听的域不同,又可以分为监听Request域的,监听Session域的,监听ServletContext域的

    2.2、Spring整合web环境的思路及实现

    在进行Java开发时要遵循三层架构+MVC,Spring操作最核心的就是Spring容器,web层需要注入Service,service层需要注入Dao (Mapper) , web层使用Servlet技术充当的话,需要在Servlet中获得Spring容器

    AnnotationConfigApplicationContext applicationContext =
    		new AnnotationConfigApplicationContext(ApplicationContextConfig.class);
    AccountService accountService = (AccountService)applicationContext.getBean("accountService");
    accountService.transferMoney("tom","lucy",100);
    
    • 1
    • 2
    • 3
    • 4

    web层代码如果都去编写创建AnnotationConfigApplicationContext的代码,那么配置类重复被加载了,Spring容器也重复被创建了,不能每次想从容器中获得一个Bean都得先创建一次容器,这样肯定是不允许。所以,我们现在的诉求很简单,如下:

    • ApplicationContext创建一次,配置类加载一次;
    • 最好web服务器启动时,就执行第1步操作,后续直接从容器中获取Bean使用即可;
    • ApplicationContext的引用需要在web层任何位置都可以获取到。

    针对以上诉求我们给出解决思路,如下:

    • 在ServletContextListener的contextInitialized方法中执行ApplicationContext的创建。或在Servlet的init方法中执行ApplicationContext的创建,并给Servlet的load-on-startup属性一个数字值,确保服务器启动Servlet就创建;
    • 将创建好的ApplicationContext存储到ServletContext域中,这样整个web层任何位置就都可以获取到了

    Listener的代码

    public class ContextLoaderListener implements ServletContextListener {
        System.out.println("ContextLoaderListener init..........");
            ServletContext servletContext = servletContextEvent.getServletContext();
            //0.获取contextConfigLocation配置文件的名称
            String contextConfigLocation = servletContext.getInitParameter(CONTEXT_CONFIG_LOCATION);
            //解析出配置文件的名称
            contextConfigLocation = contextConfigLocation.substring("classpath:".length());
            //1.创建Spring容器  执行一次
            ApplicationContext App = new ClassPathXmlApplicationContext(contextConfigLocation);
            //2.将容器存储到servletContext域中
            servletContextEvent.getServletContext().setAttribute("applicationContext",App);
        }
        @Override
        public void contextDestroyed(ServletContextEvent servletContextEvent) {
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    Servlet层的代码

    @WebServlet("/accountServlet")
    public class accountServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
            //通过request域获得servletContext
            ServletContext servletContext = request.getServletContext();
            //再通过applicationContext得到servletContext域里面的数据,强转成ApplicationContext类
            ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
            AccountService bean = applicationContext.getBean(AccountService.class);
            bean.transferMoney("李四","张三",500.0);
        }
        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            this.doGet(request, response);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    WebApplicationContextUtils

    public class WebApplicationContextUtils {
        public static ApplicationContext getWebApplicationContext(ServletContext servletContext){
            ApplicationContext applicationContext = (ApplicationContext)servletContext.getAttribute("applicationContext");
            return applicationContext;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    2.3、Spring的Web开发组件spring-web

    到此,就将一开始的诉求都解决了,当然我们能想到的Spring框架自然也会想到,Spring其实已经为我们定义好了一个ContextLoaderListener,使用方式跟我们上面自己定义的大体一样,但是功能要比我们强百倍,所以,遵循Spring "拿来主义"的精神,我们直接使用Spring提供的就可以了,开发如下:

    先导入Spring-web的坐标

    <dependency>
    	<groupId>org.springframeworkgroupId>
    	<artifactId>spring-webartifactId>
    	<version>5.3.7version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    web.xml

    
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
             version="4.0">
        
        <context-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:applicationContext.xmlparam-value>
        context-param>
    
        
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
        listener>
    web-app>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    @WebServlet("/accountServlet")
    public class accountServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
            //通过request域获得servletContext
            ServletContext servletContext = request.getServletContext();
            //再通过applicationContext得到servletContext域里面的数据,强转成ApplicationContext类
            ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
            AccountService bean = applicationContext.getBean(AccountService.class);
            bean.transferMoney("李四","张三",500.0);
        }
        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            this.doGet(request, response);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    2.4、web层MVC框架思想与设计思路

    Java程序员在开发一般都是MVC+三层架构,MVC是web开发模式,传统的Javaweb技术栈实现的MVC如下

    在这里插入图片描述
    原始Javaweb开发中,Servlet充当Controller的角色,Jsp充当View角色,JavaBean充当模型角色,后期Ajax异步流行后,在加上现在前后端分离开发模式成熟后,View就被原始Html+Vue替代。原始Javaweb开发中,Service充当Controller有很多弊端,显而易见的有如下几个:

    Servlet作为Controller的问题解决思路和方案
    每个业务功能请求都对应一个Servlet根据业务模块去划分Controller
    每个Servlet的业务操作太繁琐将通用的行为,功能进行抽取封装
    Servlet获得Spring容器的组件只能通过客户端代码去获取,不能优雅的整合通过spring的扩展点,去封装一个框架,从原有的Servlet完全接手过来web层的业务

    负责共有行为的Servlet称之为前端控制器,负责业务行为的JavaBean称之为控制器Controller

    在这里插入图片描述
    分析前端控制器基本功能如下:

    1. 具备可以映射到业务Bean的能力
    2. 具备可以解析请求参数、封装实体等共有功能
    3. 具备响应视图及响应其他数据的功能
  • 相关阅读:
    java多线程-线程间通信
    计算机毕设(附源码)JAVA-SSM健身房管理系统
    slurm是什么,怎么用? For slurm和For Pytorch有什么区别和联系?
    计算机英文论文写作需要注意哪些细节? - 易智编译EaseEditing
    Fastflow——基于golang的轻量级工作流框架
    绝地求生【商城更新】WIA联名上架//专属商店下架
    翻页视图ViewPager
    案例研究 | 玉湖冷链基于JumpServer实现管理安全一体化
    无水印免费4K视频素材网站 可商用-Free Stock Video
    奇舞周刊第 458 期 浅谈低代码平台远程组件加载方案
  • 原文地址:https://blog.csdn.net/weixin_74155781/article/details/132710509