Spring AOP应用
来源:互联网 发布:战舰世界爱大狗数据 编辑:程序博客网 时间:2024/06/11 19:12
AOP应用
AOP面向切面编程
开发过程:编写好切面组件,然后通过配置方式切入到原有目标组件方法上。
切面:干啥
切入点:谁是目标
通知:切入时机
#案例-记录异常信息
切面:将异常信息写入文件
切入点:所有Controller组件是目标
within(org.tedu.cloudnote.controller..*)
通知:异常通知
<aop:after-throwing>
代码:
Spring-bean.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsdhttp://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"><!-- 扫描所有Controller,Service组件 --><context:component-scan base-package="org.tedu.cloudnote"/><!-- AOP示例 --><!-- 将LoggerBean组件的logController方法功能作用到所有的Controller组件execute方法上 --><!-- <bean id="loggerBean" class="org.tedu.cloudnote.aop.LoggerBean"></bean><aop:config><aop:aspect ref="loggerBean"><aop:before method="logController"pointcut="within(org.tedu.cloudnote.controller..*)"/></aop:aspect></aop:config> --><!-- AOP示例:异常处理 --><!-- <bean id="handlerException" class="org.tedu.cloudnote.aop.HandlerException"></bean><aop:config> <aop:aspect ref="handlerException"><aop:after-throwing method="execute" throwing="ex"pointcut="within(org.tedu.cloudnote.controller..*)"/> </aop:aspect></aop:config> --> <!-- 开启AOP注解配置,支持@Aspect,@AfterThrowing --> <aop:aspectj-autoproxy proxy-target-class="true"/></beans>
HandleException.java
package org.tedu.cloudnote.aop;import java.io.FileWriter;import java.io.PrintWriter;import org.aspectj.lang.annotation.AfterThrowing;import org.aspectj.lang.annotation.Aspect;import org.springframework.stereotype.Component;@Component("handlerException")//扫描等价于<bean>@Aspect//定义为切面,等价于<aop:aspect>public class HandlerException {//定义异常通知,等价于<aop:after-throwing>@AfterThrowing(throwing="ex",pointcut="within(org.tedu.cloudnote.controller..*)")public void execute(Exception ex){//将异常信息记录文件,ex就是目标抛出的异常对象try{FileWriter fw = new FileWriter("cloud_note.log",true);PrintWriter out = new PrintWriter(fw);//利用Out打印一个头部//****************************************//*时间:2016-03-22 11:04:23 * //*类型:java.lang.NullPointerException *//****************************************//将异常栈信息写入cloud_note.log文件ex.printStackTrace(out);out.close();fw.close();}catch(Exception e){System.out.println("记录异常信息失败");}}}
0 0
- Spring AOP的应用
- Spring AOP 应用模型
- Spring Aop的应用
- spring aop应用场景
- Spring AOP 的应用
- Spring---AOP应用
- Spring AOP应用
- Spring的AOP应用
- spring aop 应用场景
- Spring的AOP应用实例
- Spring Aop应用一例
- Spring的AOP应用实例
- spring实例应用(AOP)
- Spring AOP配置与应用
- Spring AOP应用实例demo
- Spring Aop 日志拦截应用
- Spring AOP配置与应用
- Spring AOP实际开发应用
- java判断字段是否final修饰
- 什么是SVG和SVG常用的形状
- hdu【1520】Anniversary party
- Java 对Map进行排序的实现方法
- c++函数参数类型-引用
- Spring AOP应用
- iOS网络开发中的同步、异步和请求队列
- win32平台而非linux平台下 深入分析Sleep(0)与Sleep(1)的区别
- android项目中,java调用kotlin代码
- CAP原则
- 第五周上机实践项目——项目1-三角形类雏形-(2)
- linux挂载的磁盘类型
- Excel启动提示向程序发送命令出现问题
- wireShark的使用