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..*(..)这样的方 ...
随机推荐
- 常见的Hadoop十大应用误解
常见的Hadoop十大应用误解 1. (误解) Hadoop什么都可以做 (正解) 当一个新技术出来时,我们都会去思考它在各个不同产业的应用,而对于平台的新技术来说,我们思考之后常会出现 ...
- 简明python教程 --C++程序员的视角(五):面向对象的编程
面向对象的编程 在大多数时候你可以使用过程性编程,但是有些时候当你想要编写大型程序或是寻求一个更加合适的解决方案的时候,你就得使用面向对象的编程技术. 对象可以使用普通的属于对象的变量存储数据.属于一 ...
- Web系统自己主动化部署脚本
Web开发的项目,除了在本地直接执行外,还可能常常须要在server上部署. 写了个自己主动化部署的脚本,仅供參考. 不少地方须要配置路径.个人建议使用绝对路径,不用依赖执行脚本时所在的路径. #!/ ...
- Python源码学习七 .py文件的解释
Python源码太复杂了... 今天看了下对.py文件的parse, 云里雾里的 py文件是最简单的, 在python的交互式窗口 import这个模块 a = 10 print(a) 开始分析,堆栈 ...
- Android完全退出程序、线程
1.先添加权限 <uses-permission android:name="android.permission.RESTART_PACKAGES" /> 2.添加A ...
- Sqrt(int x) leetcode java
Reference: http://blog.csdn.net/lbyxiafei/article/details/9375735 题目: Implement int sqrt(int x). Co ...
- 独立开发人员低成本推广APP的18条技巧
导语:知道并不等于运行,有些最主要的推广方法往往会被忽略.这些,是自国外开发人员总结出的这18条经验. 如今市面上充满了大牌子大公司和大制作的手机游戏,常常有游戏花300万成本开发,然后再花2000万 ...
- [Javascript] Prototype, hasOwnProperty(), valueOf() and toString() methods.
Sometime, use can rewrite the toString , valueOf method to make those function more useful: For exma ...
- hdu4848 求到达每一个点总时间最短(sum[d[i]])。
開始的时候是暴力dfs+剪枝.怎么也不行.后来參考他人思想: 先求出每一个点之间的最短路(这样预处理之后的搜索就能够判重返回了).截肢还是关键:1最优性剪枝(尽量最优:眼下的状态+估计还有的最小时间& ...
- Discuz常见小问题-如何修改导航栏
1 比如我要修改第一个导航栏,则在界面-导航设置,主导航,然后点击右边的编辑按钮 2 比如我把"首页"的名字改成"论坛首页",别的都不改,然后点击提交,刷新页面 ...