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 附加了什么能力比较透明
缺点 表达式有点难写;(可无限制添加)可能会有些混乱 (不能随意添加)可能会有局限性
相关推荐
一路向北⁢2 分钟前
基于 Apache POI 5.2.5 构建高效 Excel 工具类:从零到生产级实践
java·apache·excel·apache poi·easy-excel·fast-excel
毕设源码-赖学姐3 小时前
【开题答辩全过程】以 基于Android的校园快递互助APP为例,包含答辩的问题和答案
java·eclipse
damo013 小时前
stripe 支付对接
java·stripe
麦麦鸡腿堡4 小时前
Java的单例设计模式-饿汉式
java·开发语言·设计模式
假客套4 小时前
Request method ‘POST‘ not supported,问题分析和解决
java
傻童:CPU4 小时前
C语言需要掌握的基础知识点之前缀和
java·c语言·算法
爱吃山竹的大肚肚4 小时前
@Valid校验 -(Spring 默认不支持直接校验 List<@Valid Entity>,需用包装类或手动校验。)
java·开发语言
雨夜之寂5 小时前
mcp java实战 第一章-第一节-MCP协议简介.md
java·后端
皮皮林5515 小时前
蚂蚁又开源了一个顶级 Java 项目!
java
吹晚风吧5 小时前
spring是如何解决循环依赖的(二级缓存不行吗)?
java·spring·循环依赖·三级缓存