1.     两种方式:

a)     使用Annotation

b)     使用xml

2.     Annotation

a)     加上对应的xsd文件spring-aop.xsd

b)     beans.xml <aop:aspectj-autoproxy />

c)     此时就可以解析对应的Annotation了

d)     建立我们的拦截类

e)     用@Aspect注解这个类

f)      建立处理方法

g)     用@Before来注解方法

h)     写明白切入点(execution …….)

i)      让spring对我们的拦截器类进行管理@Component

3.     常见的Annotation:

a)     @Pointcut  切入点声明以供其他方法使用 , 例子如下:

@Aspect

@Component

publicclass LogInterceptor {

   

@Pointcut("execution(public * com.bjsxt.dao..*.*(..))")

publicvoid myMethod(){}

@Around("myMethod()")

publicvoidbefore(ProceedingJoinPoint pjp) throws Throwable{

System.out.println("method before");

pjp.proceed();

}

@AfterReturning("myMethod()")

publicvoid afterReturning() throws Throwable{

System.out.println("method afterReturning");

}

@After("myMethod()")

publicvoid afterFinily() throws Throwable{

System.out.println("method end");

}

}

b)     @Before 发放执行之前织入

c)     @AfterReturning 方法正常执行完返回之后织入(无异常)

d)     @AfterThrowing 方法抛出异常后织入

e)     @After 类似异常的finally

f)      @Around 环绕类似filter , 如需继续往下执行则需要像filter中执行FilterChain.doFilter(..)对象一样 执行 ProceedingJoinPoint.proceed()方可,例子如下:

@Around("execution(* com.bjsxt.dao..*.*(..))")

publicvoidbefore(ProceedingJoinPoint pjp) throws Throwable{

System.out.println("method start");

pjp.proceed();//类似FilterChain.doFilter(..)告诉jvm继续向下执行

}

4.     织入点语法

a)     void !void

b)     参考文档(* ..)

如果execution(* com.bjsxt.dao..*.*(..))中声明的方法不是接口实现则无法使用AOP实现动态代理,此时可引入包” cglib-nodep-2.1_3.jar” 后有spring自动将普通类在jvm中编译为接口实现类,从而打到可正常使用AOP的目的.

5.     xml配置AOP

a)     把interceptor对象初始化

b)     <aop:config

i.         <aop:aspect …..

1.     <aop:pointcut

2.     <aop:before

例子:

<bean id="logInterceptor" class="com.bjsxt.aop.LogInterceptor"></bean>

<aop:config>

<!-- 配置一个切面 -->

<aop:aspect id="point" ref="logInterceptor">

<!-- 配置切入点,指定切入点表达式 -->

<!-- 此句也可放到 aop:aspect标签外依然有效-->

<aop:pointcut

expression=

"execution(public* com.bjsxt.service..*.*(..))"

id="myMethod"/>

<!-- 应用前置通知 -->

<aop:before method="before"pointcut-ref="myMethod" />

<!-- 应用环绕通知需指定向下进行 -->

<aop:around method="around"pointcut-ref="myMethod" />

<!-- 应用后通知 -->

<aop:after-returning method="afterReturning"

pointcut-ref="myMethod"/>

<!-- 应用抛出异常后通知 -->

<aop:after-throwing method="afterThrowing"

pointcut-ref="myMethod"/>

<!-- 应用最终通知  -->

<aop:after method="afterFinily"

pointcut="execution(public* om.bjsxt.service..*.*(..))" />

</aop:aspect>

</aop:config>

Spring AOP配置与应用的更多相关文章

  1. Spring AOP配置方式

    AOP 面向切面编程,允许在 java 应用中的方法调用的前后做一些处理. 本文通过实例介绍两种主要的Spring AOP 配置方式:xml 方式配置,注解方式配置 XML 方式配置 1. 项目包类结 ...

  2. Java--简单的Spring AOP配置以及AOP事物管理,JDK/GCLib动态代理

    一.看一下简单的通过XML的AOP配置 1.首先创建一个简单的Student类 public class Student { private Integer age; private String n ...

  3. spring aop配置文档部分翻译

    欢迎转载交流: http://www.cnblogs.com/shizhongtao/p/3476973.html 下面的文字来自官方文档的翻译,具体事例以后奉上. Advisors "ad ...

  4. spring aop配置及用例说明(2)

    欢迎交流转载:http://www.cnblogs.com/shizhongtao/p/3473362.html 这里先介绍下几个annotation的含义, @Before:表示在切入点之前执行. ...

  5. spring aop配置及用例说明(1)

    欢迎转载交流,博客地址http://www.cnblogs.com/shizhongtao/p/3469776.html 首先,什么是aop,其实通俗一点讲就是,再方法执行时候我们加入其它业务逻辑.比 ...

  6. Spring——AOP配置时的jar包异常

    首先:这不是SSH整合的,这是单独配置Spring AOP的一个小例子. 所需要的jar包:如图: 我在这里出现的两个问题: 1.没有导入asm的jar包. 所报的异常为: java.lang.Cla ...

  7. Spring AOP配置简单记录(注解及xml配置方式)

    在了解spring aop中的关键字(如:连接点(JoinPoint).切入点(PointCut).切面(Aspact).织入(Weaving).通知(Advice).目标(Target)等)后进行了 ...

  8. perf4j+spring+aop 配置 注解方式

    今天将perf4j基于spring aop方式进入了接入,接入方法还是比较简单.具体配置如下: logback.xml <!--perf4j配置--> <appender name= ...

  9. Spring学习笔记之二----基于XML的Spring AOP配置

    在Spring配置文件中,通常使用<aop:config>元素来设置AOP,其中应包括: <aop:aspect>指定aspect,aspect是一个POJO类,包含了很多的a ...

随机推荐

  1. 水题(素数表)NYOJ素数距离

                描述 现在给出你一些数,要求你写出一个程序,输出这些整数相邻最近的素数,并输出其相距长度.如果左右有等距离长度素数,则输出左侧的值及相应距离. 如果输入的整数本身就是素数,则输 ...

  2. C# 绘制窗体客户非客户区要用WM_PAINT和WM_NCPAINT

    窗体分为两部分:客户区(Client area)和非客户区(Non-Client area) WM_PAINT消息.OnPaint()方法.GetDC()API函数都是处理窗体客户区绘制的   而标题 ...

  3. html5 拖拽的简要介绍

    1,首先,你要告诉计算机那个元素可以拖动,或者是一张图,或者是一个盒子,在标签里面加上 draggable="true"  2,然后,监听该元素被拖动的函数 ondragstart ...

  4. Vim自动补全神器:YouCompleteMe(转)

    转自:http://blog.jobbole.com/58978/ 可能会有一段时间写linxu,免不了用vim,留着,找时间实操之 原文出处: marchtea 的博客 第一次听说这个插件还是在偶然 ...

  5. 【elasticsearch】(2)centos7 超简单安装elasticsearch 的监控、测试的集群工具elasticsearch head

    elasticsearch-head是elasticsearch(下面称ES)比较普遍使用的可监控.测试等功能的集群管理工具,是由H5编写的单独的网页程序.使用方法网上很多,这里教大家一个超简单安装h ...

  6. JS中的 this

    JS中的 this 变化多端,似乎难以捉摸,但实际上对 this 的解读,还是有一定规律的. 分析this,该如何下手呢?下面有一个函数 function show(){ alert(this); } ...

  7. thinkphp3.2 namespace及use用法

    PHP 5.3中的namespace其实是个不错的东西,可以简化编程,下面介绍三类在代码中 访问namespace中类的方法 1 引用namespace和类   假设namespace的程序为name ...

  8. Android 开机过程PMS分析

    12-13 12:25:05.595 3253 3253 SystemServer !@Boot: Start PackageManagerService pm/PackageManagerServi ...

  9. Python正则表达式2

  10. sourceInsight的技巧

    在用sourceInsight看代码...在这里积累技巧,慢慢积累吧 1.如何高亮显示所有要搜的东西,例如 1.aaaaaa 2. bbbbbbbbaaaaaaa 3. ccccccc 4. aaaa ...