Spring项目中自定义注解的使用(定义日志切面类)

Java在Jdk1.5中引入了注解,Spring框架也正好把Java注解发挥得淋漓尽致。
接下来简单介绍如何在Spring中自定义注解,其中会使用到Spring框架中的AOP(面向切面编程)。

一、创建自定义注解

package org.spring.springboot.config;

import java.lang.annotation.*;

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface TestLog {
    /*@Documented:注解信息会被添加到Java文档中
    * @Retention:注解的生命周期,表示注解会被保留到什么阶段,可以选择编译阶段、类加载阶段,或运行阶段
    * @Target:注解作用的位置,ElementType.METHOD表示该注解仅能作用于方法上
    */
    String value() default "";
}

二、解析注解

接着完成注解的解析工作,这里使用了Spring的AOP(面向切面编程)特性
通过**@Aspect注解使该类成为切面类**。
通过**@Pointcut 指定切入点** ,这里指定的切入点为TestLog注解类型,也就是被@TestLog注解修饰的方法,进入该切入点,会记录日志信息。

package org.spring.springboot.config;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import java.lang.reflect.Modifier;

@Component
@Aspect
public class TestLogAspect {
    @Pointcut("@annotation(org.spring.springboot.config.TestLog)")
    private void pointcut() {}

    @Before("pointcut() && @annotation(logger)")
    public void advice(JoinPoint joinPoint, TestLog logger) {
        System.out.println("--- TestLog日志的内容为[" + logger.value() + "] ---");
        System.out.println("注解作用的方法名: " + joinPoint.getSignature().getName());
        System.out.println("所在类的简单类名: " + joinPoint.getSignature().getDeclaringType().getSimpleName());
        System.out.println("所在类的完整类名: " + joinPoint.getSignature().getDeclaringType());
        System.out.println("目标方法的声明类型: " + Modifier.toString(joinPoint.getSignature().getModifiers()));
    }
}

三、使用自定义注解

    @TestLog("自定义注解的输出日志内容8888888888888")
    @RequestMapping("/selectByPageTest")
    public IPage<Person> selectByPage(){
        return null;
    }

四、测试结果

在这里插入图片描述
在这里插入图片描述
参考文章
Spring项目中自定义注解的使用
Spring 自定义注解的实现

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用Spring AOP来通过自定义注解来实现切入点。首先,定义一个自定义注解,并在需要切入的方法上使用该注解,然后定义一个切面,并在切面使用@Before 或 @Around 注解来拦截被标记的方法,最后,在Spring的配置文件声明所需的切面,此时,所有被标记的方法都会被拦截。 ### 回答2: 使用Spring AOP切入自定义注解有以下几个步骤: 1. 在项目引入Spring AOP的依赖,一般为spring-aop和spring-aspects。 2. 在配置文件开启Spring AOP的自动代理功能。可以通过在XML配置文件添加<aop:aspectj-autoproxy />或在Java配置文件添加@EnableAspectJAutoProxy注解实现。 3. 创建一个切面,用于定义切入的逻辑。这个需要使用@Component或者其他Spring注解进行标识,以便Spring能够扫描到。 4. 在切面的方法使用@Before、@After等注解定义切入点和具体的切入操作。例如,使用@Before注解定义在某个注解标记的方法执行之前切入的逻辑。 5. 在注解定义定义的切点。可以使用@Retention和@Target等元注解来配置注解的生命周期和使用范围。 6. 在目标或方法上添加自定义的注解。例如,在一个Service的某个方法上添加自定义注解。 7. 运行项目Spring会根据配置自动代理目标,当目标或方法被调用时,切面定义的切入逻辑就会自动被执行。 8. 可以通过配置切入的顺序、通知的型等来进一步细化切入的逻辑。 通过以上步骤,我们可以使用Spring AOP方便地切入自定义注解,实现对目标或方法的增强、日志记录、权限控制等功能。 ### 回答3: 使用Spring AOP切入自定义注解需要以下几个步骤: 1. 定义一个自定义的注解。可以使用Java提供的`@interface`关键字创建一个注解,例如: ```java @Target(ElementType.METHOD) // 定义注解的作用范围为方法 @Retention(RetentionPolicy.RUNTIME) // 注解在运行时可见 public @interface CustomAnnotation { // 自定义注解的属性 } ``` 2. 创建一个切面来处理注解。可以使用Spring提供的`@Aspect`注解来标记切面,并在方法上使用`@Before`、`@After`等注解来定义切入点和增强逻辑。例如: ```java @Aspect @Component public class CustomAspect { @Before("@annotation(customAnnotation)") // 拦截带有CustomAnnotation注解的方法 public void beforeMethod(CustomAnnotation customAnnotation) { // 在方法执行前执行的逻辑 } } ``` 3. 配置Spring AOP。在Spring配置文件添加AOP的配置,例如使用`<aop:aspectj-autoproxy>`标签开启自动代理,并指定切面的包名,让Spring能够自动扫描并应用切面逻辑。 4. 在目标方法上使用自定义注解。在需要切入的方法上标记使用自定义注解,例如: ```java @CustomAnnotation public void doSomething() { // 方法的实际逻辑 } ``` 这样,在调用`doSomething()`方法时,Spring AOP会拦截到带有`@CustomAnnotation`注解的方法,并执行切面逻辑。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值