简体   繁体   中英

seedstack Explicit bindings are required and MYOBJECT is not explicitly bound

I am a newbe using seedstack the "Clean Java development" framework (seedstack.org). My first action here was to create a singleton object and call it in my rest service. But seedstack throws an error...

/*** MY SINGLETON ***/
package com.opel.application.partmapping.domain.shared;
import javax.inject.Singleton;

@Singleton
public class AppSingleton {
private String aSingleStr = "Xxxxxxx Yyyyy @ Zzzz" ;
       public String getASingleStr() {
             this.aSingleStr = this.aSingleStr + "x" ;
             return this.aSingleStr;
       }
}

/ * MY REST SERVICE * / package com.opel.application.partmapping.interfaces.rest;

import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;

import org.seedstack.seed.Application;
import org.seedstack.seed.Configuration;
import org.seedstack.seed.Logging;
import org.slf4j.Logger;

import com.opel.application.partmapping.domain.shared.AppConfig;
import com.opel.application.partmapping.domain.shared.AppSingleton;


@Path("hello")
public class HelloResource {

    @Inject
    private Application application;

    @Inject
    private AppSingleton theAppSingle;

       @Logging
       private Logger logger;

       @Configuration
       private AppConfig theAppConfig;

    @GET
    @Path("hello1")
    public String hello1() {
       logger.info("Hello Xxxxxxx... in hello1() ");
        return "The author of this tool is ..."  ;
    }

    @GET
    @Path("hello2")
    public String hello2() {
        return "The author of this tool is ..." + theAppConfig.getTheAuthor();
    }

    @GET
    @Path("hello3")
    public String hello3() {
       AppConfig myConfig = application.getConfiguration().get(AppConfig.class);

        return "The author of this tool is ..." + myConfig.getTheAuthor();
    }    

    @GET
    @Path("hello4")
    public String hello4() {

        return "The singleton ..." + theAppSingle.getASingleStr();
    }
}

Seedstack is throwing the ERROR message:

[ERROR] Failed to execute goal org.seedstack:seedstack-maven-plugin:2.7.1:watch (default-cli) on project PartMapper: An exception occurred while executing SeedStack application: [CORE] Unexpected exception: Unable to create injector, see the following errors:
[ERROR] 
[ERROR] 1) Explicit bindings are required and com.opel.application.partmapping.domain.shared.AppSingleton is not explicitly bound.
[ERROR]   while locating com.opel.application.partmapping.domain.shared.AppSingleton
[ERROR]     for field at com.opel.application.partmapping.interfaces.rest.HelloResource.theAppSingle(HelloResource.java:22)
[ERROR]   at org.seedstack.seed.rest.internal.RestModule.configure(RestModule.java:41) (via modules: com.google.inject.util.Modules$OverrideModule -> io.nuun.kernel.core.internal.injection.KernelGuiceModuleInternal -> org.seedstack.seed.rest.internal.RestPlugin$1 -> org.seedstack.seed.rest.internal.RestModule)

I do not know what bindings are required and how and where to add them. Can anyone help me to solve that issue?

This errors tells you that, when trying to inject an AppSingleton instance, the injector doesn't know how. The injector terminology for "how to inject something" is "a binding". Think of it as a rule of injection.

In your case, you can this error because the @Singleton annotation is not enough to create a binding: it just specifies the scope of the potential binding.

SeedStack does a lot of bindings for you by scanning the classpath and finding classes of interest (for instance @Path -annotated classes are JAX-RS resources automatically bound by the REST module). In your case, you want to create an arbitrary binding, so you have to use the @Bind annotation:

import javax.inject.Singleton;
import org.seedstack.seed.Bind;

@Singleton
@Bind
public class AppSingleton {
    private String aSingleStr = "Xxxxxxx Yyyy @ Zzzz" ;

    public String getASingleStr() {
        this.aSingleStr = this.aSingleStr + "x" ;
        return this.aSingleStr;
    }
}

Note that I kept the @Singleton annotation since you wanted a singleton in the first place. If you omit it, you will only have a binding without scope, meaning that everytime an instance must be injected, a new one will be created (good for statelessness).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM