书接上回
上回说道LEO盗梦,宿主抛出了Exception,LEO早有应对,逃脱了。
LEO审视自己的类,发现所有的盗梦行为中,都需要在睡梦之前做准备@before,梦醒之后逃脱@after。那么是不是可以把这两个方法合一呢。
这里我们用到@Around,这个代码貌似应该这么写
伪代码
before应该做的事情
宿主的业务
after应该做的事情
我们知道在编织waving的时候,我们用了@before和@after来表示方法在宿主运行之前还是之后。使用Spring是没有显式调用的,也就是说我们不能直接调用业务方法。这里我们用到的是ProceedingJoinPoint。
这里稍微先说说JointPoint,我不习惯大段列举名词的介绍方法,我们的原则是用到什么讲什么。
JointPoint意思是连接点,就是业务代码和Aspect代码的交汇点。在电影里,就是你做你的梦,LEO跑到你的梦里的特定方法中。一般来说连接点主要是只宿主的方法。
剩下的就好办了,ProceedingJointPoint就是指被切面切入的方法。
这里直接上代码
1、LEOIncept
package com.spring.aop;
import java.lang.reflect.Method;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class LeoIncept{
@Pointcut("execution(public void com.spring.service.Person.haveSleep())")
public void myPointcut(){}
@Around("myPointcut()")
public void stealPassword(ProceedingJoinPoint pjp) throws Throwable {
System.out.println(this.getClass().getName());
System.out.println("快要睡觉了");
pjp.proceed();
System.out.println(this.getClass().getName());
System.out.println("醒了,撤退");
}
}
与before和after不同的是,首先我要声明一个pointcut叫做myPointcut方法,myPointcut()方法里面是不需要写代码的,写了也不会执行,Spring只是用到了myPointcut()方法的方法名而已。之后的@Around是利用了myPointcut()的名字,表示切入点@Pointcut("execution(public void com.spring.service.Person.haveSleep())")这里。
pjp.proceed()是用于表示业务代码,在这里就是haveSleep()方法。
2、修改Person方法
package com.spring.service;
import org.springframework.stereotype.Component;
@Component
public class Person {
public void haveSleep()
{
System.out.println(this.getClass().getName());
System.out.println("睡觉了");
}
}
3、测试代码
package com.spring.service.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.service.Person;
public class PersonTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext ctx = new ClassPathXmlApplicationContext("springbeans.xml");
Person person = (Person) ctx.getBean("person");
person.haveSleep();
}
}
测试结果
com.spring.aop.LeoIncept
快要睡觉了
com.spring.service.Person
睡觉了
com.spring.aop.LeoIncept
醒了,撤退
分享到:
相关推荐
NULL 博文链接:https://garrincha.iteye.com/blog/2111779
NULL 博文链接:https://garrincha.iteye.com/blog/2111957
死磕Spring之AOP篇 - Spring AOP两种代理对象的拦截处理(csdn)————程序
spring aop spring aop spring aop spring aop spring aop spring aop spring aop spring aop spring aop
spring aop jar 包
spring-aop-1.1.1.jar spring-aop-1.2.6.jar spring-aop-1.2.9.jar spring-aop-2.0.2.jar spring-aop-2.0.6.jar spring-aop-2.0.7.jar spring-aop-2.0.8.jar spring-aop-2.0.jar spring-aop-2.5.1.jar spring-aop-...
描述一下Spring AOP? 在Spring AOP中关注点(concern)和横切关注点(cross-cutting concern)有什么不同? AOP有哪些可用的实现? Spring中有哪些不同的通知类型(advice types)? Spring AOP 代理是什么? 引介...
基于注解实现SpringAop基于注解实现SpringAop基于注解实现SpringAop
一、适合人群 1、具备一定Java编程基础,初级开发者 2、对springboot,mybatis,mysql有基本认识 3、对spring aop认识模糊的,不清楚如何实现Java 自定义注解的 ...4、spring boot,mybatis,druid,spring aop的使用
Spring框架的关键组件之一是面向方面编程(AOP)框架。 面向方面的编程需要将程序逻辑分解成不同的部分。 此教程将通过简单实用的方法来学习Spring框架提供的AOP/面向方面编程。
spring aop的demo spring aop的demo
springaop依赖的jar包,spring版本2.5.6,如果需要,可以下载使用,欢迎各位评论指出不足
开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE...
Spring aop Spring aop
spring aop
Spring AOP 几个不同使用方法的完整例子,使用Junit4c测试, 在我的博客上有不同配置组合的说明,可以参考
spring之AOP(动态代理),包括jdk动态代理和CGLib动态代理
springAOP详解