spring4 - 8 -重用表达式 - 切面优先级 - 基于配置文件的方式配置AOP


//使用@Order 注解指定切面的优先级, 值越小优先级越高
@Order(1)
@Aspect
@Component
public class vliDataAspect {
	
	/*
	 * 定义一个方法,用于声明切入点表达式,一般的,该方法不再需要填入其他代码
	 */
	@Pointcut("execution(public int spring.aop.impl.ArithmeticCalculator.*(..))")
	public void loggingAspectJoinPointExpression() {}
	
	/*
	 * 	再需要使用该表达式的地方加入方法即可实现表达式的复用
	 * 	在其他文件中使用该表达式需要类名.方法,不同包下还要加上包名
	 *	value = "vliDataAspect.loggingAspectJoinPointExpression()
	 */
	@Before("loggingAspectJoinPointExpression()")
	public void validataArgs(JoinPoint joinpoint) {
		System.out.println("valiData: " + joinpoint);
	}
}

AOP

(loggingAspect.java)·······················································(写入需要的方法即可,不需要注解)
package spring.aop.xml;
import java.util.Arrays;
import java.util.List;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

public class LoggingAspect {
	
	public void beforeMethod(JoinPoint joinpoint) {
		String methodName = joinpoint.getSignature().getName();
		List<Object> args = Arrays.asList(joinpoint.getArgs());
		System.out.println("the method "+ methodName +" begin..." + args);
	}
	public void afterMethod(JoinPoint joinpoint) {
		String methodName = joinpoint.getSignature().getName();
		List<Object> args = Arrays.asList(joinpoint.getArgs());
		System.out.println("the method "+ methodName +" end with ..." + args);
	}
	public void afterReturning(JoinPoint joinpoint, Object result) {
		String methodName = joinpoint.getSignature().getName();
		List<Object> args = Arrays.asList(joinpoint.getArgs());
		System.out.println("the method "+ methodName+ " run with" +args+ "return with" + result);		
	}
	
	public void afterThrowing(JoinPoint joinpoint, Exception e) {
		String methodName = joinpoint.getSignature().getName();
		System.out.println("the method" + methodName+ "occurs exception: with" + e);
	}
	
	public Object aroundMethod(ProceedingJoinPoint pjd) throws Throwable {
		Object result = null;
		
		//执行目标方法
		try {
			//前置通知
			System.out.println("the message in around method--before main method runing");
			result = pjd.proceed();
			//返回通知
			System.out.println("the message in around method--after main method run");
		}catch(Exception e){
			//异常通知
			System.out.println("the occurs exception in around method : " + e);
		}
		
		//后置通知。。。
		return result;
	}
}

(vliDataAspect.jave)···························································
package spring.aop.xml;
import org.aspectj.lang.JoinPoint;

public class vliDataAspect {
	
	public void loggingAspectJoinPointExpression() {}
	
	public void validataArgs(JoinPoint joinpoint) {
		System.out.println("valiData: " + joinpoint);
	}
}

 applicationContext-xml.xml)····························································
	<!-- 配置bean -->
	<bean id = "arithmeticCalculatorimpl" class = "spring.aop.xml.ArithmeticCalculatorlmp"></bean>
	
	<!-- 配置切面的bean -->
	<bean id = "loggingAspect" class = "spring.aop.xml.LoggingAspect"></bean>
	<bean id = "vliDataAspect" class = "spring.aop.xml.vliDataAspect"></bean>	
	
	<!-- 配置 AOP -->
	<aop:config>
		<!-- 配置切点表达式 -->
		<aop:pointcut expression="execution(public int spring.aop.xml.ArithmeticCalculator.*(..))" id="pointcut"/>
		
		<!-- 配置切面及通知 -->
		<aop:aspect ref="loggingAspect" order="2">
			<!-- 前置通知 -->
			<aop:before method="beforeMethod" pointcut-ref="pointcut"/>
			<!-- 后置通知 -->
			<aop:after method="afterMethod" pointcut-ref="pointcut"/>
		</aop:aspect>
		
		<aop:aspect ref = "vliDataAspect" order = "1">
			<aop:before method="validataArgs" pointcut-ref="pointcut"/>
		</aop:aspect>
	</aop:config>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值