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. 3DTouch

    3DTouch 一.主屏按压(Home Screen Quik Actions) 1.静态标签 在info.plist文件中新增项 关键字 意义 UIApplicationShortcutItems ...

  2. cocos2d-x系列 Mac下配置cocos2d-x开发环境(android和ios)

    一.下载cocos2d-x http://cocos2d-x.org/projects/cocos2d-x/wiki/Download cocos2d-x-2.1.4.zip @ June.18, 2 ...

  3. C语言中short的意思

    short和int等一样,是C或C++的一种内部数据类型.用于表示有符号整数.不同的是,他们在内存中所占的空间大小不同,short通常为int所占一半,也有一些实现为和int一样,但不会比int大.所 ...

  4. red bottom shoes featured

    最近做了一个红底高根鞋的电商网站 Cheap Red Bottom Shoes Christian Louboutin Loafers Bestsellers Christian Louboutin ...

  5. CSS一级导航-天蓝色(带阴影)

    一款亮丽的导航,能给网站一个画龙点睛的作用.导航在指引用户搜寻内容时,还能改变用户浏览网站的心情,浏览网站也像一场旅行,有创意的导航栏让用户欣赏起来也会更加愉悦,增加对网站的兴趣. 本人不擅长美工制作 ...

  6. 用gtest实现数据驱动的单元测试

    //使用gtest进行数据驱动的单元测试 #include <gtest/gtest.h> #include <iostream> #include <vector> ...

  7. hg vs git :这个世界除了svn还有别的

    最近想用版本控制软件来保存汉化文件,但又觉得SVN太麻烦,于是想到了最近较为流行的分布式版本控制工具.而Git和Mercurial(意思为水银的,于是经常缩写为Hg)自然是其中最为流行的工具.大名鼎鼎 ...

  8. Data guard RAC配置【二】

    2. 利用duplicate配置容灾端 1.配置容灾端oracle用户的环境变量,这里以192.166.1.61为例. export ORACLE_BASE=/opt/oracle export OR ...

  9. LA 3904

    一道DP题: 一共有三种砖,1*2,2*1,2*2,然后要你铺满整个n*2的地板,求不重复的铺法数: 方法: 首先计算了不考虑对称的情况,然后计算只考虑对称的情况: 所以结果就是(不考虑对称数+只考虑 ...

  10. httpclient 超时设置

    最近项目客户反应超时经常出现:现已经总结超时设置: 使用是apache的HttpClient: DefaultHttpClient:请求超时httpclient.getParams().setPara ...