(二十六)利用Annotation配置AOP

        之前学习的AOP操作如果在以后的开发之中基本都是固定的,但是如果是你自己写的代码,如果通过配置文件编写就太麻烦了,所以可以使用基于Annocation的配置完成,

范例:打开AOP的Annotation支持

<?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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
						http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
						http://www.springframework.org/schema/context 
						http://www.springframework.org/schema/context/spring-context-4.1.xsd
						http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd">
	<context:annotation-config></context:annotation-config>
	<context:component-scan base-package="cn.zwb"/>
	<!--<aop:config proxy-target-class="true">
	 首先定义程序的切入点 
		<aop:pointcut expression="execution(* cn.zwb..*.*(..)) and args(abc)" id="pointcut"/>
		定义要使用的面向切面的处理类 
		<aop:aspect ref="serviceAspect">
			<aop:before method="serviceBefore2" pointcut-ref="pointcut" arg-names="abc"/>
			<aop:after method="serviceAfter" pointcut="execution(* cn.zwb..*.*(..))"/>
			<aop:after-returning method="serviceAfterReturning" pointcut="execution(* cn.zwb..*.*(..))" arg-names="abc" returning="abc"/>
			<aop:after-throwing method="AfterThrowing" pointcut="execution(* cn.zwb..*.*(..))" arg-names="aa" throwing="aa"/>
			<aop:around method="serviceAroud" pointcut="execution(* cn.zwb..*.*(..))"/>
		</aop:aspect>
	</aop:config>-->
	<aop:aspectj-autoproxy/>
</beans>

        那么随后的事情就是在ServiceAspect类中编写所需要使用的Annotation.

范例:修改ServiceAspect程序类

package cn.zwb.aop;

import java.util.Arrays;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

import cn.zwb.vo.Member;

@Component
@Aspect
public class ServiceAspect {
	@Before(value="execution(* cn.zwb..*.*(..))")
	public void serviceBefore(){
		System.out.println("AO切面  执行日志记录操作");
	}
	@Before(value="execution(* cn.zwb..*.*(..)) and args(param)",argNames="param")
	public void serviceBefore2(Object vo){
		System.out.println("AO切面  执行增加前的一个操作.参数="+vo);
	}
	@After(value="execution(* cn.zwb..*.*(..))")
	public void serviceAfter(){
		System.out.println("AOP切面  执行事务处理操作");
	}
	@AfterReturning(value="execution(* cn.zwb..*.*(..))",argNames="ret",returning="ret")
	public void serviceAfterReturning(Object val){
		System.out.println("AOP切面  操作完成返回结果:"+val);
	}
	@org.aspectj.lang.annotation.AfterThrowing(value="execution(* cn.zwb..*.*(..))",argNames="e",throwing="e")
	public void AfterThrowing(Exception ex){
		System.out.println("AOP切面  操作出现异常:"+ex);
	}
	@Around("execution(* cn.zwb..*.*(..))")
	public Object serviceAroud(ProceedingJoinPoint point) throws Throwable{
		System.out.println("AOP切面  数据层方法调用之前,参数:"+Arrays.toString(point.getArgs()));
		Member vo=new Member();
		vo.setMid("啊大叔说的");
		vo.setName("非绯闻绯闻");
		Object retval=point.proceed(new Object[]{vo});//调用具体的真实操作
		System.out.println("AOP切面  数据层方法调用之后,返回值:"+retval);
		return true;  //可以自己修改返回值
	}
}

        如果在实际的开发之中需要进行一些辅助功能编写的时候,建议使用Annotation的配置操作,这样的代码是最简化的也是最直观的.


猜你喜欢

转载自blog.csdn.net/qq1019648709/article/details/80518660