2

I am faced with a strange issue trying to create a custom Scope in my application.

I made minimal sample application to illustrate the problem.

Environment:

  • Java 11 (but same with 17 either)
  • Wildfly 25.0.0.Final

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");
    }
}

At that stage everything is ok:

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

But after adding a new empty class to the project

SampleExtension.java

package com.example.extension;

import javax.enterprise.inject.spi.Extension;

public class SampleExtension implements Extension { }

It starts to fail:

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

I'm new in the Java EE world and I stuck with it. Please explain what is wrong with my code?

P.S. I've checked the same code using Jakarta EE 9.1 and GlassFish 6.2.2 (have to change imports from javax to jakarta) and there is no such issue.

Jens Piegsa
  • 7,399
  • 5
  • 58
  • 106
RoninDev
  • 5,446
  • 3
  • 23
  • 37
  • Do you have a `beans.xml` in your WAR? – James R. Perkins Oct 29 '21 at 16:37
  • @JamesR.Perkins No, I have no beans.xml – RoninDev Oct 30 '21 at 06:35
  • It seems to me that issue is tied with a fact that I introduce EJB (@Singleton @Startup) bean in my app to support eager initialization – RoninDev Oct 30 '21 at 06:48
  • 1
    If you want to use a CDI extension you need to provide, even if it's empty, a `beans.xml` file. – James R. Perkins Nov 01 '21 at 18:57
  • I don't know what the specification says about it but may be if you create an `Extension` class then the file `/META-INF/services/javax.enterprise.inject.spi.Extension` becomes mandatory. Try creating it (with your FQCN inside). Have tried with a previous version of Wildfly? Or with another Java EE 8 certified server? – gmanjon Nov 02 '21 at 18:28
  • @JamesR.Perkins Post your comment as an answer, please. It's works – RoninDev Nov 03 '21 at 07:27

1 Answers1

1

Given you're adding a CDI extension you need to include a beans.xml in your deployment. Per the 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.

James R. Perkins
  • 16,800
  • 44
  • 60