Spring AOP 与 Solon AOP 有什么区别?

Spring 和 Solon 作为容器型框架。都具有 IOC 和 AOP 的能力。其中:

  • Spring AOP 使用表达式确定"切入点",可以是某个注解(有侵入),可以是包名或类名或方法(无侵入)
  • Solon AOP 只使用某个注解确定"切入点"(有侵入)

先看两个示例

1、Spring AOP 示例

Spring AOP 有很多不同的能力构建方式。此处采用更简洁的一种方式:

java 复制代码
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LoggingAspect {
    @Pointcut("execution(* com.example.demo.service.*.*(..))") //也可以是某注解表达式
    public void serviceLayer() {}

    @Around("serviceLayer()")
    public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("test");
        return joinPoint.proceed();
    }
}

应用示例

java 复制代码
package com.example.demo.service;

@Component
public class UserService {
    public String getUserById(Long id) {
        return "user-" + id;
    }

    public void updateUser(String user) {
        System.out.println("update: " + user);
    }
}

2、Solon AOP 示例

Solon AOP 有两种能力构建方式。此处采用更简洁的一种方式:

java 复制代码
import org.noear.solon.annotation.Around;
import org.noear.solon.core.aspect.Invocation;
import org.noear.solon.core.aspect.MethodInterceptor;

@Around(Logging.LoggingInterceptor.class) //为注解,附加包围处理的能力
@Target({ElementType.TYPE,  ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Logging {

    class LoggingInterceptor implements MethodInterceptor {
        @Override
        public Object doIntercept(Invocation i) throws Throwable {
            System.out.println("test");
            return i.invoke();
        }
    }
}

应用示例

java 复制代码
package com.example.demo.service;

@Logging
@Component
public class UserService {
    public String getUserById(Long id) {
        return "user-" + id;
    }

    public void updateUser(String user) {
        System.out.println("update: " + user);
    }
}

3、总结

体验感受 Spring AOP Solon AOP
有侵入体验 通过表达式描述,使用时添加"注解" 定义注解,使用时添加"注解"
无侵入体验 通过表达式描述包名或类名或方法,使用时无感 /
优点 可以完全"无侵入"实现 AOP 附加了什么能力比较透明
缺点 表达式有点难写;(可无限制添加)可能会有些混乱 (不能随意添加)可能会有局限性
相关推荐
Traving Yu1 分钟前
Spring源码与框架原理
java·后端·spring
Lyyaoo.6 分钟前
【JAVA基础面经】线程安全的单例模式
java·安全·单例模式
_李小白10 分钟前
【OSG学习笔记】Day 39: NodeCallback(帧回调机制)
java·笔记·学习
如来神掌十八式12 分钟前
设计模式之装饰器模式
java·设计模式
cch891819 分钟前
C++、Python与汇编语言终极对比
java·开发语言·jvm
tsyjjOvO23 分钟前
【Spring Data Redis 从入门到实战】一站式掌握 Redis 操作与封装
redis·spring
好家伙VCC30 分钟前
**InfluxDB实战进阶:基于Golang的高性能时序数据采集与可视化方
java·开发语言·后端·python·golang
斌味代码31 分钟前
Java SpringBoot 微服务实战:企业级架构设计与性能调优完全指南
java·spring boot·微服务
好家伙VCC32 分钟前
**发散创新:基于Go语言的服务网格实践与流量治理实战**在微服务架构日益复杂的今天,**服务网格(S
java·python·微服务·架构·golang
一定要AK9 小时前
Spring 入门核心笔记
java·笔记·spring