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. 15个具有高度影响力的Apache开源项目

    自1999年创立以来,Apache软件基金会如今已成了众多重要的开源软件项目之家.其中成功的项目有Geronimo,有Tomcat,有Hadoop,有如今成了大数据王国关键车毂的分布式计算系统. 虽然 ...

  2. Linux命令缩写来由

    A 命令 全称 功能 备注 apt AdvancedPackaging Tool APT用来自动下载,配置,安装二进制或者源代码格式的软件包   awk Aho Weiberger and Kerni ...

  3. Android开发之Navigationdrawer导航抽屉功能的实现(源码分享)

    导航抽屉(navigationdrawer)是一个从屏幕左边滑入的面板,用于显示应用的主要导航项目.用户能够通过在屏幕左边缘滑入或者触摸操作栏的应用图标打开导航抽屉. 导航抽屉覆盖在内容之上,但不覆盖 ...

  4. 用Spark查询HBase中的表数据

    java代码如下: package db.query; import org.apache.commons.logging.Log; import org.apache.commons.logging ...

  5. 机器学习理论与实战(十一)关联规则分析Apriori

    <机器学习实战>的最后的两个算法对我来说有点陌生,但学过后感觉蛮好玩,了解了一般的商品数据关联分析和搜索引擎智能提示的工作原理.先来看看关联分析(association analysis) ...

  6. OpenGL使用libPng读取png图片

    #include<stdarg.h> #include<png.h> #include<glut.h> #include<math.h> #includ ...

  7. MongoDB学习笔记(一)--基础

    Insert                                                                                        MongoD ...

  8. angularJs的一次性数据绑定:双冒号::

    angularJs 中双冒号 ::来实现一次性数据绑定. 原文: https://blog.csdn.net/qianxing111/article/details/79971544 -------- ...

  9. Logistic Regression的几个变种

    原文:http://blog.xlvector.net/2014-02/different-logistic-regression/ 最近几年广告系统成为很多公司的重要系统之一,定向广告技术是广告系统 ...

  10. 1-2Html与CSS的关系

    Html和CSS的关系 基于微信的后台开发须要掌握的基础知识包括有:HTML.CSS.JavaScript语言. 以下我们就来了解下这三门技术都是用来实现什么的: 1. HTML是网页内容的载体.内容 ...