spring AOP——通过配置文件实现

目录

说明

本例子不是web项目。

配置applicationContext.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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx.xsd">

	<!-- 切面类bean -->
	<bean id="aspect" class="text3_AOP_text3.MyAspect"></bean>

	<!-- 目标对象 -->
	<bean id="studenttarget" class="text3_AOP_text3.StudentImpl"></bean>

	<!-- 定义AOP切面,切入点,通知 -->
	<aop:config>
		<aop:aspect id="aspectAOP" ref="aspect">
			<!-- 切点 -->
			<aop:pointcut id="piontcut" expression="execution(* text3_AOP_text3.StudentImpl.*(..))"/>
			<!--  使用逻辑运算符and,限定参数是注解-->
<!--      	expression="execution(* text3_AOP_text3.*.*(..)) and -->
<!--         						@annotation(text3_AOP_text3.MyAnnotation)" /> -->
			<!-- 前置通知方法,引入切点 -->
			<aop:before method="before" pointcut-ref="piontcut"/>
			<!-- 环绕通知方法 -->
			<aop:around method="around" pointcut-ref="piontcut"/>
			<!-- 返回后通知方法 -->
			<aop:after-returning method="afterReturning" pointcut-ref="piontcut" returning="re"/>
			<!-- 异常通知方法 -->
			<aop:after-throwing method="afterThrowing" pointcut-ref="piontcut" throwing="e"/>
			<!-- 后通知通知方法 -->
			<aop:after method="after" pointcut-ref="piontcut"/>
			
			<!-- 环绕通知带参数的方法 -->
			<aop:around method="aroundadd"
				pointcut="execution(* text3_AOP_text3.StudentImpl.addStudent(String))
							and args(name)" />

			<!-- 转向操作,访问匹配 text3_AOP_text3.People.*(+)的方法,转向执行实了FBI接口的类FBIImpl -->
			<aop:declare-parents types-matching="text3_AOP_text3.People.*(+)"
				implement-interface="text3_AOP_text3.FBI" default-impl="text3_AOP_text3.FBIImpl"/>
		</aop:aspect>
	</aop:config>
</beans>


说明:
匹配的目标对象都将通过切面类,在一个切面类中定义各种类型的通知
注意有五种通知+一个转向操作

切面类:MyAspect.java

切面类:MyAspect.java,代码片.

package text3_AOP_text3;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
public class MyAspect {

	// 前置通知方法
	public void before(JoinPoint joinPoint) {
		System.out.println("前置通知---方法名: "
							+joinPoint.getSignature().getName());
	}

	// 返回后通知方法
	public void afterReturning(JoinPoint joinPoint, Object re) {
		System.out.println("后置通知---方法名: "
							+joinPoint.getSignature().getName()
							+", 返回参数re: " + re);
	}

	// 异常通知方法
	public void afterThrowing(JoinPoint joinPoint, Throwable e) {
		System.out.println("异常通知: " 
							+ "出现异常的方法名: "+joinPoint.getSignature().getName() 
							+ ", 异常信息e: " + e.getMessage());
		System.exit(0);
	}

	// 后通知方法
	public void after(JoinPoint joinPoint) {
		System.out.println("后通知---方法名: "
							+joinPoint.getSignature().getName());
	}

	// 环绕通知方法
	public Object around(ProceedingJoinPoint joinPoint) {
		Object re = null;
		try {
			System.out.println("环绕通知前");
			re = joinPoint.proceed();
			System.out.println("环绕通知后");
		} catch (Throwable e) {
			e.printStackTrace();
		}
		return re;
	}

	// 有参数的环绕通知方法,执行的上下文ProceedingJoinPoint
	public Object aroundInit(ProceedingJoinPoint joinPoint, String name) {
		System.out.println("姓名:" + name);
		Object re = null;
		try {
			System.out.println("环绕通知前");
			re = joinPoint.proceed();
			System.out.println("环绕通知后");
		} catch (Throwable e) {
			e.printStackTrace();
		}
		return re;
	}

}

说明:

--------------------------------------------------------------------------------------------------------------------------------------------
	 * 了解一下spring aop的两个接口的主要方法:  
	 * 1)JoinPoint 
	 * 		java.lang.Object[] getArgs():获取连接点方法的参数集合 
	 * 		Signature getSignature() :获取连接点的方法对象;
	 * 		java.lang.Object getTarget() :获取连接点所在类的目标对象; 
	 * 		java.lang.Object getThis():获取切面对象本身;  
	 * 2)ProceedingJoinPoint 
	 * 	ProceedingJoinPoint继承JoinPoint子接口,新增了两个用于执行连接点方法的方法:
	 * 		java.lang.Object proceed() throws java.lang.Throwable:通过反射执行目标对象的连接点处的方法;
	 * 		java.lang.Object proceed(java.lang.Object[] args) throws java.lang.Throwable:通过反射执行目标对象连接点处的方法,不过使用新的入参替换原来的入参。 
	 * 

总结

	1. 使用配置的方式来实现AOP相对于API方式更加方便,更易于管理,用匹配的的方式,可以对一类目标对象进行管理。
**引用包是最大的困难(一步一步来)**
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值