Java 添加扩展后,EE 8 应用程序无法在 Wildfly 25 上启动

Java EE 8 application failed to start on Wildfly 25 after adding Extension

我在尝试在我的应用程序中创建自定义范围时遇到一个奇怪的问题。

我制作了最小的示例应用程序来说明问题。

环境:

build.gradle:

plugins {
    id 'java'
    id 'war'
}

group 'com.example.extension'
version '1.0-SNAPSHOT'

sourceCompatibility = 11

war {
    archiveName 'extension.war'
}

repositories {
    mavenCentral()
}

dependencies {
    compileOnly group: 'jakarta.platform', name: 'jakarta.jakartaee-api', version: '8.0.0'
}

Main.java

package com.example.extension;

import javax.annotation.PostConstruct;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.inject.Inject;

@Singleton
@Startup
public class Main {

    @Inject
    SampleClass sc;

    @PostConstruct
    public void init() {
        sc.sayHello();
    }
}

SampleClass.java

package com.example.extension;

import javax.enterprise.context.ApplicationScoped;

@ApplicationScoped
public class SampleClass {
    public void sayHello() {
        System.out.println("Hello world");
    }
}

那个阶段一切正常:

INFO [stdout] (ServerService Thread Pool -- 78) Hello world

但是在向项目添加一个新的空 class 之后

SampleExtension.java

package com.example.extension;

import javax.enterprise.inject.spi.Extension;

public class SampleExtension implements Extension { }

开始失败:

com.example.extension : extension.war: java.lang.Exception: {"WFLYCTL0080: Failed services" => {"jboss.deployment.unit."extension.war".component.Main.WeldInstantiator" => "Failed to start service Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type SampleClass with qualifiers @Default at injection point [BackedAnnotatedField] @Inject com.example.extension.Main.sc at com.example.extension.Main.sc(Main.java:0) "}}
com.example.extension : extension.war: Artifact is deployed successfully

我是 Java EE 世界的新手,我坚持使用它。请解释我的代码有什么问题?

P.S。我已经使用 Jakarta EE 9.1 和 GlassFish 6.2.2 检查了相同的代码(必须将导入从 javax 更改为 jakarta)并且没有这样的问题。

鉴于您要添加 CDI 扩展,您需要在部署中包含 beans.xml。根据 specification:

An archive which:

  • contains a beans.xml file with the bean-discovery-mode of none, or,

  • contains an extension and no beans.xml file

is not a bean archive.