Spring AOP配置与应用
来源:互联网 发布:windows to go mac 编辑:程序博客网 时间:2024/04/30 14:45
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
例子:
<beanid="logInterceptor"class="com.bjsxt.aop.LogInterceptor"></bean>
<aop:config>
<!--配置一个切面 -->
<aop:aspectid="point"ref="logInterceptor">
<!--配置切入点,指定切入点表达式 -->
<!--此句也可放到 aop:aspect标签外依然有效-->
<aop:pointcut
expression=
"execution(public* com.bjsxt.service..*.*(..))"
id="myMethod"/>
<!--应用前置通知 -->
<aop:beforemethod="before"pointcut-ref="myMethod"/>
<!--应用环绕通知需指定向下进行 -->
<aop:aroundmethod="around"pointcut-ref="myMethod"/>
<!--应用后通知 -->
<aop:after-returningmethod="afterReturning"
pointcut-ref="myMethod"/>
<!--应用抛出异常后通知 -->
<aop:after-throwingmethod="afterThrowing"
pointcut-ref="myMethod"/>
<!--应用最终通知 -->
<aop:aftermethod="afterFinily"
pointcut="execution(public* om.bjsxt.service..*.*(..))"/>
</aop:aspect>
</aop:config>
- Spring AOP配置与应用
- Spring AOP配置与应用
- Spring学习笔记(二)----Spring AOP配置与应用
- spring aop与事务配置
- spring aop介绍与配置
- Spring AOP---基于Xml配置应用demo
- Spring aop切面实际应用 XML配置
- Spring aop切面实际应用 XML配置
- Spring AOP 实现原理与 CGLIB 应用
- Spring AOP 实现原理与 CGLIB 应用
- Spring AOP 实现原理与 CGLIB 应用
- Spring AOP 实现原理与 CGLIB 应用
- Spring AOP 实现原理与CGLIB应用
- Spring AOP 实现原理与 CGLIB 应用
- 【spring aop】实现原理与 CGLIB 应用
- Spring AOP实现原理与CGLIB应用
- Spring AOP 实现原理与 CGLIB 应用
- Spring AOP 实现原理与CGLIB应用
- 删除重做日志出现ORA-00362
- 古筝考级中的提示与要求【汇总】
- 业余时尚经典
- Spring IOC配置与应用
- c语言中的volatile关键字
- Spring AOP配置与应用
- 【每天一个Linux命令】21. 列出目录内容命令ls
- (应用目的)javascript高级教程学习笔记
- Django get_absolute_url 用法
- 怀孕的禁忌
- 中信i白金卡
- JavaScript 获取/设置光标位置,兼容Input&&TextArea
- 蝴蝶效应
- 无限分类