一、实现接口并重写方法

实现org.aopalliance.intercept.MethodInterceptor接口,这是AOP Alliance规范中的接口,Spring AOP支持它。这种方式比较适合需要非常细粒度控制的场景。

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation; public class LoggingInterceptor implements MethodInterceptor { @Override
public Object invoke(MethodInvocation invocation) throws Throwable {
System.out.println("Before method: " + invocation.getMethod().getName());
try {
Object result = invocation.proceed(); // 继续执行被拦截的方法
return result;
} finally {
System.out.println("After method: " + invocation.getMethod().getName());
}
}
}

在spring配置中,将此拦截器注册到切面中

<aop:config>
<aop:aspect id="loggingAspect" ref="loggingInterceptor">
<aop:pointcut id="serviceMethods" expression="execution(* com.example.service.*.*(..))"/>
<aop:around method="invoke" pointcut-ref="serviceMethods"/>
</aop:aspect>
</aop:config> <bean id="loggingInterceptor" class="com.example.aspect.LoggingInterceptor"/>

二、通过注解方式(以环绕通知为例)

@Aspect
@Component
public class LoggingAspect { @Around("execution(* com.example.service.*.*(..))")
public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("Before method: " + joinPoint.getSignature().getName());
try {
Object result = joinPoint.proceed();
return result;
} finally {
System.out.println("After method: " + joinPoint.getSignature().getName());
}
}
}

aop的两种配置方法的更多相关文章

  1. visualvm远程监控jvm两种配置方法

    参考:http://blog.itpub.net/17203031/viewspace-765810 一.Jstatd RMI远程监控方法 VisualVM在监控本地JVM的时候是很方便的.只要应用程 ...

  2. (一)spring aop的两种配置方式。

    sring aop的方式有两种:(1)xml文件配置方式(2)注解的方式实现,我们可以先通过一个demo认识spring aop的实现,然后再对其进行详细的解释. 一.基于注解的springAop配置 ...

  3. servlet两种配置方法详解

     1.web.xml中Servlet的注解 <servlet> <!-- servlet的内部名称,自定义 --> <servlet-name>DemoAction ...

  4. spring AOP的两种配置方式

    连接点(JoinPoint) ,就是spring允许你是通知(Advice)的地方,那可就真多了,基本每个方法的前.后(两者都有也行),或抛出异常是时都可以是连接点,spring只支持方法连接点.其他 ...

  5. spring AOP的两种配置

    xml配置 定义要被代理的方法的接口 public interface TestAop { public void print(String s); } 实现上述接口 public class Tes ...

  6. java:comp/env/jdbc/ 的两种配置方法

    1. 在 META-INF 下建立文件: context.xml <?xml version="1.0" encoding="UTF-8"?> &l ...

  7. TestLink学习三:发送邮件的两种配置方法

    第一种:修改config.inc.php中的[smtp],配置为默认本地发送,用hotmail用户做接收,调试成功!(本人未尝试这种) // ----------------------------- ...

  8. springmvc两种配置方法

    基于配置文件xml方式, 配置springmvc步骤: 1.在pom文件中引入jar包: <!--导入springmvc的jar包--> <dependency> <gr ...

  9. 学习JavaWeb aop两种配置方式

    aop aop:面向切面编程,它可以解决重复代码. aop有两种方式: 一..xml方式 1.在springmvc-servlet.xml中配置aop,应用bean文件: <!--aop配置-- ...

  10. springmvc配置AOP的两种方式

    spingmvc配置AOP有两种方式,一种是利用注解的方式配置,另一种是XML配置实现. 应用注解的方式配置: 先在maven中引入AOP用到的依赖 <dependency> <gr ...

随机推荐

  1. cuBlas API Launch Latency 耗时异常分析记录

    一.背景 最近在做 AI 编译器生成 Kernel 支持 Bert 模型训练调优工作,在分析 bert 的timeline中发现,在每个 step 的前两个 cinn_instruction_run ...

  2. ansible(8)--ansible的hostname模块

    1. hostname模块 功能:管理远程主机的主机名. 示例一:更改192.168.20.22的主机名为nginx01: [root@xuzhichao ~]# ansible 192.168.20 ...

  3. python教程8-页面爬虫

    python爬虫常用requests和beautifulSoup这2个第三方模块.需要先进行手动安装. requests负责下载页面数据,beautifulSoup负责解析页面标签. 关于beauti ...

  4. leaflet 使用kriging.js实现前端自定义插值

    1.GitHub地址:https://github.com/oeo4b/kriging.js 2.核心代码 var variogram = kriging.train(t, x, y, model, ...

  5. Vue-Plugin-HiPrint

    Vue-Plugin-HiPrint 是一个Vue.js的插件,旨在提供一个简单而强大的打印解决方案.通过 Vue-Plugin-HiPrint,您可以轻松地在Vue.js应用程序中实现高度定制的打印 ...

  6. width:100%与width:auto区别

    小知识 width:100%与width:auto区别 width:100% : 子元素的 content 撑满父元素的content,如果子元素还有 padding.border等属性,或者是在父元 ...

  7. 在uGUI正交相机中实现旋转透视效果

    正常uGUI使用正交相机的话,旋转是没有透视效果的,但如果能实现较简单的透视, 对一些效果表现来说还是不错的:见下图(左为透视效果): 正常思路感觉各种麻烦. 因为uGUI使用unity的x和y方向表 ...

  8. C语言:生成单词列表----使用单链表实现

    解决之前用结构体数组导致内存过剩问题,使用动态分配内存优化单词列表. txt文本内容不允许出现其他字符形式,这个仅限于判断在txt网页文件已经删除了超链接等,文本里面只允许出现单词才能进行判断和进行单 ...

  9. redis持久化存储数据(rdb和aof)

    rdb持久化存储数据 总的 redis持久化 防止数据丢失,持久化到本地,以文件形式保存 持久化的方式 ,两种 aof和 rdb模式 1.触发机制, - 手动执行save命令 - 或者配置触发条件 s ...

  10. 研二学妹面试字节,竟倒在了ThreadLocal上,这是不要应届生还是不要女生啊?

    一.写在开头     今天和一个之前研二的学妹聊天,聊及她上周面试字节的情况,着实感受到了Java后端现在找工作的压力啊,记得在18,19年的时候,研究生计算机专业的学生,背背八股文找个Java开发工 ...