AOP used in the Spring Framework
There are two ways in which we can complete the first use manner annotated for ease of understanding
Step 1 is added a package dependent Jar
2 Step Two Create a class section
@Aspect //注解 @Controller public class ArithmaticCalculateaop { //前置通知@Before(value="execution( * com.zhiyou100.aop.*.*(..))") public void before(JoinPoint JoinPoint) { Object[] args = JoinPoint.getArgs(); String name = JoinPoint.getSignature().getName(); System.out.println("===com.zhiyou100===="+name+"======start===result"+Arrays.asList(args)); } //后置通知@After(value = "execution( * com.zhiyou100.aop.*.*(..))") 定义表达式 public void after(JoinPoint JoinPoint) { String name = JoinPoint.getSignature().getName(); System.out.println("===com.zhiyou100===="+name+"======end===result"); // returns notice } @AfterReturning(value = "execution( * com.zhiyou100.aop.*.*(..))",returning="result") public void cc(Object result) { System.out.println("============="+result); } @AfterThrowing(value = "execution( * com.zhiyou100.aop.*.*(..))",throwing="e") public void dd(Exception e) { System.out.println("异常了"); } }
3 Step Three open comment section in the Spring configuration file
<- - Open Pack scan!> <Context: Component Base-Package-Scan = "com.zhiyou100.aop" /> <-! Cut open annotation -> <AOP: AspectJ-the autoproxy />
Xml-based manner (annotations can be removed)
<! - definition of a class program is notified -> <the bean ID = "ASD" class = "com.zhiyou100.aop.ArithmaticCalculateImp" /> <! - the bean class definitions section -> <= the bean class "com.zhiyou100.aop.ArithmaticCalculateaop" ID = "QWE"> </ the bean> <- profile XML ->! <AOP: config> <- expression defined cut-off point ->! <AOP: the pointcut = expression The "Execution (.. com.zhiyou100.aop * * * (..))" ID = "ZXC" /> ! <- defined aspects -> <AOP: Aspect REF = "QWE"> <-! - pre-defined notifications -> <AOP: before Method = "before" the pointcut-REF = "ZXC" /> <AOP: After Method = "After"pointcut-ref="zxc"/> <aop:after-returning method="cc" pointcut-ref="zxc" returning="result"/> </aop:aspect> </aop:config>