运行Java程序时找不到自定义注解处理器生成的类
注解处理器编译生成类运行时无法找到问题
问题现象
- IDE(IntelliJ)能正常识别编译期间生成的类,无编译错误;生成带
@Component的Spring Bean时,IDE也能正确识别其为可注入Bean - 运行普通Java程序时报错:
java: cannot find symbol
symbol: class DuhStudentMapper
- 启动Spring Boot服务器时报错:
Field wordMapper2 in com.oworms.word.controller.WordController required a bean of type 'com.oworms.WordMapper2' that could not be found.
项目配置
父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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.noydb.duhmap</groupId> <artifactId>duhmapper</artifactId> <name>duhmapper</name> <version>0.5.1</version> <packaging>pom</packaging> <modules> <module>processor</module> <module>runner</module> </modules> <properties> <java.version>17</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>${java.version}</maven.compiler.source> <maven.compiler.target>${java.version}</maven.compiler.target> <duhmap.processor.version>0.6.0</duhmap.processor.version> </properties> </project>
Processor模块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> <parent> <groupId>com.noydb.duhmap</groupId> <artifactId>duhmapper</artifactId> <version>0.5.1</version> </parent> <artifactId>processor</artifactId> <version>${duhmap.processor.version}</version> <packaging>jar</packaging> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.11.0</version> <executions> <execution> <id>default-compile</id> <configuration> <source>${java.version}</source> <target>${java.version}</target> <compilerArgument>-proc:none</compilerArgument> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>3.3.0</version> <executions> <execution> <id>attach-sources</id> <phase>verify</phase> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
注:不确定是否需要maven-source-plugin,参考其他流行注解处理器库添加了该配置
Runner测试模块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> <parent> <groupId>com.noydb.duhmap</groupId> <artifactId>duhmapper</artifactId> <version>0.5.1</version> </parent> <artifactId>runner</artifactId> <version>0.0.0-final</version> <description> A simple test module to practice and test the processor. Zero importance in regard to this library </description> <dependencies> <dependency> <groupId>com.noydb.duhmap</groupId> <artifactId>processor</artifactId> <version>${duhmap.processor.version}</version> </dependency> </dependencies> </project>
处理器文件写入代码
final Filer filer = processingEnv.getFiler(); final FileObject fileObject; try { fileObject = filer.createResource(StandardLocation.SOURCE_OUTPUT, packageName, className + ".java"); } catch (final IOException e) { throw new DuhMapException( String.format( "Error during writing of DuhMap file to source output for class: %s.%s", packageName, className ), e ); } try (final Writer writer = fileObject.openWriter()) { writer.write(content); } catch (final IOException e) { throw new DuhMapException( String.format( "Error during writing of DuhMap file to source output for class: %s.%s", packageName, className ), e ); }
求助
推测问题出在Maven配置或编译器选项上,参考同类库未发现问题,不知道如何排查,求解决思路。
内容的提问来源于stack exchange,提问作者Mustapha Mond
相关产品推荐
相关产品推荐

