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