2

我是使用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服务* /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 正在抛出错误消息:

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

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

4

1 回答 1

1

这个错误告诉你,当试图注入一个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注释,因为您首先想要一个单例。如果省略它,您将只有一个没有范围的绑定,这意味着每次必须注入一个实例时,都会创建一个新实例(有利于无状态)。

于 2018-09-11T14:49:43.187 回答