`
别惹Java
  • 浏览: 44761 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

Spring的那些配置(事务和日志)

阅读更多

Spring整合Ibatis典型的配置文件

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


	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<property name="url"
			value="jdbc:mysql:///test?useUnicode=true&amp;characterEncoding=utf-8" />
		<property name="username" value="root" />
		<property name="password" value="root" />
		<property name="maxActive" value="100" />
		<property name="maxIdle" value="30" />
		<property name="maxWait" value="10000" />
		<property name="defaultAutoCommit" value="false" /> <!--设置自动提交 -->
	</bean>

	<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
		<property name="configLocation">
			<value>classpath:sqlMap-config.xml</value>
		</property>
		<property name="dataSource" ref="dataSource" />
	</bean>

	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>

	<!-- 事务通知      要注明rollback-for类型,并不是所有的异常都回滚的 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="add*" propagation="REQUIRED" rollback-for="Throwable"/>
			<tx:method name="del*" propagation="REQUIRED" rollback-for="Throwable"/>
			<tx:method name="mod*" propagation="REQUIRED" rollback-for="Throwable"/>
			<!--除了上面标识的方法,其他方法全是只读方法-->
			<tx:method name="*" read-only="true" />
		</tx:attributes>
	</tx:advice>

	<!-- 日志Bean -->
	<bean id="logService" class="com.ksfzhaohui.load.LogService"></bean>

	<!-- Spring AOP config -->
	<aop:config>
		<!-- 配置哪些类的方法需要进行事务管理 --> 
		<aop:pointcut id="newServicesPointcut"
			expression="execution(* com.ksfzhaohui.service.impl.*.*(..))" />
		<aop:advisor advice-ref="txAdvice" pointcut-ref="newServicesPointcut" />

		<!-- 日志管理 -->
		<aop:aspect id="myAspect" ref="logService">
			<aop:pointcut expression="execution(* com.ksfzhaohui.service.impl.*.*(..))"
				id="logPointCut" />
			<aop:before method="before" pointcut-ref="logPointCut" />
			<aop:after method="after" pointcut-ref="logPointCut" />
		</aop:aspect>
	</aop:config>

</beans>



1.Spring的事务配置
Spring使用 <tx:advice>和 <aop:config> 用来配置事务,具体如何配置你可以参考Spring文档
关于这里的expression="execution(* com.ksfzhaohui.service.impl.*.*(..))"分析
第一个*,表示任何返回值类型
第二个*,表示com.ksfzhaohui.service.impl包下的任何类
第三个*,表示com.ksfzhaohui.service.impl包下的任何类下的任意方法
最后的(..),表示方法可以有0个或多个参数

<tx:advice>中的相关属性

属性 是否需要 默认值 描述
name 与事务属性关联的方法名。
propagation 不是 REQUIRED 事务传播行为
isolation 不是 DEFAULT 事务隔离级别
timeout 不是 -1 事务超时的时间(以秒为单位)
read-only 不是 false 事务是否只读
rollback-for 不是 将被触发进行回滚的 Exception(s)以逗号分开。 
no-rollback-for 不是 不被触发进行回滚的 Exception(s)以逗号分开。 

 

 

2.日志配置

<aop:before>,<aop:after>分别表示在方法执行之前和之后要执行的内容

    典型案例

public class LogService {

	static final Log log = LogFactory.getLog(LogService.class);

	public void before(JoinPoint joinPoint) {
		StringBuffer sb=new StringBuffer();
		sb.append("logInfo--调用类" + joinPoint.getTarget().getClass().getName()+"--方法" + joinPoint.getSignature().getName());
		Object args[] = joinPoint.getArgs();
		for (int i = 0; i < args.length; i++) {
			sb.append("--参数" + i + ":" + args[i]);
		}
		log.info(sb);
	}

	public void after(JoinPoint joinPoint) {
		log.info("结束方法:" + joinPoint.getSignature().getName());
	}
}
 
分享到:
评论

相关推荐

    Spring Boot配置AOP打印日志的全过程

    主要给大家介绍了关于Spring Boot配置AOP打印日志的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Spring Boot具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧

    Spring--事务操作.pdf

    Spring中得事务操作一图详解-&gt;基于注解、基于xml配置文件、完全注解

    Spring Boot实战与原理分析视频课程包含14-18

    --演示了如何在Spring Boot里面使用日志配置,以及logback,log4j2等日志的使用 23 Spring Boot 监控和度量47:09 --Spring Boot内置的监控点、自定义的监控状况检查、自定义度量统计,输出等等 24 Spring Boot ...

    Spring的学习笔记

    一、 Spring配置hibernate3的SessionFactory 30 (一) xml形式的SessionFactory 30 (二) annotation注解方式的SessionFactory 30 二、 引入hibernate所需要使用的jar 31 (一) 基本jar 31 (二) 加入annotation功能的...

    spring-boot mybaits spring security redis整合

    logback打印日志,业务日志和调试日志分开打印。同时基于时间和文件大小分割日志文件。 9、项目构建 =========== mybatis generator生成mybatis映射文件。 请先安装eclipse mybatis-generator插件。 10、...

    SpringBatch+Spring+Mybatis+MySql (spring batch 使用jar)

    Spring Batch可以提供大量的,可重复的数据处理功能,包括日志记录/跟踪,事务管理,作业处理统计工作重新启动、跳过,和资源管理等重要功能。 业务方案: 1、批处理定期提交。 2、并行批处理:并行处理工作。 3、...

    Spring Boot详解

    分享我的Spring Boot 笔记文档,本文档从Spring 的演变说起,根据文档一步一步学习,让您全面理解Spring Boot的工作原理,...7、Spring Boot的事务管理; 8、Spring Boot的日志管理; 9、缓存支持、配置一详解等内容。

    spring.net中文手册在线版

    14.5.1.理解Spring.NET声明式事务管理的实现 14.5.2.第一个例子 14.5.3.Transaction特性的设置 14.5.4.通过AutoProxyCreator使用声明式事务 14.5.5.通过TransactionProxyFactoryObject使用声明式事务 14.5.6. 通过...

    spring3.0jar包

    ◆容器——Spring包含并管理应用对象的配置和生命周期,在这个意义上它是一种容器,你可以配置你的每个bean如何被创建——基于一个可配置原型(prototype),你的bean可以创建一个单独的实例或者每次需要时都生成一...

    spring的概要介绍与分析

    AOP则允许开发者在不影响业务逻辑代码的情况下,定义横切关注点,如日志、事务管理等,实现功能的模块化。 此外,Spring框架提供了丰富的功能组件,如数据访问(Spring Data JPA、MyBatis等)、Web开发(Spring MVC...

    spring boot 全面的样例代码

    - chapter1:[基本项目构建(可作为工程脚手架),引入...由于Spring Cloud偏宏观架构,Spring Boot偏微观细节,内容上越来越多,为了两部分内容不互相干扰,所以迁移Spring Cloud内容到:[SpringCloud-Learning项目]...

    Spring 中文API&开发文档.rar

    ◆容器——Spring包含并管理应用对象的配置和生命周期,在这个意义上它是一种容器,你可以配置你的每个bean如何被创建——基于一个可配置原型(prototype),你的bean可以创建一个单独的实例或者每次需要时都生成一...

    spring2.5 学习笔记

    一、 Spring配置hibernate3的SessionFactory 30 (一) xml形式的SessionFactory 30 (二) annotation注解方式的SessionFactory 30 二、 引入hibernate所需要使用的jar 31 (一) 基本jar 31 (二) 加入annotation功能的...

    spring boot2.0以上版本整合mybatis+pagehelper+druid

    2)配置数据库连接、配置 Spring 事务 3)配置加载配置文件的读取,开启注解 4)配置日志文件 … n) 配置完成之后部署 tomcat 调试 可能你还需要考虑各个版本的兼容性,jar 包冲突的各种可行性。 那么使用 ...

    基于Spring Boot 2和Spring Cloud Finchley.RELEASE,致力做更简洁的分布式和服务化解决方案

    roses-kernel 微服务框架的核心,利用spring boot自动配置,提供项目开发所需要的大部分配置,提供代码生成,jwt验证工具类,日志记录工具类,资源扫描,签名,参数校验等。本项目为Roses系列微服务框架的模块之一,...

    基于Spring Cloud的进销存管理系统源码

    采用前后端分离的模式,微服务版本前端vue 后端采用Spring Boot、Spring Cloud & Alibaba。注册中心、配置中心选型Nacos,权限认证使用Redis。流量控制框架选型Sentinel,分布式事务选型Seata。 商品管理:商品类型...

    spring-framework-3.1.0.RELEASE.zip

    容器——Spring包含并管理应用对象的配置和生命周期,在这个意义上它是一种容器,你可以配置你的每个bean如何被创建——基于一个可配置原型(prototype),你的bean可以创建一个单独的实例或者每次需要时都生成一个...

    使用MyEclipse创建Spring Boot项目demo

    2)配置数据库连接、配置 Spring 事务 3)配置加载配置文件的读取,开启注解 4)配置日志文件 … n) 配置完成之后部署 tomcat 调试 可能你还需要考虑各个版本的兼容性,jar 包冲突的各种可行性。 那么使用 ...

    Struts2SpringMybatis 增删改查

    Struts2SpringMybatis 增删改查,里面的Struts2,spring,Mybatis配置文件都是模块化的管理,实现了社会版本的配置,不再是个人玩弄的小项目。具体的 sql语句也已经加到了WEBRROT目录下面了,自己下载下来可以详细看...

    vip会员登录系统结合spring + mvc +hibernate

    其中包含apache的log4j记录日志信息,spring管理组件,springmvc分层,springaop配置数据库事务控制,hibernate二级缓存配置,实现了查询,用户登录注册,请求验证是否登录等基础功能Demo,基于后台测试,使用前台...

Global site tag (gtag.js) - Google Analytics