![](/img/trans.png)
[英]What are the risks of explicitly casting from a list of type List<? extends MyObject> to a list of type List<MyObject> in Java?
[英]seedstack Explicit bindings are required and MYOBJECT is not explicitly bound
我是使用“干凈的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;
}
}
/ *我的休息服務* /包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)
我不知道需要什么綁定以及如何以及在何處添加它們。 誰能幫我解決這個問題?
該錯誤告訴您,在嘗試注入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
批注,因為您首先想要一個單例。 如果您省略它,則將只有一個沒有范圍的綁定,這意味着每次必須注入一個實例時,都會創建一個新實例(有利於無狀態)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.