标签: guice

org.restlet.ext.guice 的状态是什么?

在restlet中寻找guice支持我遇到了这篇文章 - http://wiki.restlet.org/developers/172-restlet/257-restlet/284-restlet.html

但是 2.0(稳定版)和 2.1(候选发布版)都不包含 org.restlet.ext.guice.jar

所以,我的问题是它的状态是什么?我在哪里可以下载它?我正在使用restlet 2.0

谢谢。

restlet guice restlet-2.0

2
推荐指数
1
解决办法
784
查看次数

com.sun.istack.internal.Nullable 注释和 Guice

当 Guice 说它可以识别这里的任何 @Nullable 注释(http://code.google.com/p/google-guice/wiki/UseNullable)时,这是否意味着我可以使用 com.sun.istack.internal.Nullable 提供的Java 7 运行时成功了吗?

包名是 com.sun.istack.internal 的事实让我相信我可能不应该使用它,但它使用起来非常方便......

java null nullable guice

2
推荐指数
1
解决办法
7454
查看次数

如何使用@Named 但相同的接口类型绑定两个多绑定器?

我们有一个 Multibinder 并且想要两个,所以我们需要 @Named 注释所以我们可以做

@Inject @Named("work")
private Set<Runnable> work;
@Inject @Named("otherWork");
private Set<Runnable> otherWork;
Run Code Online (Sandbox Code Playgroud)

有没有办法将 Multibinder 与名称绑定?

谢谢,院长

guice

2
推荐指数
1
解决办法
1391
查看次数

How do I substitute Guice modules with fake test modules for unit tests?

Here's how we are using Guice in a new application:

public class ObjectFactory {
  private static final ObjectFactory instance = new ObjectFactory();
  private final Injector injector;

  private ObjectFactory() throws RuntimeException {
    this.injector = Guice.createInjector(new Module1());
  }

  public static final ObjectFactory getInstance() {
    return instance;
  }

  public TaskExecutor getTaskExecutor() {
    return injector.getInstance(TaskExecutor.class);
  }
}
Run Code Online (Sandbox Code Playgroud)

Module1 defines how the TaskExecutor needs to be constructed.

In the code we use ObjectFactory.getInstance().getTaskExecutor() to obtain and the instance of TaskExecutor.

In unit tests we …

java spring unit-testing guice

2
推荐指数
1
解决办法
2051
查看次数

如何对具有字段注入的类进行单元测试(使用 guice)

被测试的类如下所示:

public class SomeAdapter {

@Inject
HttpService httpService;

@Inject
Configuration configuration;

public SomeAdapter()
{
    GuiceInjector.getInjector().injectMembers(this);
}

public String getBaseUrl()
{
    return configuration.getProtocol()+ "://" + some.getServer() + ":" + configuration.getPort();
  }
}
Run Code Online (Sandbox Code Playgroud)

我已经尝试过来自mockito框架的InjectMocks,但它似乎并不可靠。是否有必要创建一个扩展AbstractModule的单独的测试模块?

unit-testing dependency-injection guice junit4

2
推荐指数
1
解决办法
3382
查看次数

玩!Scala:测试时如何禁用在应用程序开始时加载的actor模块?

我在 Play 开始时加载了一些演员!2.5 这样的应用:

class Module extends AbstractModule with AkkaGuiceSupport with ScalaModule {
  override def configure() = {
    bindActor[MainSupervisor]("main-supervisor")
  }
}
Run Code Online (Sandbox Code Playgroud)

问题是,当我运行测试时,我从加载的参与者(以及整个集群和远程系统)那里得到了很多日志(和不必要的调用),比如

[INFO ] a.r.Remoting: Starting remoting 
[INFO ] a.r.Remoting: Remoting started; listening on addresses :[akka.tcp://application@127.0.0.1:41496] 
[INFO ] a.r.Remoting: Remoting now listens on addresses: [akka.tcp://application@127.0.0.1:41496] 
Run Code Online (Sandbox Code Playgroud)

例如,我测试了一个不需要任何演员的类,但我找不到任何方法来禁用它们(甚至更好的是整个演员系统)。

我尝试过的是:

lazy val appWithoutActorsBuilder = new GuiceApplicationBuilder()
  .disable[ActorSystem]
  .disable[MainSupervisor]
  .build()
lazy val injectorWithoutActors = appWithoutActorsBuilder.injector
lazy val wSClientWithoutActors = injectorWithoutActors.instanceOf[WSClient]
lazy val ec = scala.concurrent.ExecutionContext.Implicits.global
lazy val facebookAPI = new FacebookAPI(wSClientWithoutActors, ec)
Run Code Online (Sandbox Code Playgroud)

但是当我测试 FacebookAPI …

scala guice akka playframework

2
推荐指数
1
解决办法
993
查看次数

Guice 中基于 AOP 的日志记录

我正在尝试在 Google - Guice 中实现基于 AOP 的日志记录。我已经用过MethodInterceptor这个但它不起作用。我在 Spring 中通过定义切入点来使用相同的方法。那里一切都运转良好。

基于 AOP 的日志记录的 Spring 代码 -

@Aspect
public class LoggingAspect {

 private static Logger logger = LoggerFactory.getLogger(LoggingAspect.class);

   @Around("requiredLog()")
   public Object bentoBoxAround(ProceedingJoinPoint proceedingJoinPoint) {

      Object returnValue = null;
      try {

          logger.info("Entered into the method -> " + proceedingJoinPoint.getSignature().toShortString()
                  + " and input arguments are -> " + Arrays.asList(proceedingJoinPoint.getArgs()));
          returnValue = proceedingJoinPoint.proceed();
          logger.info("Method Execution over !! " + proceedingJoinPoint.getSignature().toShortString());
      } catch (Throwable e) {
          logger.error("Method has an exception " …
Run Code Online (Sandbox Code Playgroud)

java logging aop aspectj guice

2
推荐指数
1
解决办法
5123
查看次数

使用 Play 框架设置 Akka 集群

我目前正在尝试使用自动发现服务来实现集群播放 + akka 实现。然而,我似乎遇到了游戏中包含的 Guice DI 加载器的问题。他们的文档摘录指出:

\n\n

https://www.playframework.com/documentation/2.5.x/ScalaAkka#Integrating-with-Akka

\n\n
\n

虽然我们建议您使用内置的 Actor 系统,因为它设置了所有内容,例如正确的类加载器、生命周期挂钩等,但没有什么可以阻止您使用自己的 Actor 系统。但重要的是要确保您执行以下操作:

\n\n

注册一个停止钩子以在 Play 关闭时关闭 Actor 系统\n 从 Play 环境传递正确的类加载器,否则 Akka 将\xe2\x80\x99 无法找到你的应用程序类

\n\n

确保您使用 play.akka.config 更改 Play 读取 akka 配置的位置,或者您不从默认 akka 配置读取 akka 配置,因为这将导致导致问题,例如当系统尝试绑定到相同的远程端口时

\n
\n\n

我已经完成了他们推荐的上述配置,但是我似乎无法绕过仍然从BuiltInModule绑定它的内部ActorSystemProvider:

\n\n
class BuiltinModule extends Module {\ndef bindings(env: Environment, configuration: Configuration): Seq[Binding[_]] = \n\n    {\n        def dynamicBindings(factories: ((Environment, Configuration) => Seq[Binding[_]])*) = {\n          factories.flatMap(_(env, configuration))\n        }\n\n        Seq(\n          bind[Environment] to env,\n          bind[ConfigurationProvider].to(new ConfigurationProvider(configuration)),\n          bind[Configuration].toProvider[ConfigurationProvider],\n          bind[HttpConfiguration].toProvider[HttpConfiguration.HttpConfigurationProvider],\n\n          // Application lifecycle, bound …
Run Code Online (Sandbox Code Playgroud)

scala guice akka playframework akka-cluster

2
推荐指数
1
解决办法
3947
查看次数

Google Guice 每次都返回新实例

我是依赖注入的新手,最近不得不学习 Spring 和 Guice。下面的问题可能很蹩脚。

Spring 允许您使用 @Scope("prototype") 注释创建原型对象,这意味着每次都返回一个新对象

例如在我的弹簧容器中:-

@Scope("prototype")
@Bean
A getA(){
   return new A();
}
Run Code Online (Sandbox Code Playgroud)

并且在@autowired 的所有引用中注入了一个新对象A。

class B {
  @Autowired
  A objA;
}
Run Code Online (Sandbox Code Playgroud)

但是,在 guice 注入器中,正在注入相同的实例。看起来 guice 只提供单例、请求或会话范围https://github.com/google/guice/wiki/Scopes。我的应用程序不完全是一个 Web 服务,我想知道如何在任何地方注入对象的新实例。我读过可以使用提供程序,但找不到任何蹩脚/简单的例子来理解。

我如何将 A 的新实例注入 B 或任何其他类?使用 Guice,目前我只能在 guice 容器中注入具有以下所有类的相同实例

bind(A.Class).toInstance(new A());
new B(); // or create with other ways 
Run Code Online (Sandbox Code Playgroud)

dependency-injection guice

2
推荐指数
1
解决办法
6169
查看次数

如何使用 Scala Guice 绑定一个使用单子类型参数扩展 Trait 的类?

我需要绑定这个特征的实现:

trait ClientRepository[F[_]] {
  def list(): F[Iterable[ClientDTO]]
}
Run Code Online (Sandbox Code Playgroud)

对于这个实现:

import cats.effect.IO

@Singleton
class ClientRepositoryImpl @Inject()(db: OldDataBase, c: IOContextShift)
    extends ClientRepository[IO] {

  override def list(): IO[Iterable[ClientDTO]] = ???
}
Run Code Online (Sandbox Code Playgroud)

我正在使用 Scala Play!v2.7.2 和 Scala v2.12.8,以及scala-guicev4.2.1。为了将特征绑定到它的实现,我想在我的中做类似的事情Module.scala

class Module(environment: Environment, configuration: Configuration)
    extends AbstractModule
    with ScalaModule {

  override def configure() = {

    bind[ClientRepository].to[ClientRepositoryImpl[IO]].in[Singleton]

  }
}
Run Code Online (Sandbox Code Playgroud)

我得到的错误是:

[error] app/Module.scala:37:9: kinds of the type arguments (ClientRepository) do not conform to the expected kinds of the type parameters (type T).
[error] ClientRepository's …
Run Code Online (Sandbox Code Playgroud)

scala guice playframework

2
推荐指数
1
解决办法
519
查看次数