我想创建一个自定义的注释(方法作用域)将在数据库中插入。这个注解会附着在我休息控制器的每一个方法,这样,当一个API调用时,注释保存在数据库中的一个跟踪用户表所做的动作
到目前为止,我创建的注释界面,我想我需要补充的是保存在跟踪用户表中的动作与笔者的方法,但我不知道在哪里或如何:
@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(); }
然后在我的数据库中,我应该有它的作者行动的新的插入
要创建自己的注解,你必须首先创建你已经不是你写的看点类一样做一个接口。
@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;
}
}