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 附加了什么能力比较透明
缺点 表达式有点难写;(可无限制添加)可能会有些混乱 (不能随意添加)可能会有局限性
相关推荐
z晨晨8 小时前
互联网大厂Java求职面试实战:Spring Boot与微服务场景深度解析
java·spring boot·redis·微服务·kafka·spring security·电商
练习时长一年8 小时前
Bean后处理器
java·服务器·前端
野犬寒鸦8 小时前
从零起步学习Redis || 第五章:利用Redis构造分布式全局唯一ID
java·服务器·数据库·redis·分布式·后端·缓存
吹晚风吧8 小时前
SSE是什么?SSE解决什么问题?在什么场景使用SSE?
java·springboot·sse
Terio_my9 小时前
IDEA自动构建与热部署配置
java·ide·intellij-idea
数智顾问9 小时前
Java坐标转换的多元实现路径:在线调用、百度与高德地图API集成及纯Java代码实现——纯Java代码实现与数学模型深度剖析
java·开发语言
武子康9 小时前
Java-138 深入浅出 MySQL Spring Boot 事务传播机制全解析:从 REQUIRED 到 NESTED 的实战详解 传播机制原理
java·大数据·数据库·spring boot·sql·mysql·事务
码神本神10 小时前
【附源码】基于Spring Boot的高校爱心捐助平台的设计与实现
java
真的想不出名儿10 小时前
登录前验证码校验实现
java·前端·python
珹洺10 小时前
Java-Spring入门指南(十九)thymeleaf基本概念
java·spring·状态模式