当前位置: 首页 > 知识库问答 >
问题:

如何创建批注处理器由gradle预编译运行,以向方法添加代码?

慕容恩
2023-03-14

我该如何做到这一点?我需要做什么才能使Gradle调用的东西能够更改传递给编译器/构建器的方法定义并能够读取方法上的注释?

(我们使用Spring Boot和Gradle,但这可能不会有什么不同)

共有1个答案

康锦
2023-03-14

Spring AOP足以满足您的需求。

这是一个小示例,可以让您了解:有两个类,每个类都有三个共同的方法:play()、addPlayer()和gameover(),每次调用play方法时,程序都必须调用一个例程来打印文本,使用AOP时,您不需要重复相同的代码。

对于组织顺序,我将使用一个接口,它不是强制性的,但这是一个很好的实践:

public interface Game {
    void play();
    void addPlayer(String name);
    void gameOver();
}
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");
    }
}
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
@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方法之前调用该方法。另外,您需要指定一个切入点表达式,告诉方面如何匹配您需要触发的方法调用。例如,在本例中,我们使用play方法,它是pointcut表达式:“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();
    }
}
 类似资料:
  • 在META-INF/Services中,添加了javax.annotation.processing.processor条目,指定自定义注释处理器类。

  • null 代码(步骤1和2): 实际上,第一个任务执行良好,并为注释处理器实现编译.class文件。它在第二个任务停止。 Ant说: Java 1.6 Ant 1.8.2

  • 问题内容: 是否可以使用XML配置中的规范以编程方式添加Log4J2附加程序? 我计划在log4j2.xml中定义所有内容,然后按情况选择追加器(不会编译): 问题答案: 编辑:有关log4j2的最新版本,请参阅下方答案 我给他们留下了不希望您这样做的印象,但这对我有用:

  • 我有一个批处理文件,它运行几个python脚本来修改表。 > 我还想添加注释,特别提醒他们在运行批处理文件之前需要在批处理文件中更新的变量。我知道我可以使用。但这看起来更像是为了在用户运行之后更新进度。 是否有更恰当地添加注释的语法?

  • 我试图遵循下面提到的两个步骤: 1)下载的源代码 https://sourceforge.net/projects/hunspell/files/hyphen/2.8/hyphen-2.8.8.tar.gz/download 连字符-2.8.8$./example~/dev/smc/hyphenation/hi_in/hyph_hi_in.dic~/hi_sample.text 我已经下载并解压缩