一、实现接口并重写方法

实现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. gin框架对接快递100 查询快递跟踪记录 Golang实现快递查询

    参考ui效果: https://www.kuaidi100.com/?from=openv gin框架: 请求地址 http://localhost:8822/kd100/auto_com_num?n ...

  2. 记录一次安装PIDtoolBox报缺少jvm.dll问题。

    背景: 1.在安装PIDtoolBox时,报 安装程序错误 安装程序无法启动JVM. could not find file C:\Users\AdministratorAppData\Local\M ...

  3. openstack虚拟机用keep alive添加的VIP,其它机器无法访问

    neutron port-list |grep ipneutron port-update a7fbxxf6cc2 --allowed_address_pairs type=dict list=tru ...

  4. redis 的下载与安装

    下载地址:https://github.com/MicrosoftArchive/redis/releases 选择免安装包: 解压到路径  D:\Redis-x64-3.0.504 用管理员权限打开 ...

  5. 三:nacos的配置中心

    配置中心的使用: 编辑当前项目的pom.xml,加入必要的依赖配置 <!-- spring-cloud-alibaba-dependencies 依赖同注册中心 --> <depen ...

  6. 分享一个Byte KB MB GB 单位转换方法 从《C#本质论第三版》

    static public string FormatBytes(long bytes) { string[] magnitudes = new string[] { "GB", ...

  7. P1683 入门

    传送锚点:https://www.luogu.com.cn/problem/P1683 题目描述 不是任何人都可以进入桃花岛的,黄药师最讨厌像郭靖一样呆头呆脑的人.所以,他在桃花岛的唯一入口处修了一条 ...

  8. 容器化tomcat9.0

    #启动tomcat容器: docker run -d --name tomcat9.0 -p 8080:8080 registry.cn-hangzhou.aliyuncs.com/chenleile ...

  9. 算法金 | Python 中有没有所谓的 main 函数?为什么?

    ​大侠幸会,在下全网同名[算法金] 0 基础转 AI 上岸,多个算法赛 Top [日更万日,让更多人享受智能乐趣] 定义和背景 在讨论Python为何没有像C或Java那样的明确的main函数之前,让 ...

  10. Nacos 版本不一致报错: Request nacos server failed

    在做微服务开发中,测试环境使用Nacos没有问题,但是生产环境服务启动一直报错: com.alibaba.nacos.api.exception.NacosException: Request nac ...