我是一个使用种子堆栈“清洁Java开发”框架(seedstack.org)的新手。这里我的第一个操作是创建一个单例对象,并在rest服务中调用它。但是种子堆叠抛出了一个错误。
/*** 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服务*/包
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)我不知道需要什么绑定,以及如何和在何处添加它们。有人能帮我解决这个问题吗?
发布于 2018-09-11 14:49:43
此错误告诉您,当尝试注入AppSingleton实例时,注入器不知道如何注入。“如何注入某物”的注入器术语是“绑定”。把它当作注射的规则。
在您的示例中,您可能会出现此错误,因为@Singleton注释不足以创建绑定:它只是指定潜在绑定的范围。
SeedStack通过扫描类路径和查找感兴趣的类为您做了许多绑定(例如,@Path-annotated类是由REST模块自动绑定的JAX资源)。在您的示例中,您希望创建任意绑定,因此必须使用@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注释,因为您首先想要一个单例。如果省略它,您将只有一个没有作用域的绑定,这意味着每次必须注入一个实例时,都会创建一个新的实例(对于无国籍状态很好)。
https://stackoverflow.com/questions/52278368
复制相似问题