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. CSS Text文本格式

    Text Color 颜色属性被用来设置文字的颜色. 颜色是通过CSS最经常的指定: 十六进制值 - 如"#FF0000" 一个RGB值 - "RGB(255,0,0)& ...

  2. 搭建golang的beego注意事项

    大家都知道,在学golang的时候,大家都会去关注谢大的beego快速开发架构. 首先,小弟是win7 32bit系统,在这里,我要把我学习golang的过程和小心得记录起来. 相信想学的人一定会早早 ...

  3. eclipse EE neon创建dynamic web project时,卡在installing dynamic web module facet,解决办法

    我们在用eclipse EE neon创建dynamic web project时,如果你发现底部状态栏一直卡在installing dynamic web module facet,永远到不了100 ...

  4. java中的包装类

    每一个包装类都对应一种基本数据类型.包装类有:Integer.character.Byte.Short.Long.Floot.Double.Boolean这八种,分别对应的基本数据类型是:int.ch ...

  5. zoj1276矩阵连乘dp

    很经典的入门dp /*******************************************************************************/ /* OS : 3 ...

  6. 零售ERP开发(一)

    随着企业信息化不断提高,各种企业管理系统应用而生:怎么才能开发一套简洁.易用.功能健全的管理系统,尤其是能够帮助企业做出重大策略的系统,一致是我们追求的目标.近些年,接触的Erp系统主要包括国内比较知 ...

  7. node 后台ajax文件(同时支持http、https)

    var http = require("http"), Url = require("url"), querystring = require('queryst ...

  8. 最常见的HTTP错误

    1. HTTP 500错误(内部服务器错误)对对HTTP 500错误的定义已经充分证明了这是一个最常见的HTTP错误. 一般来说,HTTP 500 错误就是web服务器发生内部错误时返回的信息. 例如 ...

  9. reverse string | leetcode

    思路:在原来的字符串后面添加上strlen-1个字符,返回 class Solution { public: string reverseString(string s) { unsigned int ...

  10. 我们说的oc是动态运行时语言是什么意思?

    1.KVC和KVO区别,分别在什么情况下使用?  答:KVC(Key-Value-Coding) KVO(Key-Value-Observing)理解KVC与KVO(键-值-编码与键-值-监看) 当通 ...