谈谈spring-boot自动生成spring-configuration-metadata.json原理
2019-05-03 07:59
344 查看
JDK6提供了一个可以在编译阶断对代码进行额外补充操作的一个功能
该功能有以下几个步骤:
首先需要继承javax.annotation.processing.AbstractProcesso
r类。
@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包。运行编译可以看到效果:
[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
再来看看spring-boot是怎么生成元素据提示信息的
需要依赖一个JAR包,该JAR包含一个继承javax.annotation.processing.AbstractProcesso
的实现类。
<!-- @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>
打开原码结构看一下:
(adsbygoogle = window.adsbygoogle || []).push({});相关文章推荐
- Spring Boot核心原理-自动配置 以及@ConfigurationProperties 注解
- SpringBoot2.0系列教程(十)Springboot框架通过freemarker自动生成service,serviceImpl,controller功能
- 《Spring Boot极简教程》第5章 Spring Boot自动配置原理
- springboot和mybatis集成,自动生成model、mapper,增加mybatis分页功能
- 自动生成Spring Boot项目
- SpringBoot自动加载的原理
- springboot自动生成mysql的DAO层代码
- springboot与myabitis的整合,以及自动生成mapper
- SpringBoot 自动生成API文档
- 关于spring boot自动注入出现Consider defining a bean of type 'xxx' in your configuration问题解决方案 标签: spring 201
- 全面解析SpringBoot自动配置的实现原理
- spring boot rest接口自动生成文档(包含swagger)
- spring-boot集成mybatis问题1:IntellijIdea中j解决自动生成代码xml配置文件中出现“uri is not registered”问题
- SpringBoot中mybatis的自动生成
- Spring Boot自动配置原理
- SpringBoot结合Swagger2自动生成api文档的方法
- Spring Boot核心原理-自动配置
- springboot使用generator-mybatis插件自动生成mybatis文件
- 谈谈Shiro的原理及在SSM和SpringBoot两种环境下的使用姿势(下篇)
- 【完美】SpringBoot+Mybatis-Generator自动生成