seedstack需要显式绑定,并且未明确绑定MYOBJECT

时间:2018-09-11 14:34:52

标签: java seedstack

我是一个使用“干净的Java开发”框架(seedstack.org)的seedstack的新手。 我在这里的第一个动作是创建一个单例对象,并在我的rest服务中调用它。但是Seedstack引发了错误...

/*** 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;
       }
}

/ *我的REST服务* /     包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();
    }
}

种子堆栈抛出错误消息:

[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)

我不知道需要什么绑定以及如何以及在何处添加它们。 谁能帮助我解决这个问题?

1 个答案:

答案 0 :(得分:0)

此错误告诉您,在尝试注入AppSingleton实例时,注入器不知道如何操作。用于“如何注入某些东西”的注入器术语是“绑定”。将其视为注射的规则。

在您的情况下,您会出现此错误,因为@Singleton注释不足以创建绑定:它仅指定潜在绑定的范围。

SeedStack通过扫描类路径并查找感兴趣的类来为您做很多绑定(例如,带有注释的@Path类是REST模块自动绑定的JAX-RS资源)。在您的情况下,您想创建一个任意绑定,因此必须使用@Bind批注:

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

请注意,我保留了@Singleton注释,因为您首先想要一个单例。如果您省略它,则将只有一个没有范围的绑定,这意味着每次必须注入一个实例时,都会创建一个新实例(有益于无状态性)。