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 附加了什么能力比较透明
缺点 表达式有点难写;(可无限制添加)可能会有些混乱 (不能随意添加)可能会有局限性
相关推荐
choice of15 分钟前
SpringMVC通过注解实现全局异常处理
java·后端·spring
单线程bug15 分钟前
Spring Boot中Filter与Interceptor的区别
java·spring boot·后端
小蒜学长21 分钟前
基于uni-app的蛋糕订购小程序的设计与实现(代码+数据库+LW)
java·数据库·spring boot·后端·小程序·uni-app
GitCode官方23 分钟前
告别环境地狱!Java生态“AI原生”解决方案入驻 GitCode
java·开源·gitcode
东方芷兰1 小时前
Leetcode 刷题记录 21 —— 技巧
java·算法·leetcode·职场和发展·github·idea
kyle~2 小时前
排序---选择排序(Selection Sort)
java·算法·排序算法
七夜zippoe5 小时前
事务方案选型全景图:金融与电商场景的实战差异与落地指南
java·分布式·事务
杨二K6 小时前
认识HertzBeat的第一天
java·hertzbeat
DevilSeagull6 小时前
JavaScript WebAPI 指南
java·开发语言·javascript·html·ecmascript·html5
二掌柜,酒来!7 小时前
完美解决:应用版本更新,增加字段导致 Redis 旧数据反序列化报错
redis·spring·bootstrap