2
votes

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.

1

1 Answers

0
votes

Did not work with extensions, but reading Java EE CDI Extension example, it said that you must add a file named javax.enterprise.inject.spi.Extension inside /META-INF/services folder. This file must contain our extension class fully qualified name. In your case it is com.example.extension.SampleExtension

Here is an article which says the same, but specially for WildFly: Creating an Extension