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注解 事例的更多相关文章

  1. 来一手 AOP 注解方式进行日志记录

    系统日志对于定位/排查问题的重要性不言而喻,相信许多开发和运维都深有体会. 通过日志追踪代码运行状况,模拟系统执行情况,并迅速定位代码/部署环境问题. 系统日志同样也是数据统计/建模的重要依据,通过分 ...

  2. Spring详解(六)------AOP 注解

    上一篇博客我们讲解了 AspectJ 框架如何实现 AOP,然后具体的实现方式我们是通过 xml 来进行配置的.xml 方式思路清晰,便于理解,但是书写过于麻烦.这篇博客我们将用 注解 的方式来进行 ...

  3. Spring详解(七)------AOP 注解

    上一篇博客我们讲解了 AspectJ 框架如何实现 AOP,然后具体的实现方式我们是通过 xml 来进行配置的.xml 方式思路清晰,便于理解,但是书写过于麻烦.这篇博客我们将用 注解 的方式来进行 ...

  4. SpringBoot —— AOP注解式拦截与方法规则拦截

    AspectJ是一个面向切面的框架,它扩展了Java语言.AspectJ定义了AOP语法,所以它有一个专门的编译器用来生成遵守Java字节编码规范的Class文件. SpringBoot中AOP的使用 ...

  5. Spring AOP注解为什么失效?90%Java程序员不知道

    使用Spring Aop注解的时候,如@Transactional, @Cacheable等注解一般需要在类方法第一个入口的地方加,不然不会生效. 如下面几种场景 1.Controller直接调用Se ...

  6. Spring入门(三)— AOP注解、jdbc模板、事务

    一.AOP注解开发 导入jar包 aop联盟包. aspectJ实现包 . spring-aop-xxx.jar . spring-aspect-xxx.jar 导入约束 aop约束 托管扩展类和被扩 ...

  7. SpringBoot整合Mybatis多数据源 (AOP+注解)

    SpringBoot整合Mybatis多数据源 (AOP+注解) 1.pom.xml文件(开发用的JDK 10) <?xml version="1.0" encoding=& ...

  8. Spring AOP注解形式简单实现

    实现步骤: 1:导入类扫描的注解解析器 命名空间:xmlns:context="http://www.springframework.org/schema/context" xsi ...

  9. AOP注解不起作用的debug结果

    经过2天的调试,我发现AOP注解配置不起作用居然是表达式的错误导致的 在xml文件中配置的base-package有关,初步认为@PointCut只能使用base-package..*(..)这样的方 ...

随机推荐

  1. weblogic 12C 安全加强:更改Console及管理端口

    一.管理端口的好处:1.由于我们使用的是weblogic直接提供web访问,所以防火墙无法对80端口做安全策略. 2.所有用户都可以使用http://www.reyo.cn/console/来登陆我们 ...

  2. table固定首行(一)

    <html> <head> <title>Untitled Document</title> <meta http-equiv="Con ...

  3. ubuntu12.04-server版 倒腾

    前面的话: 第一份工作,在阿里巴巴干了一年零四个月不到几天,来到百度上海.从运维岗转到开发岗.从前端业务线转到后段数据线,基本上算从头开始,给自己鼓个劲. 进到正题,公司发了笔记本,自己原来那个破笔记 ...

  4. spring IOC的常见几种以来注入的方式

    在spring ioc中有三种依赖注入,分别是:a.接口注入:b.setter方法注入:c.构造方法注入: 接口注入: public class ClassA { private InterfaceB ...

  5. cocos2dxHellowoed 发现 2.2.3

    cocos2d 笔记 文件夹介绍 cocosdx ----->cocos2d主要代码 CocosDenshion---->cocos2d的声音的 Document------>文档 ...

  6. require.js 最佳实践

    require.js是一个js库,相关的基础知识,前面转载了两篇博文:Javascript模块化编程(require.js), Javascript模块化工具require.js教程,RequireJ ...

  7. IE9对HTML5中一部分属性不提供支持的原因

    为什么在IE9中对于HTML5标准中的离线应用程序以及CSS3中的一部分不提供支持?笔者间接了解到了这个原因. 微软日前已经发布了Internet Explorer 9(以下简称IE9)正式版.在该版 ...

  8. IIS6.0支持PHP设置

    找到了一份兼职,做网站的,但是公司里面服务器是用的IIS,Win2003系统,而且以前的网站都是aspx的.老板是我们学校的一个老师,我是被学长推荐过去了. --------------------- ...

  9. ftp-ftp权限

    在服务器上创建ftp站点时勾选的是读写权限对所有的用户开放,但是发现有些用户还是只能读取不能写入,后来发现是因为ftp指向的文件夹本身的权限没有打开导致的,解决办法是,设置ftp指向的文件夹的权限为u ...

  10. Kettle中调用用户自定义的jar包

    ETL工具断断续续的也接触了 Informatica,Kettle, SSIS,个人感觉Info很强大但是也很贵,而且有着一些神秘感.Kettle 4.0版本以来已经有了User  defined j ...