当前位置: 首页 > news >正文

天眼查官网查询入口seo优化网站优化排名

天眼查官网查询入口,seo优化网站优化排名,cms 免费,网站建设目的分析Spring学习笔记——4 一、基于AOP的声明式事务控制1.1、Spring事务编程概述1.2、搭建测试环境1.3、基于XML声明式事务控制1.4、基于注解声明式事务控制 二、Spring整合web环境2.1、JavaWeb三大组件作用及其特点2.2、Spring整合web环境的思路及实现2.3、Spring的Web开发组件spri…

Spring学习笔记——4

  • 一、基于AOP的声明式事务控制
    • 1.1、Spring事务编程概述
    • 1.2、搭建测试环境
    • 1.3、基于XML声明式事务控制
    • 1.4、基于注解声明式事务控制
  • 二、Spring整合web环境
    • 2.1、JavaWeb三大组件作用及其特点
    • 2.2、Spring整合web环境的思路及实现
    • 2.3、Spring的Web开发组件spring-web
    • 2.4、web层MVC框架思想与设计思路

一、基于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配置文件

<?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:cotext="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd
"><!--组件扫描--><cotext:component-scan base-package="com.Smulll"/><!--加载properties文件--><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><!--配置SqlSessionFactoryBean,作用将SqlSessionFactory存储到Spring容器--><bean class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource"></property></bean><!--MapperScannerConfigurer,作用扫描指定的包,产生Mapper对象存储到Spring容器--><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com.Smulll.mapper"></property></bean>
</beans>

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);
}

service层代码

@Service("accountService")
public class AccountServiceImpl implements AccountService {@Autowiredprivate accountMapper accountMapper;public void transferMoney(String outAccount, String inAccount, Double money){accountMapper.decrMoney(outAccount,money);accountMapper.incrMoney(inAccount,money);}
}

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

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

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

我们分析:

  • 通知类是Spring提供的,需要导入Spring事务的相关的坐标;
  • 配置目标类AccountServicelmpl;
  • 使用advisor标签配置切面。
<?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: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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsd"><!--组件扫描--><cotext:component-scan base-package="com.Smulll"/><!--加载properties文件--><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><!--配置SqlSessionFactoryBean,作用将SqlSessionFactory存储到Spring容器--><bean class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource"></property></bean><!--MapperScannerConfigurer,作用扫描指定的包,产生Mapper对象存储到Spring容器--><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><!--事务增强的aop--><tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><tx:method name="*"/></tx:attributes></tx:advice><!--事务增强的AOP--><aop:config><!--配置切点表达式--><aop:pointcut id="txPointcut" expression="execution(* com.Smulll.service.Impl.*.*(..))"/><!--配置织入关系 通知advice-ref引入Spring提供好的--><aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/></aop:config>
</beans>
<!--事务增强的aop-->
<tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><!--配置不同的方法的事务属性name:方法名称   *代表通配符isolaton:控制事务的隔离级别timeout:超时时间 默认-1 单位是秒例:设置为3 则若3秒内事务没有提交则系统回自动返回read-only:是否只读  查询操作设置为只读,其他的为falsepropagation:事务的传播行为 解决业务方法调用业务方法(事务嵌套问题)--><tx:method name="*" isolation="READ_COMMITTED" propagation="" timeout="3" read-only="false"/></tx:attributes>
</tx:advice>

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"/>

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

<!--设置查询操作的超时时间是3秒-->
<tx:method name="select*" read-only="true" timeout="3" />

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//相当于<tx:annotation-driven transaction-manager="transactionManager"/>
public class SpringConfig {@Beanpublic 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;}@Beanpublic SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();sqlSessionFactoryBean.setDataSource(dataSource);return sqlSessionFactoryBean;}@Beanpublic DataSourceTransactionManager transactionManager(DataSource dataSource){DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();dataSourceTransactionManager.setDataSource(dataSource);return dataSourceTransactionManager;}
}

其中,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);

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);}@Overridepublic void contextDestroyed(ServletContextEvent servletContextEvent) {}
}

Servlet层的代码

@WebServlet("/accountServlet")
public class accountServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");//通过request域获得servletContextServletContext servletContext = request.getServletContext();//再通过applicationContext得到servletContext域里面的数据,强转成ApplicationContext类ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);AccountService bean = applicationContext.getBean(AccountService.class);bean.transferMoney("李四","张三",500.0);}@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this.doGet(request, response);}
}

WebApplicationContextUtils

public class WebApplicationContextUtils {public static ApplicationContext getWebApplicationContext(ServletContext servletContext){ApplicationContext applicationContext = (ApplicationContext)servletContext.getAttribute("applicationContext");return applicationContext;}
}

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

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

先导入Spring-web的坐标

<dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>5.3.7</version>
</dependency>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><!--配置Listener--><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>
</web-app>
@WebServlet("/accountServlet")
public class accountServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");//通过request域获得servletContextServletContext servletContext = request.getServletContext();//再通过applicationContext得到servletContext域里面的数据,强转成ApplicationContext类ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);AccountService bean = applicationContext.getBean(AccountService.class);bean.transferMoney("李四","张三",500.0);}@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this.doGet(request, response);}
}

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. 具备响应视图及响应其他数据的功能
http://www.tj-hxxt.cn/news/28042.html

相关文章:

  • 网站建设就业怎么样自己建网站的详细步骤
  • 西安22日感染数据大连seo顾问
  • 杭州建设网站需要多少钱常见的搜索引擎有哪些?
  • 郑州专业的网站建设seo排名怎么看
  • 网站做批发文具小白如何学电商运营
  • 网站交易截图可以做证据吗商丘网络推广公司
  • 开通网站软件的会计科目怎么做网络广告营销的案例
  • 宁波网站建设最好代做百度关键词排名
  • 做网站一定要域名吗qq群引流推广软件
  • 海口会计报名网站软件定制
  • 曲阜网站建设公司网络推广代理怎么做
  • c 做交易网站关键词搜索排名优化
  • 经营性网站可以进行非经营行网站备案吗seo搜索引擎优化实训
  • 郑州建设工程信息网官网首页站长工具seo综合查询权重
  • 设计新颖的网站建站今日国内重大新闻事件
  • 凤凰网站ui专业设计百度seo外包
  • 小程序和网站开发难度谷歌独立站seo
  • 摄影网站设计素材抖音关键词查询工具
  • wordpress 做图片站seo搜索引擎优化排名报价
  • 建设银行网站用户名是什么意思新站seo优化快速上排名
  • 研艺影楼网站建设百度刷排名优化软件
  • 做服装招聘的网站站长工具关键词
  • 两个人 b站营业推广促销方式有哪些
  • 昌乐网站制作价格抖音广告投放代理商
  • 网站建设信息发布seo点击工具帮你火21星热情
  • 北京网站优化企业谷歌外贸
  • 工程平台网长沙seo网络推广
  • 长沙竞价网站建设报价纹绣培训班一般价格多少
  • 淘宝 网站建设外链交易平台
  • 域名注册后怎么建网站谷歌搜索引擎首页