該功能有如下幾個步驟: 首先須要繼承javax.annotation.processing.AbstractProcesso
r類。java
@SupportedAnnotationTypes({"*"}) public class MyProcessor extends AbstractProcessor { @Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { System.out.println("start process"); annotations.forEach(s->{ System.out.println(s); Set<? extends Element> set = roundEnv.getElementsAnnotatedWith(s); set.forEach(System.out::println); }); return true; } @Override public Set<String> getSupportedAnnotationTypes() { return super.getSupportedAnnotationTypes(); } }
而後在class下添加META-INF/services目錄,裏面添加一個javax.annotation.processing.Processor
文件,類容爲上面的實現類: com.github.liuax.mybatis.batch.autoconfigure.MyProcessor
注意編譯時須要傳參:<compilerArgument>-proc:none</compilerArgument>。理由:是由於編譯該工程時,該處理類還未編譯。 新建一個工程,依賴上面打包好的JAR包。運行編譯能夠看到效果:git
[INFO] Compiling 3 source files to Z:\ideaProject\mybatis-batch-parent\mybatis-batch-starter\target\classes start process javax.xml.ws.ServiceMode test.Test2 javax.annotation.Generated test.Test1 test.Test3 start process [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS
須要依賴一個JAR包,該JAR包含一個繼承javax.annotation.processing.AbstractProcesso
的實現類。github
<!-- @ConfigurationProperties annotation processing (metadata for IDEs) --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <version>2.0.1.RELEASE</version> <optional>true</optional> </dependency>
打開原碼結構看一下: spring