在使用自定义注解和注解处理器时,编写完后,会忘记创建注解处理器的描述文件,于是找到了
@AutoService
。
@AutoService
是google为注解处理器提供的注解,其作用是编译时在classpath
下生成注解处理器的描述文件。即,生成Processor
实现类的描述文件(META-INF/services/...
)
[AutoService]
项目地址:https://github.com/google/auto/tree/master/service例如:
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.lnkdoc</groupId> <artifactId>AutoService-Demo</artifactId> <version>1.0.0</version> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> <auto-service.version>1.0</auto-service.version> </properties> <dependencies> <dependency> <groupId>com.google.auto.service</groupId> <artifactId>auto-service-annotations</artifactId> <version>${auto-service.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <annotationProcessorPaths> <path> <groupId>com.google.auto.service</groupId> <artifactId>auto-service</artifactId> <version>${auto-service.version}</version> </path> </annotationProcessorPaths> </configuration> </plugin> </plugins> </build> </project>
MyProcessor.java
package cn.lnkdoc.examples.autoservice; import com.google.auto.service.AutoService; import javax.annotation.processing.AbstractProcessor; import javax.annotation.processing.Processor; import javax.annotation.processing.RoundEnvironment; import javax.lang.model.element.TypeElement; import java.util.Set; /** * . * * @author langkye * @date 2021/9/4 */ @AutoService(Processor.class) public class MyProcessor extends AbstractProcessor { /** * {@inheritDoc} * * @param annotations * @param roundEnv */ @Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { //TODO return false; } }
SPI、[
] 、[APT
(Annotation-Processing Tool )JSR269
(Pluggable Annotation Processing API)] 。
注:APT从JDK1.8
起已经被移除(JEP 117: Remove the Annotation-Processing Tool (apt) ),转而使用标准化的JSR269
API: