锋盈数科-知识库 Logo
首页
软件开发
计算机基础
Hello Halo
新手必读
关于本知识库
登录 →
锋盈数科-知识库 Logo
首页 软件开发 计算机基础 Hello Halo 新手必读 关于本知识库
登录
  1. 首页
  2. 软件开发
  3. Spring Boot——自定义注解和AOP实现

Spring Boot——自定义注解和AOP实现

0
  • 软件开发
  • 发布于 2024-08-17
  • 0 次阅读
黄健
黄健

要实现自定义注解和AOP(面向切面编程)在Spring Boot中的应用,可以通过自定义注解来标记需要进行AOP处理的方法,然后使用AOP来实现对这些方法的增强操作。

1. 创建自定义注解

首先创建一个自定义注解,用于标记需要进行AOP处理的方法。

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface CustomAnnotation {
   
    String value() default "";
}

2. 创建切面类

创建一个切面类,用于定义在被 CustomAnnotation 注解标记的方法上执行的增强操作。

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

@Aspect
@Component
public class CustomAspect {
   

    @Before("@annotation(com.example.CustomAnnotation)")
    public void beforeMethod(JoinPoint joinPoint) {
   
        System.out.println("Before executing method: " + joinPoint.getSignature().getName());
    }
}

3. 创建一个Spring Boot应用

创建一个Spring Boot应用,并在主类中添加@EnableAspectJAutoProxy注解来启用AOP功能。

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@SpringBootApplication
@EnableAspectJAutoProxy
public class MySpringBootApplication {
   

    public static void main(String[] args) {
   
        SpringApplication.run(MySpringBootApplication.class, args);
    }
}

4. 创建一个Service类

创建一个Service类,在其中的方法上加上自定义注解 @CustomAnnotation 。

import org.springframework.stereotype.Service;

@Service
public class MyService {
   

    @CustomAnnotation
    public void myMethod() {
   
        System.out.println("Executing myMethod");
    }
}

5. 在Controller中调用Service方法

创建一个Controller类,在其中调用Service类中被 @CustomAnnotation 注解标记的方法。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {
   

    @Autowired
    private MyService myService;

    @GetMapping("/execute")
    public String executeMethod() {
   
        myService.myMethod();
        return "Method executed";
    }
}

通过以上步骤,可以实现在Spring Boot应用中使用自定义注解和AOP来实现对方法的增强操作。当调用被 @CustomAnnotation 注解标记的方法时,AOP切面会在方法执行前输出日志信息。

原文链接: https://blog.csdn.net/2401_82884096/article/details/138335892

标签: #Spring Boot 173 #软件开发 1171 #JAVA 991
相关文章

万字:支付“核心系统”详解 2024-11-02 15:33

专栏作者:隐墨星辰 \| 主编:陈天宇宙 这篇文章也尝试化繁为简,探寻支付系统的本质,讲清楚在线支付系统最核心的一些概念和设计理念。 虽然支付行业已经过了风头最劲的时光,但跨境支付仍然在蓬勃发展,每年依然有很多新人进入这个行业,这篇文章尝试为这些刚入行的新人提供一点帮助。 文章只介绍一些支付行业十几

资深支付架构师视角:实战从问题定义到代码落地的完整套路 2024-11-02 15:33

前言 今天从一个实际案例入手,介绍站在架构师的角度,如何识别并定义问题,提炼需求,技术方案选型,再到详细设计,最后利用AI的能力协助写出核心的代码,验证与调优。 解决问题存在一定的模式,也可以称之为框架,总结出自己的思考和解题框架,以后再碰到同类型的问题就可以如庖丁解牛一样容易。 很多年前,我写代码

Spring 实现 3 种异步接口 2024-10-18 09:07

大家好,我是苏三~ 如何处理比较耗时的接口? 这题我熟,直接上异步接口,使用 Callable、WebAsyncTask 和 DeferredResult、CompletableFuture等均可实现。 但这些方法有局限性,处理结果仅返回单个值。在某些场景下,如果需要接口异步处理的同时,还持续不断地

重学SpringBoot3-集成Redis(五)之布隆过滤器 2024-10-08 11:24

更多SpringBoot3内容请关注我的专栏:《SpringBoot3》 期待您的点赞👍收藏⭐评论✍ 重学SpringBoot3-集成Redis(五)之布隆过滤器 1. 什么是布隆过滤器? * 基本概念 适用场景 2. 使用 Redis 实现布隆过滤器 * 项目依赖 Redis 配置

设计模式第16讲——迭代器模式(Iterator) 2024-10-08 11:24

一、什么是迭代器模式 迭代器模式是一种行为型设计模式,它提供了一种统一的方式来访问集合对象中的元素,而不是暴露集合内部的表示方式。简单地说,就是将遍历集合的责任封装到一个单独的对象中,我们可以按照特定的方式访问集合中的元素。 二、角色组成 抽象迭代器(Iterator):定义了遍历聚合对象所需的方法

vue2路由和vue3路由区别及原理 2024-10-08 11:24

一、Vue2 与 Vue3 路由的区别 1. 创建路由实例方式的不同 Vue 2 中,通过 Vue.use() 注册路由插件,并通过 new VueRouter() 来创建路由实例。 import Vue from 'vue';import VueRouter from 'vue-router';i

目录

IT 外包服务商

  • 意见投递
  • zyf6619

软件开发应用

主菜单

  • 首页
  • 软件开发
  • 计算机基础
  • Hello Halo
  • 新手必读
  • 关于本知识库
Copyright © 2024 your company All Rights Reserved. Powered by Halo.