aop注解 事例
spring.xml中aop的配置
<!--
aop的配置
-->
<aop:config>
<!--
切入点表达式
-->
<aop:pointcut expression="execution(* cn.itcast.spring.aop.sh.PersonDaoImpl.*(..))" id="perform"/>
<aop:aspect ref="myTransaction">
<!--
<aop:before method="beginTransaction" pointcut-ref="perform"/>
<aop:after-returning method="commit" pointcut-ref="perform" returning="var"/>
-->
<aop:after method="finallyMethod" pointcut-ref="perform"/>
<aop:after-throwing method="throwingMethod" pointcut-ref="perform" throwing="ex"/>
<aop:around method="aroundMethod" pointcut-ref="perform"/>
</aop:aspect>
</aop:config>
自定义的切面
注解形式的aop 写着 切面上
package cn.itcast.spring.aop.annotation.sh; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.hibernate.Transaction;
import org.springframework.stereotype.Component; /**
* @Aspect
* ==
* <aop:config>
* <aop:pointcut
* expression=
* "execution(* cn.itcast.spring.aop.annotation.sh.PersonDaoImpl.*(..))"
* id="aa()"/>
* </aop:config>
* @author Think
*
*/
@Component("myTransaction")
@Aspect
public class MyTransaction extends HibernateUtils{
private Transaction transaction; @Pointcut("execution(* cn.itcast.spring.aop.annotation.sh.PersonDaoImpl.*(..))")
private void aa(){}//方法签名 返回值必须是void 方法的修饰符最好是private ,aa是随便定义的 做类比 @Before("aa()")
public void beginTransaction(JoinPoint joinpoint){
this.transaction = sessionFactory.getCurrentSession().beginTransaction();
} @AfterReturning(value="aa()",returning="val")
public void commit(Object val){
this.transaction.commit();
} }
然后在spring.xml中引入
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <!-- 扫描注解bean -->
<context:component-scan base-package="cn.us.aspect"/>
<!-- 开启切面代理 使得spring认识 @Aspect -->
<aop:aspectj-autoproxy/>
aop注解 事例的更多相关文章
- 来一手 AOP 注解方式进行日志记录
系统日志对于定位/排查问题的重要性不言而喻,相信许多开发和运维都深有体会. 通过日志追踪代码运行状况,模拟系统执行情况,并迅速定位代码/部署环境问题. 系统日志同样也是数据统计/建模的重要依据,通过分 ...
- Spring详解(六)------AOP 注解
上一篇博客我们讲解了 AspectJ 框架如何实现 AOP,然后具体的实现方式我们是通过 xml 来进行配置的.xml 方式思路清晰,便于理解,但是书写过于麻烦.这篇博客我们将用 注解 的方式来进行 ...
- Spring详解(七)------AOP 注解
上一篇博客我们讲解了 AspectJ 框架如何实现 AOP,然后具体的实现方式我们是通过 xml 来进行配置的.xml 方式思路清晰,便于理解,但是书写过于麻烦.这篇博客我们将用 注解 的方式来进行 ...
- SpringBoot —— AOP注解式拦截与方法规则拦截
AspectJ是一个面向切面的框架,它扩展了Java语言.AspectJ定义了AOP语法,所以它有一个专门的编译器用来生成遵守Java字节编码规范的Class文件. SpringBoot中AOP的使用 ...
- Spring AOP注解为什么失效?90%Java程序员不知道
使用Spring Aop注解的时候,如@Transactional, @Cacheable等注解一般需要在类方法第一个入口的地方加,不然不会生效. 如下面几种场景 1.Controller直接调用Se ...
- Spring入门(三)— AOP注解、jdbc模板、事务
一.AOP注解开发 导入jar包 aop联盟包. aspectJ实现包 . spring-aop-xxx.jar . spring-aspect-xxx.jar 导入约束 aop约束 托管扩展类和被扩 ...
- SpringBoot整合Mybatis多数据源 (AOP+注解)
SpringBoot整合Mybatis多数据源 (AOP+注解) 1.pom.xml文件(开发用的JDK 10) <?xml version="1.0" encoding=& ...
- Spring AOP注解形式简单实现
实现步骤: 1:导入类扫描的注解解析器 命名空间:xmlns:context="http://www.springframework.org/schema/context" xsi ...
- AOP注解不起作用的debug结果
经过2天的调试,我发现AOP注解配置不起作用居然是表达式的错误导致的 在xml文件中配置的base-package有关,初步认为@PointCut只能使用base-package..*(..)这样的方 ...
随机推荐
- DIV元素不换行
DIV盒子默认是换行独占100%宽度:DIV盒子没有赋予CSS样式时,默认DIV盒子是独占一行(宽度为100%). 如下默认情况HTML代码: <!DOCTYPE html> <ht ...
- 不明白的sizeof(enum)数据结构存储问题
不明白的sizeof(enum)数据结构存储问题 typedef struct weekday_st { enum week {sun=123456789,mon,tue,wed,thu,fri,sa ...
- Linux 提权常用命令集
转载:http://www.myhack58.com/Article/html/3/8/2017/83236.htm 0x00 操作系统相关 操作系统类型版本 cat /etc/issue cat / ...
- 学习笔记:A*算法
简易地图 如图所示简易地图, 其中绿色方块的是起点 (用 A 表示), 中间蓝色的是障碍物, 红色的方块 (用 B 表示) 是目的地. 为了可以用一个二维数组来表示地图, 我们将地图划分成一个个的小方 ...
- iOS:制作一个简易的计算器
初步接触视图,制作了一个简易的计算器,基本上简单的计算是没有问题的,不是很完美,可能还有一些bug,再接再厉. // // ViewController.m // 计算器 // // Created ...
- SQL Server’s Storage Top 10 Best Practices
好文章, 简明扼要. Storage Top 10 Best Practices http://technet.microsoft.com/en-us/library/cc966534.aspx
- Android数据解析-JSON解析
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,基于JavaScript(Standard ECMA-262 3rd Edition - December ...
- 比较全的OA系统功能模块列表
如何判断一款协同OA软件,是否智能,是否注重细节,是否足够成熟呢?产品的设计优势.功能特性,需要我们总结,也需要让更多的用户了解.功能到底强在哪里?下文中将给出一个详尽的答案. 软件安装 傻瓜化向导式 ...
- Linux c 屏蔽信号、切换信号
信号导致的问题 不是任何信号我们都需要的,如果遇到我们不想处理的信号,我们怎么避免这个信号? 1. 信号屏蔽 intsigprocmask(int how,//操作方式 SIG_BLOCK屏 ...
- A5-1和DES两个加密算法的学习
A5-1加密算法 1.基本原理 A5-1加密算法是一种流password,通过密钥流对明文进行加密.然后用密钥流进行对密文的解密操作. 这样的算法主要用于GSM加密.也就是我们平时打电话的时候.通信数 ...