javawelddeltaspike

DeltaSpike interface configuration not injectable


I'm following the DeltaSpike documentation to create an injectable interface that binds configuration properties.

@Configuration(prefix = "application.")
public interface AppConfig {

    @ConfigProperty(name = "name", evaluateVariables = false)
    String getApplicationName();
}

I've tried using it both via BeanProvider#getContextualReference and with @Inject.

@Inject
public Framework(final AppContext context, final BeanManager beanManager, AppConfig app) {
    this.appContext = context;
    this.beanManager = beanManager;
    logger.info("Initialization application with name {}.", app.getApplicationName());
}
Exception in thread "main" org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type AppConfig with qualifiers @Default

I also receive the warning:
Unsatisfied dependency: no bean matches the injection point

I've tried fiddling with the beans.xml file and dependencies in build.gradle but to no avail, and I'm unsure what I'm doing wrong compared to the documentation.

Could someone try to point me in the right direction?


Solution

  • The problem wasn't with DeltaSpike, but rather with Weld, the CDI implementation the project depends on.

    The issue is caused from Gradle building the main and resource separately, so first the resource output needs to be modified to put them together. This sets the resource output to the classes directory.

    sourceSets {
        main {
            output.resourcesDir = output.classesDirs.singleFile
        }
    
        test {
            output.resourcesDir = output.classesDirs.singleFile
        }
    }
    

    Then, run the application with Gradle instead of the IDE, to do this, add and configure the application plugin. This will create a Gradle task called run.

    plugins {
        id "application"
        id "java"
        id "io.spring.dependency-management" version "1.0.9.RELEASE"
    }
    
    application {
        mainClassName = "org.example.app.Main"
    }
    

    If one is using DeltaSpike annotations, they may need to modify beans.xml to find all rather than annotated.

    More information:
    https://discuss.gradle.org/t/application-plugin-run-task-should-first-consolidate-classes-and-resources-folder-or-depend-on-installapp-or-stuff-like-weld-se-wont-work/1248

    Repository containing a minimal DeltaSpike / Weld / Gradle project:
    https://gitlab.com/SethFalco/mini-deltaspike