1、使用SpringAOP完成简单的程序
1.导入SpringAOP所需jar包
2.编写spring的配置文件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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.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 " xmlns:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" > <!-- configure the package for spring to scan --> <context:component-scan base-package="test.Spring.AOP" /> <!-- make the aspectj annotation to be used --> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> </beans>
3.创建一个HelloWord接口以及它的实现类HelloWordImpl
public interface HelloWord { public int sayHello(int num); }
@Component public class HelloWordImpl implements HelloWord{ public int sayHello(int num){ System.out.println("hello word"); return 100/num; } }
4.SpringAOP注释的类型有5种
@Before 前置通知 在方法执行前执行
@After 后置通知 在方法执行后执行
@AfterThrowing 异常通知 在方法抛出异常之后执行
@AfterReturning 返回通知 在方法返回结果之后执行
@Around 环绕通知 环绕着方法执行
5.创建一个切面类(包含@Before @After @AfterThrowing @AfterReturning)
@Component @Aspect public class HelloWordAspect { @Before(value="execution(* test.Spring.AOP.HelloWord.sayHello(..))") public void beforeMethod(JoinPoint jp){ String methodName = jp.getSignature().getName(); System.out.println(methodName); System.out.println("before method execute,args are "+Arrays.toString(jp.getArgs())); } @After("execution(* test.Spring.AOP.HelloWord.sayHello(..))") public void afterMethod(JoinPoint jp){ System.out.println("after method execute,args are "+Arrays.toString(jp.getArgs())); } @AfterThrowing(value="execution(* test.Spring.AOP.HelloWord.sayHello(..))",throwing="ex") public void afterThrow(Exception ex){ System.out.println("afterThrow"+ex.getMessage()); } @AfterReturning(value="execution(* test.Spring.AOP.HelloWord.sayHello(..))",returning="result") public void afterReturn(Object result){ System.out.println("the result is "+result); } }
6.在主函数调用
public class Main { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContextForAOP.xml"); HelloWord hw = (HelloWord) context.getBean("helloWordImpl"); hw.sayHello(10); } }
7.调用结果
结果说明,在sayHello方法是被Spring代理执行了,执行前后加上了一些切面类中定义的信息。
8.使用Around环绕通知切面类实现类似效果
@Component @Aspect public class HelloWordAspectAround { @Around(value="execution(* test.Spring.AOP.HelloWord.sayHello(..)))") public Object aroundMethod(ProceedingJoinPoint pjp){ Object result = null; String methodName = pjp.getSignature().getName(); try { result = pjp.proceed(); System.out.println("the result is "+result); } catch (Throwable e) { System.out.println("Exception occurs : "+e.getMessage()); throw new RuntimeException(e); } System.out.println(methodName+" end"); return result; } }