Java 实现自定义注释

Java 实现自定义注释,java,mysql,spring,annotations,implementation,Java,Mysql,Spring,Annotations,Implementation,我想创建一个将插入数据库的自定义注释(方法范围)。此注释将附加到我的rest控制器中的每个方法,以便在进行api调用时,注释将在数据库的track user表中保存所做的操作 到目前为止,我创建了注释界面,我想我需要添加一个方法,将action&author保存在track user表中,但我不知道在何处或如何: @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface ActionLog

我想创建一个将插入数据库的自定义注释(方法范围)。此注释将附加到我的rest控制器中的每个方法,以便在进行api调用时,注释将在数据库的track user表中保存所做的操作

到目前为止,我创建了注释界面,我想我需要添加一个方法,将action&author保存在track user表中,但我不知道在何处或如何:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ActionLog {
    String action() default "UNDEFINED";
    String author() default "UNDEFINED";
}
我想这样使用它:

@ActionLog(author="John",action="get all users")
public List<User> getAllUsers() { return repo.findAll(); }
@ActionLog(author=“John”,action=“获取所有用户”)
公共列表getAllUsers(){return repo.findAll();}

然后在我的数据库中,我应该有一个新的插入动作及其作者

要创建您自己的注释,您必须首先创建一个您已经完成的接口,而不是为相同的操作编写一个方面类

@Component
@Aspect
public class ActionLogAspect {


  @Around(value = "@annotation(ActionLog)", argNames = "ActionLog")
  public  getUsersByAuthorName(ProceedingJoinPoint joinPoint, ActionLog actionLog) throws Throwable {

    List<User> userList = new ArrayList();

     //Your Logic for getting user from db using Hibernate or Jpa goes here.
     //You can call your functions here to fetch action and author by using
    // actionLog.action() and actionLog.author()

    return userList;
    }

}
@组件
@面貌
公共类ActionLogAspect{
@大约(value=“@annotation(ActionLog)”,argNames=“ActionLog”)
public getUsersByAuthorName(ProceedingJoinPoint、ActionLog、ActionLog)抛出可丢弃的{
List userList=new ArrayList();
//使用Hibernate或Jpa从db获取用户的逻辑如下。
//您可以在此处调用函数,通过使用
//actionLog.action()和actionLog.author()
返回用户列表;
}
}

这取决于您使用的框架。它是否支持拦截器或其他形式的AOP?事实上,我被要求不要使用AOP您目前在rest服务中使用的是什么?(这些标签并不明显)只是更新了标签,这是一个简单的rest spring boot应用程序这将是AOP的一个亮点。为什么不允许你使用它?