Spring Boot中使用AOP

spring boot 中使用AOP,需要先在pom.xml中添加依赖:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

然后编写切面程序,例如:

@Aspect
@Component
public class HttpAspect {
 
    private static final Logger logger=LoggerFactory.getLogger(HttpAspect.class);
 
    //配置切点
    @Pointcut("execution(public * com.springboot.controller.GirlController.*(..))")
    public void log(){
    }
 
    //joinPoint用于获取域切入点方法有关的信息
    @Before("log()")
    public void doBefore(JoinPoint joinPoint){
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request=requestAttributes.getRequest();
        //url
        logger.info("url={}",request.getRequestURL());
        //method
        logger.info("method={}",request.getMethod());
        //ip
        logger.info("ip={}",request.getLocalAddr());
        //类方法
        logger.info("class_method={}",joinPoint.getSignature().getDeclaringType()+"."+joinPoint.getSignature().getName());
        //方法参数
        logger.info("args={}",joinPoint.getArgs());
    }
 
    @After("log()")
    public void doAfter(){
        logger.info("doAfter={}",2222222);
    }
 
    //得到response返回的数据,returning代表切入点方法返回的数据
    @AfterReturning(returning = "object",pointcut = "log()")
    public void doAfterReturning(Object object){
        logger.info("response={}",object.toString());
    }
}

需要在类上添加@Aspect注解表明这是一个切面,添加@Component注解是为了能够让spring的bean容器对齐进行管理,@Pointcut定义切入点,@Befor,@After,等注解定义连接点,被这些注解的方法作为Advice(通知)织入pointcut匹配的方法中。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot 使用 AOP,可以通过以下步骤来配置: 1. 在 Maven 或 Gradle 构建文件,添加 `spring-boot-starter-aop` 依赖项,以引入 Spring Boot AOP 模块。 2. 定义一个切面类,使用 `@Aspect` 注解标注该类,同时在该类定义一个或多个通知方法,通知方法使用 `@Before`、`@After`、`@Around` 等注解来标注。 3. 在 Spring Boot 应用程序类上添加 `@EnableAspectJAutoProxy` 注解,以启用 AspectJ 自动代理。 4. (可选)如果需要使用基于注解的 AOP 配置,可以在 Spring Boot 应用程序类上添加 `@ComponentScan` 注解,以扫描切面类和被切入的类。 5. 在 Spring Boot 配置文件(如 `application.properties` 或 `application.yml`),可以配置 AOP 的相关属性,例如日志级别、切入点表达式等。 下面是一个简单的示例: ``` // 定义切面类 @Aspect @Component public class LoggingAspect { @Before("execution(* com.example.demo.service.*.*(..))") public void logBefore(JoinPoint joinPoint) { System.out.println("method " + joinPoint.getSignature().getName() + " is called"); } } // 启用 AspectJ 自动代理 @EnableAspectJAutoProxy @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } ``` 在上面的示例,定义了一个切面类 `LoggingAspect`,它包含一个 `@Before` 通知方法。在 `DemoApplication` 类上添加了 `@EnableAspectJAutoProxy` 注解,以启用 AspectJ 自动代理。在 `LoggingAspect` 类上添加了 `@Component` 注解,以使它能够被 Spring 扫描到并注册为一个切面类。在 `@Before` 注解定义了一个切入点表达式,表示该通知方法将在 `com.example.demo.service` 包的任何方法执行前调用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值