我是一个使用seedstack的newbe“Clean Java development”框架(seedstack.org)。我在这里的第一个动作是创建一个单例对象并在我的休息服务中调用它。但是种子堆会抛出一个错误......
/*** 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消息:
[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)
我不知道需要什么绑定以及如何以及在何处添加它们。任何人都可以帮我解决这个问题吗?
这个错误告诉你,当试图注入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
注释,因为你首先想要一个单例。如果省略它,您将只有一个没有范围的绑定,这意味着每次必须注入一个实例时,将创建一个新的实例(适用于无状态)。