Spring AOP应用

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.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		http://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("记录异常信息失败");
		}
	}
	
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值