Spring AOP demo
基于注解与 XML 配置文件两种形式的 AOP demo。 基于 xml 配置文件的 aop 管理 ```xml <!-- 配置切面的bean --> <bean id="loggingAspect" class="com.jas.aop.xml.LoggingAspect"/> <aop:config> <!-- 配置切点表达式 --> <aop:pointcut id="pointcut1" expression="execution(public void com.jas.aop.bean.PersonImpl.sayHello())"/> <aop:pointcut id="pointcut2" expression="execution(public void com.jas.aop.bean.PersonImpl.sayBye(String))"/> <!-- 配置切面 --> <aop:aspect ref="loggingAspect"> <!-- 配置通知 --> <aop:before method="beforeMethod" pointcut-ref="pointcut1"/> <aop:around method="aroundMethod" pointcut-ref="pointcut2"/> </aop:aspect> </aop:config> ``` Java 类 ```java public class LoggingAspect { public void beforeMethod(JoinPoint joinPoint) { System.out.println("在目标方法执行之前执行" + ", 要拦截的方法是:" + joinPoint.getSignature()); } public Object aroundMethod(ProceedingJoinPoint joinPoint) throws Throwable { Object[] args = joinPoint.getArgs(); // 判断目标方法参数,满足条件修改参数值 if(" See You Again".equals(args[0])) { args[0] = " See You Again ..."; } // 在目标方法执行之前执行,相当于前置通知 System.out.println("这是一个前置通知"); // 执行目标方法 Object result = joinPoint.proceed(args); // 在目标方法执行之后之后,相当于后置通知 System.out.println("这是一个后置通知"); return result; } }
- 1
- 粉丝: 4
- 资源: 48
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助