You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Quarkus多模块项目依赖注入错误:需手动添加无参构造器

解决Quarkus多模块拆分后无参构造器代理问题

1. 直接修正Bean类构造器

Quarkus生成代理类需要依赖非私有无参构造器,直接给NFCeSyncServiceImpl添加空实现的非私有无参构造器即可:

public class NFCeSyncServiceImpl implements NFCeSyncService {
    // 原有带参构造器
    public NFCeSyncServiceImpl(SomeDependency dependency) {
        this.dependency = dependency;
    }

    // 添加非私有无参构造器
    public NFCeSyncServiceImpl() {
        // 空实现即可,供Quarkus代理生成使用
    }

    // 业务方法逻辑...
}

同时检查类是否被final修饰,若有则去掉——final类无法被Quarkus生成代理。

2. 多模块Jandex索引配置优化

单模块时Quarkus自动处理索引,多模块需确保索引跨模块可识别:

  • 在base模块的pom.xml中配置Jandex插件,确保生成索引:
    <build>
        <plugins>
            <plugin>
                <groupId>org.jboss.jandex</groupId>
                <artifactId>jandex-maven-plugin</artifactId>
                <version>3.1.2</version>
                <executions>
                    <execution>
                        <id>make-index</id>
                        <goals>
                            <goal>jandex</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    
  • 在dominio模块依赖base时,显式引入Jandex索引:
    <dependency>
        <groupId>com.gestorsistemas.chupacabra</groupId>
        <artifactId>base</artifactId>
        <version>${project.version}</version>
        <classifier>jandex</classifier>
    </dependency>
    
  • 也可在根pom.xml的pluginManagement中统一配置Jandex,让所有模块自动生成索引:
    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.jboss.jandex</groupId>
                    <artifactId>jandex-maven-plugin</artifactId>
                    <version>3.1.2</version>
                    <executions>
                        <execution>
                            <id>default-jandex</id>
                            <goals>
                                <goal>jandex</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
    

3. 检查beans.xml配置

确保每个模块的src/main/resources/META-INF/目录下都存在beans.xml,且配置正确:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_2_0.xsd"
       bean-discovery-mode="all">
</beans>

bean-discovery-mode="all"确保Quarkus能扫描到模块内所有Bean。

4. 验证多模块依赖与结构

  • 检查根pom.xml的模块声明,确保base和dominio都被正确纳入:
    <modules>
        <module>base</module>
        <module>dominio</module>
    </modules>
    
  • 确认dominio对base的依赖是compile scope(默认即为compile,若手动修改过需恢复),保证base的类能被dominio正常扫描。

5. 清理缓存并重新构建

执行命令清理Maven缓存,避免旧编译文件干扰:

mvn clean install -DskipTests

重启Quarkus开发模式,加载最新的类文件与配置。


内容的提问来源于stack exchange,提问作者Lucas Bruschi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 19:25:28