Spring AOP配置与应用
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配置与应用的更多相关文章
- Spring AOP配置方式
AOP 面向切面编程,允许在 java 应用中的方法调用的前后做一些处理. 本文通过实例介绍两种主要的Spring AOP 配置方式:xml 方式配置,注解方式配置 XML 方式配置 1. 项目包类结 ...
- Java--简单的Spring AOP配置以及AOP事物管理,JDK/GCLib动态代理
一.看一下简单的通过XML的AOP配置 1.首先创建一个简单的Student类 public class Student { private Integer age; private String n ...
- spring aop配置文档部分翻译
欢迎转载交流: http://www.cnblogs.com/shizhongtao/p/3476973.html 下面的文字来自官方文档的翻译,具体事例以后奉上. Advisors "ad ...
- spring aop配置及用例说明(2)
欢迎交流转载:http://www.cnblogs.com/shizhongtao/p/3473362.html 这里先介绍下几个annotation的含义, @Before:表示在切入点之前执行. ...
- spring aop配置及用例说明(1)
欢迎转载交流,博客地址http://www.cnblogs.com/shizhongtao/p/3469776.html 首先,什么是aop,其实通俗一点讲就是,再方法执行时候我们加入其它业务逻辑.比 ...
- Spring——AOP配置时的jar包异常
首先:这不是SSH整合的,这是单独配置Spring AOP的一个小例子. 所需要的jar包:如图: 我在这里出现的两个问题: 1.没有导入asm的jar包. 所报的异常为: java.lang.Cla ...
- Spring AOP配置简单记录(注解及xml配置方式)
在了解spring aop中的关键字(如:连接点(JoinPoint).切入点(PointCut).切面(Aspact).织入(Weaving).通知(Advice).目标(Target)等)后进行了 ...
- perf4j+spring+aop 配置 注解方式
今天将perf4j基于spring aop方式进入了接入,接入方法还是比较简单.具体配置如下: logback.xml <!--perf4j配置--> <appender name= ...
- Spring学习笔记之二----基于XML的Spring AOP配置
在Spring配置文件中,通常使用<aop:config>元素来设置AOP,其中应包括: <aop:aspect>指定aspect,aspect是一个POJO类,包含了很多的a ...
随机推荐
- Eclipse debug调试
Eclipse debug调试: F5:跳入方法F6:向下逐行调试F7:跳出方法F8:直接跳转到下一个断点
- 开发错误日志之IllegalArgumentException:MALFORMED
java.lang.IllegalArgumentException: MALFORMED 上面存在中文问题是因为java.util.zip下的格式转换有问题 ,jdk中的zip存在字符编码的问题. ...
- js获取天气
我们经常看到有的网站显示天气信息,它是怎么做出来的呢?今天就来分享一些关于js调用天气API的几种做法. 网上也能找到很多种方法,但是一般都是免费的不好用,好用的不免费. 以前用过新浪的天气api,查 ...
- php练习——打印半金字塔、金字塔、空心金字塔、菱形、空心菱形
半金字塔 金字塔 空心金字塔 菱形 空心菱形
- ng-html 报 不安全 警告解决办法
app.filter('to_trusted',['$sce',function($sce){ return function(text){ return $sce.trustAsHtml(text) ...
- 帝国cms 灵动标签【列表页】调用当前父栏目下的所有子栏目
<? $bclassid = $class_r[$GLOBALS[navclassid]][bclassid]; //获取当前父栏目ID ?> [e:loop={"select ...
- Warning File `.depend' has modification time 1.6 s in the future
一.前提知识 主机时间与虚拟机时间不同步所致.我们在某一操作系统所属磁盘空间下创建一个文件,那么这个文件的创建时间是以磁盘所属的操作系统的时钟为基准的. 我们假设主机windows的系统时间是10:0 ...
- 1、C# MVC学习之NVelocity基本使用
小白一个,刚刚开始学,大神不要笑话...... NVelocity是一个很容易上手的框架,从它开始学习,可以循序渐进 首先,创建空web应用程序,新建一般处理程序 Login2.ashx 然后,引入N ...
- 当xcode里点运行出现treating unicode character as whites
可能是由于粘贴网页上的代码的时候两行之间的回车引起的,两行之间重新输入回车就行......删掉重新写一遍就ok了 引入网页上的回车时 可能 网页对其格式做了处理,所以Xcode 不认识了
- 金山网络2014春季Android实习生招聘-成都站-笔试第一题
实现单例模式,并实现方法int getResult(float a),将a*8后返回. package jinshanwangluo.exam; /** * @author guoxm * @date ...