【问题标题】:seedstack Explicit bindings are required and MYOBJECT is not explicitly boundseedstack 显式绑定是必需的,MYOBJECT 没有显式绑定
【发布时间】:2018-09-11 14:34:51
【问题描述】:

我是使用seedstack“清洁Java开发”框架(seedstack.org)的新手。 我在这里的第一个动作是创建一个单例对象并在我的休息服务中调用它。但是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();
    }
}

Seedstack 抛出错误消息:

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

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

【问题讨论】:

    标签: java seedstack


    【解决方案1】:

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

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

    SeedStack 通过扫描类路径和查找感兴趣的类为您做了很多绑定(例如,@Path-annotated 类是由 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 注释,因为您首先想要一个单例。如果省略它,您将只有一个没有范围的绑定,这意味着每次必须注入一个实例时,都会创建一个新实例(有利于无状态)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-23
      相关资源
      最近更新 更多