SpringAOP之Order注解的完整示例分享

欣喜 Spring 发布时间:2025-03-19 11:17:44 阅读数:17684 1
下文笔者将完整展示AOP之Order注解的示例分享,如下所示
包含 `@Order` 注解的完整配置

1.`LoggingAspect`

package com.example.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Aspect
@Order(2) // 优先级较低
@Component
public class LoggingAspect {

    @Before("execution(* com.example.service.*.*(..))")
    public void logBefore(JoinPoint joinPoint) {
        System.out.println("LoggingAspect: Before method " + joinPoint.getSignature().getName());
    }
}
 
2.`SecurityAspect`

package com.example.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Aspect
@Order(1) // 优先级较高
@Component
public class SecurityAspect {

    @Before("execution(* com.example.service.*.*(..))")
    public void checkSecurity(JoinPoint joinPoint) {
        System.out.println("SecurityAspect: Checking security for method " + joinPoint.getSignature().getName());
    }
}

3.`MyService`

package com.example.service;

import org.springframework.stereotype.Service;

@Service
public class MyService {

    public void performAction() {
        System.out.println("MyService: Performing action");
    }
}

4.`AppConfig`

package com.example;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@Configuration
@ComponentScan("com.example")
@EnableAspectJAutoProxy
public class AppConfig {
}

5.`MainApp`

package com.example;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.example.service.MyService;

public class MainApp {
    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
        MyService myService = context.getBean(MyService.class);
        myService.performAction();
    }
}

运行以上代码,将输出以下信息

SecurityAspect: Checking security for method performAction
LoggingAspect: Before method performAction
MyService: Performing action
 
使用`@Order`注解
    可以明确指定切面的优先级
	确保切面按预期顺序执行
	
1.`@Order`注解:
   - 使用`@Order(value)`
      指定切面的优先级,值越小,
	   优先级越高

2.示例配置:
   - 定义两个切面 `LoggingAspect` 和 `SecurityAspect`,
       并使用 `@Order` 注解设置优先级。
   - 确保在 Spring 应用上下文中
      启用AOP支持

3.注意事项:
   - 使用整数值来指定优先级。
   - 确保切点表达式正确。
   - 设置合理的优先级,避免不必要的性能开销

采用以上方式
  AOP可以按照指定的顺序运行 
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/JavaFramework/Spring/202503/8377.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者