嗨,大家好问我的问题.一个GWT项目,因为我读过Gin我只能在客户端使用,而不是Guice可以在服务器端使用.这是我的问题.
我们先发布一些示例代码.
服务器端.
public class WebchargeServiceImpl extends RemoteServiceServlet implements WebchargeService
{
@Inject
private Injector injector;
@Inject
private ExecuteOperations executeOperations;
.....
executeOperations.do(); ....
Run Code Online (Sandbox Code Playgroud)
这是注入的类ExecuteOperations
@Singleton
public class ExecuteOperations
{
.........
}
Run Code Online (Sandbox Code Playgroud)
我也有servlet模块类
public class SampleWebGuiceServletConfig extends GuiceServletContextListener
{
@Override
protected Injector getInjector()
{
return Guice.createInjector(Stage.DEVELOPMENT, new SampleWebModule());
}
} // class
Run Code Online (Sandbox Code Playgroud)
.....
public class SampleWebModule extends ServletModule
{
@Override
protected void configureServlets()
{
bind(WebchargeServiceImpl.class); //is it correct to bind a class like that?
} // class
Run Code Online (Sandbox Code Playgroud)
web.xml中
<servlet>
<servlet-name>.......WebchargeService</servlet-name>
<servlet-class>.....WebchargeServiceImpl</servlet-class>
</servlet> …Run Code Online (Sandbox Code Playgroud) 我很难弄清楚如何使用Guice将接口绑定到枚举.
public interface Snack {
public int caloriesCount();
}
public enum Chocolate implements Snack {
MILK(20),
DARK(5),
WHITE(10);
private int calCount;
private Chocolate(int calCount) {
this.calCount = calCount;
}
@Override
public int caloriesCount() {
return calCount;
}
}
Run Code Online (Sandbox Code Playgroud)
如果我尝试:
bind(Snack.class).to(Chocolate.class);
我明白了 No implementation for Chocolate was bound
我明白,我不应该尝试绑定到枚举,而应该绑定到枚举值的集合,但它无法实现如何实现.感谢任何提示.谢谢,Maciek
我有一个类,我声明我的静态常量:
public final class ConfigOptions {
public static final String FILE_PATH_SERVER = "/home/user/me/somefile";
}
Run Code Online (Sandbox Code Playgroud)
然后我用Guice将它绑定在我的ServletModule:
public class MyServletModule extends ServletModule {
bind(String.class).annotatedWith(Names.named("filePath"))
.toInstance(ConfigOptions.FILE_PATH_SERVER);
// Also tried
// bindConstant().annotatedWith(Names.named("filePath")).to(ConfigOptions.FILE_PATH_SERVER)
// ... other bindings
}
Run Code Online (Sandbox Code Playgroud)
我的GuiceServletContextListener:
public class MyServletContextListener extends GuiceServletContextListener {
@Override
protected Injector getInjector() {
return Guice.createInjector(new MyServletModule());
}
}
Run Code Online (Sandbox Code Playgroud)
最后,我尝试使用filePath:
public class MyClass {
@Inject
@Named("filePath")
private String filePath;
public MyClass() { ... }
public void doSomething() {
someotherThing.setFilePath(filePath); // But filePath is …Run Code Online (Sandbox Code Playgroud) 我有一个Module提供这样的JDBI DBI实例:
@Provides
@Singleton
DBI dbi(DataSource dataSource) { return new DBI(dataSource); }
Run Code Online (Sandbox Code Playgroud)
在另一个模块中,我想在该DBI实例上调用一些初始化方法(配置对特定数据类型的支持).放入JDBI模块本身并不合适,因为它的应用程序特定于使用JDBI的任何应用程序都不通用.有没有勾到我做那种"额外"的配置?
我尝试使用该bindListener方法,但似乎没有调用以这种方式提供的对象.
我是 Guice 和 JavaFX 的新手(非常新)。我正在构建一个应用程序,它有一个线程侦听套接字连接,并且在接收到事件后,线程将值存储在 ObservableArrayList() 上,应用程序会将它们通知给用户。
我的问题是如何构建所有这些行为,以及如何从线程和 JavaFX 控制器“共享” ObservableList。
我正在阅读有关 Guice 的文章,它可以帮助解耦new对象的创建。
我试图设置一些东西,但 @Inject 属性在我的可运行任务中为空:
图形模块:
public class AppGuiceModule extends AbstractModule{
@Override
protected void configure() {
bind(EventsDAO.class).toInstance(new EventsDAO());
}
}
Run Code Online (Sandbox Code Playgroud)
EventsDAO(具有 ObservableArrayList )
@Singleton
public class EventsDAO {
private ObservableList<ScheduledEvent> localCache = FXCollections.observableArrayList();
public void addEvent(ScheduledEvent event) {
localCache.add(event);
}
public void removeEvent(ScheduledEvent event) {
this.localCache.remove(event);
}
}
Run Code Online (Sandbox Code Playgroud)
有了两个这个,我在我的主要我去创建注入器:
@Override
public void start(Stage stage) throws Exception {
Injector injector = Guice.createInjector(new AppGuiceModule());
Platform.setImplicitExit(false);
Thread …Run Code Online (Sandbox Code Playgroud) 我正在使用Jersey v1.x和Guice Servlet.我想要做的是绑定一个匹配any 的Jersey资源@Path,这样我就可以使用Jersey来回复404.
我希望这样做,因为我的servlet由不同的组件组成(例如,生活在其中的休息API /api,以及生活在其下的Web UI /.在Guice术语中,这意味着我有几个ServletModules,每个都设置了一部分servlet的:
ApiServletModule:serve("/api").with(GuiceContainer.class, conf)WebUiServletModule:serve("/").with(GuiceContainer.class, conf)在这个设置中,我想从负责的每个子项目的代码库定义webapp(/api或/)的每个部分的404响应主体的外观,而不必重新实现Jersey
到目前为止,我已经尝试绑定匹配的资源@Path("/"),@Path("*")和@Path("/*"),但没有这些似乎当我要求被拾起/some/path/that/doesnt/exist
在我的应用程序中,我有以下特征/具体类:
在包中models.daos:
trait UserDAO // UserDAO.scala
class UserDAOImpl @Inject() (app: play.api.Appliction) extends UserDAO // UserDAOImpl.scala
Run Code Online (Sandbox Code Playgroud)
在包中controllers:
class Application @Inject() (userDao: UserDAO) extends Controller
Run Code Online (Sandbox Code Playgroud)
但是,当我运行应用程序时,我得到:
ProvisionException: Unable to provision, see the following errors:
1) No implementation for models.daos.UserDAO was bound.
while locating models.daos.UserDAO
for parameter 0 at controllers.Application.<init>(Application.scala:15)
while locating controllers.Application
for parameter 1 at router.Routes.<init>(Routes.scala:31)
while locating router.Routes
while locating play.api.inject.RoutesProvider
while locating play.api.routing.Router
Run Code Online (Sandbox Code Playgroud)
我是依赖注入的新手,并不知道怎么告诉Guice使用具体的类.
dependency-injection scala guice playframework playframework-2.0
是否可以创建play.api.mvc.Controller具有依赖项注入参数的重载特征?
例如,假设我有几个定制的Action,它们需要注入依赖项AuthorizationService。我想这样写我的控制器:
class UserController extends CustomController {
def getUser(userID: String) = CustomAction {
...
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我无法弄清楚如何创建CustomController特性,从而不需要我在UserController中注入AuthorizationService。Guice有办法做到这一点吗?
我有一个要由 Guice 管理的组件:-
class MyComponent {
Run Code Online (Sandbox Code Playgroud)
我可以在应用程序启动时成功地让 Guice 实例化它,我希望它立即可用,也可以作为单身人士使用。这有效:-
bind(classOf[MyComponent]).asEagerSingleton
Run Code Online (Sandbox Code Playgroud)
我的问题是 -> 我是否需要在类上添加注释,还是在这种情况下它是多余的?
@Singleton
class MyComponent {
Run Code Online (Sandbox Code Playgroud) 我很抱歉这个愚蠢的问题,但我真的在寻找这个问题的答案,但没有得到明确的答案。
我知道 jersey 使用 hk2 作为默认 DI 并且因为 hk2 是性能损失,替代 DI 是 Guice 更好,我们需要使用 guice-hk2-bridge 配置 jersey 以使用 Guice。
问题是为什么?,为什么我们需要配置球衣?,为什么我们不能只使用com.google.inject.Inject代替javax.inject.Inject,只导入com.google.inject包来使用Guice ?
这座桥有什么重要的地方?,我尝试在没有 guice-hk2-bridge 的情况下工作,它对我来说效果很好......我确定我误解了一些东西......
谢谢