文章目录

  • Spring整合
    • 配置文件
      • 1.基本配置文件
      • 2.事务配置文件
    • web.xml中注册

  上篇文章中我们整合了Dao层,本篇文章将Spring整合进来。

Spring整合

  Spring的整合相对要简单很多,我们只要需要添加对应的配置文件,然后在web.xml文件中配置监听及配置文件路径。

配置文件

1.基本配置文件

  在src/main/resources/spring目录下新建一个applicationContext-service.xml文件,如下图所示。

配置文件中先配置扫描器即可,

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"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-4.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd"><!-- 配置扫描路径 --><context:component-scan base-package="com.bobo"use-default-filters="true"><context:exclude-filter type="annotation"expression="org.springframework.stereotype.Controller" /></context:component-scan>
</beans>

logistics-manager-service项目中创建对应的package

2.事务配置文件

  项目中事务肯定是需要开启的,所以我们单独添加一个事务管理的配置文件,如下图

说明:applicationContext-trans.xml文件的内容如下所示。其中事务的传播行为需要说明一下,当接口名以save、insert、add、create、delete、upate开头时spring会自动帮我们开启事务(前提是我们配置了事务传播行为),而find、select、get开头的接口是查询,不涉及更改数据库,因此不需要事务,spring不会为查询接口自动开启事务。下面再说说切面,也就是事务的作用范围, execution(* com.bobo.service..(…)) 的意思是,com.bobo.service下的任意类的任意方法的任意参数及任意返回值都是事务的切入点。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"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-4.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd"><!-- 事务管理器 --><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="save*" propagation="REQUIRED" /><tx:method name="insert*" propagation="REQUIRED" /><tx:method name="add*" propagation="REQUIRED" /><tx:method name="create*" propagation="REQUIRED" /><tx:method name="delete*" propagation="REQUIRED" /><tx:method name="update*" propagation="REQUIRED" /><tx:method name="find*" propagation="SUPPORTS" read-only="true" /><tx:method name="select*" propagation="SUPPORTS" read-only="true" /><tx:method name="get*" propagation="SUPPORTS" read-only="true" /></tx:attributes></tx:advice><!-- 切面 --><aop:config><aop:advisor advice-ref="txAdvice"pointcut="execution(* com.bobo.service.*.*(..))" /></aop:config>
</beans>

web.xml中注册

  我们创建的3个Spring的配置文件,相互之间是独立的,我们需要在logistics-manager-web项目的web.xml文件中配置,在服务器初始化的时候加载。

配置内容为:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" id="WebApp_ID" xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><display-name>taotao-manager</display-name><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list><!-- 初始化spring容器 --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring/applicationContext-*.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>
</web-app>

Spring整合完成~~

好好编程-物流项目06【Spring整合】相关推荐

  1. java 定时任务插件_详解Spring整合Quartz实现动态定时任务

    最近项目中需要用到定时任务的功能,虽然spring 也自带了一个轻量级的定时任务实现,但感觉不够灵活,功能也不够强大.在考虑之后,决定整合更为专业的Quartz来实现定时任务功能. 普通定时任务 首先 ...

  2. 【编程不良人】快速入门Spring学习笔记08---事务属性、Spring整合Structs2框架(SM)、Spring整合Mybatis+Struts2(SSM)、Spring注解、SSM注解式开发

    1. 事务属性 1.1 事务传播属性 配套视频:[编程不良人]快速入门Spring,SpringBoot.SpringCloud学不好完全是因为Spring没有掌握!_哔哩哔哩_bilibili # ...

  3. spring整合mina开发web项目和简单mina客户端的使用

    场景要求在web项目中使用mina与一些客户端通讯. 一.maven引包 <project xmlns="http://maven.apache.org/POM/4.0.0" ...

  4. ssh项目实战----Spring计时器任务 Spring整合JavaMail(邮件发送)

    一.常用数据频度维护 对于系统使用度较高的数据,客户在查看时希望这些数据最好先出现,此时需要为其添加排序规则.在进行排序时,使用次数成为排序的依据.因此需要设置一个字段用来描述某种数据的使用次数,也就 ...

  5. 【struts2+hibernate+spring项目实战】Spring计时器任务 Spring整合JavaMail(邮件发送)(ssh)

    一.常用数据频度维护 对于系统使用度较高的数据,客户在查看时希望这些数据最好先出现,此时需要为其添加排序规则.在进行排序时,使用次数成为排序的依据.因此需要设置一个字段用来描述某种数据的使用次数,也就 ...

  6. Spring整合ActiveMQ完成消息队列MQ编程

    <–start–> 第一步:新建一个maven,将工程命名为activeMQ_spring.在pom.xml文件中导入相关jar包. ①spring开发和测试相关的jar包: spring ...

  7. spring整合hibernate事务编程中错误分析

    2019独角兽企业重金招聘Python工程师标准>>> 在spring整合hibernate过程中,我们的配置文件: <?xml version="1.0" ...

  8. 《实战突击.php项目开发案例整合》.(明日科技).[PDF]ckook

    图书作者: 明日科技 图书编号: 9787121141140 图书格式: PDF 出 版 社: 电子工业出版社 出版年份: 2011 图书页数: 800-900 [内容简介] <实战突击:php ...

  9. Spring整合CXF,发布RSETful 风格WebService

    这篇文章是承接之前CXF整合Spring的这个项目示例的延伸,所以有很大一部分都是一样的.关于发布CXF WebServer和Spring整合CXF这里就不再多加赘述了.如果你对Spring整合CXF ...

最新文章

  1. redis zse如何取值_你真的懂redis的數據結構了嗎?redis內部數據結構和外部數據結構揭秘...
  2. 《唐山大地震》高清下载,迅雷下载,在线观看!
  3. 学习ASP.NET Core Razor 编程系列十八——并发解决方案
  4. 二层环路保护,RRPP多环的配置
  5. L2-006. 树的遍历-PAT团体程序设计天梯赛GPLT
  6. wpf判断一个窗体是否运行_算法8 判断一个数是否是回文数
  7. 中国人工智能论文首超美国,背后的秘密竟然是……
  8. sv_labs学习笔记——sv_lab2(System Verilog)
  9. 因需要**云音乐歌单转到apple music,不满**云音乐下载都是ncm格式,所以想办法转化格式
  10. UniFi AP 5.5.20的基本使用与设置(普通漫游和无缝漫游)
  11. 高考方向计算机基础试题题库,计算机基础考试题库 计算机基础考试题库(含答案).doc...
  12. 【渝粤教育】广东开放大学 建筑工程计量与计价 形成性考核 (47)
  13. linux恢复误删除文件
  14. 网易考拉海购产品分析报告
  15. buuctf crypto Quoted-printable
  16. 计算机更新配置卡住了,win10更新设置卡死怎么办|win10更新设置卡死的完美解决方法...
  17. 数据结构笔记:选择排序
  18. (P9)socket编程四:流协议与粘(nian)包,粘包产生的原因,粘包处理方案,readn,writen 6.回射客户/服务器
  19. 深入理解ceph-disk的工作机制
  20. 短距离无线通讯-NFC

热门文章

  1. java内部枚举类_内部类和枚举类
  2. DocBook学习(v1.6.7)
  3. 奶爸日记26 - 生日祝福
  4. 港科夜闻|“香港科大商学院-国科京东方”2021【人工智能】百万奖金国际创业大赛决赛成功举办,心鉴智控获得冠军。...
  5. 23级应届硕士招聘-给北京户口
  6. 耳机左右声道接反的问题
  7. cocosCreator 物理关节组件
  8. 我,37岁,从互联网大厂跳槽到国企后,我发现没有一劳永逸的工作
  9. 全新先发岁月云易支付网站系统源码+七色彩虹模版
  10. 软件设计师——数据流图(DFD) [ 笔记 ]