我们有一些代码需要在很多方法中运行,对于我们的开发人员来说,必须一遍又一遍地编写这些代码是很乏味的(而且我们希望允许调用的 API 能够在不需要的情况下进行更改)更改任何使用它的代码)。
我们的想法是在调用此代码的方法上添加自定义注释,并且我们将编写自己的注释处理代码来查找该注释,然后在编译之前将代码添加到方法的末尾(但是之后当他们在 IDE 中查看文件时,代码仍然不存在)。
我该如何实现这一目标?我需要做什么才能使 Gradle 调用的东西能够更改传递给编译器/构建器的方法定义并能够读取方法上的注释?
(我们使用 Spring Boot 和 Gradle,但这可能没有什么区别)
最佳答案
Spring AOP 足以满足您的要求。
这是一个小例子,可以让您了解一下:有两个类,每个类都有三个共同的方法:play()、addPlayer() 和 gameover(),并且每次调用 play 方法时程序都会这样做必须调用例程来打印文本,使用 AOP,您不需要重复相同的代码。
对于组织顺序,我将使用一个界面,这不是强制性的,但这是一个很好的做法:
游戏界面:
public interface Game {
void play();
void addPlayer(String name);
void gameOver();
}
实现 Game 的足球类(class)
public class Soccer implements Game {
@Override
public void play() {
System.out.println("Soccer Play started");
}
@Override
public void addPlayer(String name) {
System.out.println("New Soccer Player added:" + name);
}
@Override
public void gameOver() {
System.out.println("This soccer Game is Over");
}
}
实现 Game 的棒球类
public class Baseball implements Game {
@Override
public void play() {
System.out.println("Baseball game started at " + new Date());
}
@Override
public void addPlayer(String name) {
System.out.println("New Baseball Player added: " +name);
}
@Override
public void gameOver() {
System.out.println("The game is over");
}
}
现在调用 play 方法时捕获的 Aspect 配置
@Aspect
@Component
public class AspectConfiguration {
@Before("execution(* org.boot.aop.aopapp.interfaces.Game.play(..))")
public void callBefore(JoinPoint joinPoint){
System.out.println("Do this allways");
System.out.println("Method executed: " + joinPoint.getSignature().getName());
System.out.println("******");
}
}
@Before
注解表示该方法将在执行 play 方法之前调用。此外,您还需要指定一个切入点表达式,告诉 Aspect 如何匹配您需要触发的方法调用。例如,在本例中,我们使用 play 方法,它是切入点表达式: "execution(* org.boot.aop.aopapp.interfaces.Game.play(..))"
最后是 Spring Boot 应用程序类:
@EnableAspectJAutoProxy
@SpringBootApplication
public class AopappApplication {
public static void main(String[] args) {
Game soccer=null;
Game baseball=null;
AnnotationConfigApplicationContext ctx = (AnnotationConfigApplicationContext) SpringApplication.run(AopappApplication.class, args);
soccer = (Game) ctx.getBean("soccer");
baseball = (Game) ctx.getBean("baseball");
soccer.play();
baseball.play();
soccer.addPlayer("Player 1");
soccer.addPlayer("Player 2");
baseball.addPlayer("Player 23");
soccer.gameOver();
baseball.gameOver();
}
@Bean("soccer")
public Game soccer(){
return new Soccer();
}
@Bean("baseball")
public Game baseball(){
return new Baseball();
}
}
有一个关于Spring AOP的大神文档请看下面的链接。 Spring AOP Doc.
关于java - 如何创建由 gradle 预编译运行的注释处理器以将代码添加到方法中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45449397/