找回密码
 立即注册
首页 业界区 业界 Spring AOP 与 Solon AOP 有什么区别?

Spring AOP 与 Solon AOP 有什么区别?

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

  • Spring AOP 使用表达式确定“切入点”,可以是某个注解(有侵入),可以是包名或类名或方法(无侵入)
  • Solon AOP 只使用某个注解确定“切入点”(有侵入)
先看两个示例
1、Spring AOP 示例

Spring AOP 有很多不同的能力构建方式。此处采用更简洁的一种方式:
  1. import org.aspectj.lang.JoinPoint;
  2. import org.aspectj.lang.annotation.*;
  3. import org.springframework.stereotype.Component;
  4. @Aspect
  5. @Component
  6. public class LoggingAspect {
  7.     @Pointcut("execution(* com.example.demo.service.*.*(..))") //也可以是某注解表达式
  8.     public void serviceLayer() {}
  9.     @Around("serviceLayer()")
  10.     public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
  11.         System.out.println("test");
  12.         return joinPoint.proceed();
  13.     }
  14. }
复制代码
应用示例
  1. package com.example.demo.service;
  2. @Component
  3. public class UserService {
  4.     public String getUserById(Long id) {
  5.         return "user-" + id;
  6.     }
  7.     public void updateUser(String user) {
  8.         System.out.println("update: " + user);
  9.     }
  10. }
复制代码
2、Solon AOP 示例

Solon AOP 有两种能力构建方式。此处采用更简洁的一种方式:
  1. import org.noear.solon.annotation.Around;
  2. import org.noear.solon.core.aspect.Invocation;
  3. import org.noear.solon.core.aspect.MethodInterceptor;
  4. @Around(Logging.LoggingInterceptor.class) //为注解,附加包围处理的能力
  5. @Target({ElementType.TYPE,  ElementType.METHOD})
  6. @Retention(RetentionPolicy.RUNTIME)
  7. @Documented
  8. public @interface Logging {
  9.     class LoggingInterceptor implements MethodInterceptor {
  10.         @Override
  11.         public Object doIntercept(Invocation i) throws Throwable {
  12.             System.out.println("test");
  13.             return i.invoke();
  14.         }
  15.     }
  16. }
复制代码
应用示例
  1. package com.example.demo.service;
  2. @Logging
  3. @Component
  4. public class UserService {
  5.     public String getUserById(Long id) {
  6.         return "user-" + id;
  7.     }
  8.     public void updateUser(String user) {
  9.         System.out.println("update: " + user);
  10.     }
  11. }
复制代码
3、总结

体验感受Spring AOPSolon AOP有侵入体验通过表达式描述,使用时添加“注解”定义注解,使用时添加“注解”无侵入体验通过表达式描述包名或类名或方法,使用时无感/优点可以完全“无侵入”实现 AOP附加了什么能力比较透明缺点表达式有点难写;(可无限制添加)可能会有些混乱(不能随意添加)可能会有局限性
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册