标签: guice-3

如何使用Guice的AssistedInject?

我已阅读https://github.com/google/guice/wiki/AssistedInject,但它没有说明如何传递AssistedInject参数的值.injector.getInstance()调用会是什么样的?

java guice guice-3

79
推荐指数
1
解决办法
3万
查看次数

Play Framework:Dependency Inject Action Builder

从Play Framework 2.4开始,就有可能使用依赖注入(使用Guice).

AuthenticationService在我的ActionBuilders中使用对象之前(例如):

object AuthenticatedAction extends ActionBuilder[AuthenticatedRequest] {
  override def invokeBlock[A](request: Request[A], block: (AuthenticatedRequest[A]) => Future[Result]): Future[Result] = {
    ...
    AuthenticationService.authenticate (...)
    ...
  }
}
Run Code Online (Sandbox Code Playgroud)

现在AuthenticationService不再是一个对象,而是一个类.我怎么还能使用AuthenticationService我的ActionBuilder

dependency-injection scala guice playframework guice-3

16
推荐指数
2
解决办法
2846
查看次数

guice辅助注射工厂中通用返回类型的问题

到目前为止,我成功地使用了google guice 2.在迁移到guice 3.0时,我遇到了辅助注入工厂的麻烦.假设以下代码

public interface Currency {}
public class SwissFrancs implements Currency {}

public interface Payment<T extends Currency> {}
public class RealPayment implements Payment<SwissFrancs> {
    @Inject
    RealPayment(@Assisted Date date) {}
}

public interface PaymentFactory {
    Payment<Currency> create(Date date);
}

public SwissFrancPaymentModule extends AbstractModule {
    protected void configure() {
        install(new FactoryModuleBuilder()
             .implement(Payment.class, RealPayment.class)
             .build(PaymentFactory.class));
    }
}
Run Code Online (Sandbox Code Playgroud)

在创建注入器时,我得到以下异常:

com.google.inject.CreationException: Guice creation errors:

1) Payment<Currency> is an interface, not a concrete class.
   Unable to create AssistedInject factory. while locating Payment<Currency>
   at PaymentFactory.create(PaymentFactory.java:1) …
Run Code Online (Sandbox Code Playgroud)

java guice guice-3

13
推荐指数
1
解决办法
3278
查看次数

如何使用Guice的注射器?

我正在学习Guice,我不清楚如何使用Injector实例.最好Injector在应用程序引导程序上创建一次实例,并使其成为公共单例?

我们总是必须使用Injector#getInstance(SomeClass.class)我们推出Guice @Inject注释的课程,这是真的吗?

java guice guice-3

10
推荐指数
1
解决办法
1万
查看次数

如何在测试中验证Guice范围的用法?

如果某些Guice范围使用不正确,我有一些测试我想失败.例如,a @Singleton不应该有任何@RequestScoped@TestScoped依赖(Provider<>当然,s是可以的).

在生产中,这部分地得到了解决,因为在输入范围之前将构建热切的单体,从而产生OutOfScopeExceptions.但在开发过程中,单例将在范围内懒洋洋地创建,并且没有明显的问题.

从这 两个未解决的问题来看,似乎没有简单的内置方法来做到这一点.我可以使用SPI实现这一目标吗?我尝试使用a TypeListener但不清楚如何获取给定类型的依赖项.

java guice guice-3

10
推荐指数
1
解决办法
1471
查看次数

guice注入静态变量

我对guice注射有疑问.是否可以将@named变量值注入静态变量?

我试过了

@Provides
@Named("emp.id")
public Integer getEmpId() {
   return 2;
}
Run Code Online (Sandbox Code Playgroud)

并试图将此值注入静态变量,如

 @Inject
 @Named("emp.id")
 private static Integer id;
Run Code Online (Sandbox Code Playgroud)

id返回值为null,当我删除静态修饰符时,id给出了值1.

这里到底发生了什么?

java guice guice-3

9
推荐指数
1
解决办法
1万
查看次数

在AWS Lambda中集成Google Guice

我正在尝试将Google Guice集成到AWS Lambda中,但由于某些原因,注入效果不佳.每当我试着打电话时,它都会给我null

处理程序代码:

public class FirstLamdba implements RequestHandler<Request, Object>{   

        private UserService userService;

        @Inject
        public void seUserService(UserService userService) {
            this.userService = userService;
        }

        public Object handleRequest(Request request, Context context){

            userService.persistData();
}
Run Code Online (Sandbox Code Playgroud)

UserService

public interface UserService {
    List<String> persistData();
}
Run Code Online (Sandbox Code Playgroud)

UserServiceImpl

public class UserServiceImpl implements UserService{


    @Override
    public List<String> persistData() {

        System.out.println("***Working*********");
}
Run Code Online (Sandbox Code Playgroud)

绑定类:

public class MessageGuiceModule extends AbstractModule
{
  protected void configure() {

    bind(UserService.class).to(UserServiceImpl.class);
  }
}
Run Code Online (Sandbox Code Playgroud)

测试类:

 @Test
        public void testLambdaFunctionHandler() {
Request request = new Request();
            request.setName("Name");
            FirstLamdba handler …
Run Code Online (Sandbox Code Playgroud)

java guice guice-3 java-8 aws-lambda

6
推荐指数
2
解决办法
3932
查看次数

Guice 3.0-启动时出现ArrayIndexOutOfBoundsException吗?

为什么Guice 3.0针对错误的配置组件(例如,缺少@Inject)抛出此异常而不是格式化消息?

Exception in thread "main" com.google.inject.internal.util.$ComputationException: java.lang.ArrayIndexOutOfBoundsException: 16640
    at com.google.inject.internal.util.$MapMaker$StrategyImpl.compute(MapMaker.java:553)
    at com.google.inject.internal.util.$MapMaker$StrategyImpl.compute(MapMaker.java:419)
    at com.google.inject.internal.util.$CustomConcurrentHashMap$ComputingImpl.get(CustomConcurrentHashMap.java:2041)
    at com.google.inject.internal.util.$StackTraceElements.forMember(StackTraceElements.java:53)
    at com.google.inject.internal.Errors.formatInjectionPoint(Errors.java:716)
    at com.google.inject.internal.Errors.formatSource(Errors.java:678)
    at com.google.inject.internal.Errors.format(Errors.java:555)
    at com.google.inject.ConfigurationException.getMessage(ConfigurationException.java:70)
    at java.lang.Throwable.getLocalizedMessage(Throwable.java:391)
    at java.lang.Throwable.toString(Throwable.java:480)
    at java.lang.String.valueOf(String.java:2982)
    at java.io.PrintStream.println(PrintStream.java:821)
    at java.lang.Throwable$WrappedPrintStream.println(Throwable.java:748)
    at java.lang.Throwable.printStackTrace(Throwable.java:655)
    at java.lang.Throwable.printStackTrace(Throwable.java:643)
    at java.lang.Throwable.printStackTrace(Throwable.java:634)
    at hu.daniel.hari.exercises.cleanarchitecture.payrollcasestudy.adapters.primary.ui.impl.swing._2.SwingUIMain2.<init>(SwingUIMain2.java:40)
    at hu.daniel.hari.exercises.cleanarchitecture.payrollcasestudy.adapters.primary.ui.impl.swing._2.SwingUIMain2.main(SwingUIMain2.java:17)
Caused by: java.lang.ArrayIndexOutOfBoundsException: 16640
    at com.google.inject.internal.asm.$ClassReader.readClass(Unknown Source)
    at com.google.inject.internal.asm.$ClassReader.accept(Unknown Source)
    at com.google.inject.internal.asm.$ClassReader.accept(Unknown Source)
    at com.google.inject.internal.util.$LineNumbers.<init>(LineNumbers.java:62)
    at com.google.inject.internal.util.$StackTraceElements$1.apply(StackTraceElements.java:36)
    at com.google.inject.internal.util.$StackTraceElements$1.apply(StackTraceElements.java:33)
    at com.google.inject.internal.util.$MapMaker$StrategyImpl.compute(MapMaker.java:549)
    ... 17 more
Run Code Online (Sandbox Code Playgroud)

我initating代码是:

Injector injector = Guice.createInjector(new SwingUIModule(useCaseFactory));
injector.getInstance(MainFrameUI.class).show();
Run Code Online (Sandbox Code Playgroud)

guice-3

5
推荐指数
2
解决办法
3798
查看次数

当我尝试启动jetty时,为什么lambda表达式会破坏guice错误处理?

我遇到以下问题,我尝试启动jetty,我得到以下的例外:

Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin:2.17:test failed: There was an error in the forked process
[ERROR] org.apache.maven.surefire.testset.TestSetFailedException: java.lang.RuntimeException: com.google.inject.internal.util.$ComputationException: java.lang.ArrayIndexOutOfBoundsException: 51966
[ERROR] at org.apache.maven.surefire.common.junit4.JUnit4RunListener.rethrowAnyTestMechanismFailures(JUnit4RunListener.java:206)
[ERROR] at org.apache.maven.surefire.junitcore.JUnitCoreWrapper.createRequestAndRun(JUnitCoreWrapper.java:114)
[ERROR] at org.apache.maven.surefire.junitcore.JUnitCoreWrapper.executeEager(JUnitCoreWrapper.java:85)
[ERROR] at org.apache.maven.surefire.junitcore.JUnitCoreWrapper.execute(JUnitCoreWrapper.java:54)
[ERROR] at org.apache.maven.surefire.junitcore.JUnitCoreProvider.invoke(JUnitCoreProvider.java:134)
[ERROR] at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:200)
[ERROR] at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:153)
[ERROR] at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:103)
[ERROR] Caused by: java.lang.RuntimeException: com.google.inject.internal.util.$ComputationException: java.lang.ArrayIndexOutOfBoundsException: 51966
[ERROR] at org.apache.maven.surefire.junitcore.TestSet.replay(TestSet.java:100)
[ERROR] at org.apache.maven.surefire.junitcore.ConcurrentRunListener.testSetCompleted(ConcurrentRunListener.java:82)
[ERROR] at org.apache.maven.surefire.junitcore.JUnitCoreRunListener.testRunFinished(JUnitCoreRunListener.java:73)
[ERROR] at org.junit.runner.notification.RunNotifier$2.notifyListener(RunNotifier.java:95)
[ERROR] at org.junit.runner.notification.RunNotifier$SafeNotifier.run(RunNotifier.java:61)
[ERROR] at org.junit.runner.notification.RunNotifier.fireTestRunFinished(RunNotifier.java:92)
[ERROR] at org.junit.runner.JUnitCore.run(JUnitCore.java:161)
[ERROR] at org.junit.runner.JUnitCore.run(JUnitCore.java:138)
[ERROR] at org.apache.maven.surefire.junitcore.JUnitCoreWrapper.createRequestAndRun(JUnitCoreWrapper.java:113)
[ERROR] …
Run Code Online (Sandbox Code Playgroud)

java lambda guice guice-3 java-8

4
推荐指数
1
解决办法
5424
查看次数

Guice - 创建与java.lang.class的绑定

是否有任何光滑的方式(如果有的话)Guice以某种方式将类类型绑定到接口?我不是指类的实例,而是实际的java.lang.class类型本身.

即(显然不起作用,但告诉我我追求的是什么):

bind(MyInterface.class).to(Class<MyImplementation>)
Run Code Online (Sandbox Code Playgroud)

我知道一开始似乎不可能,但我不知道是否有任何技巧可以用来做到这一点.想到的是将类类型包装在实际的实例化对象或其他东西中,但这似乎是最后的手段.

任何想法将不胜感激.谢谢!

java guice guice-3

3
推荐指数
1
解决办法
115
查看次数

guice 4.0向后兼容3.x吗?

guice 4.0向后兼容3.x吗?无法从发行说明或常见问题解答中找到它...

如果没有,是否有某个兼容性问题列表?

guice guice-3

3
推荐指数
1
解决办法
917
查看次数

Guice,在使用@AssistedInject时注入TypeLiteral &lt;T&gt;

这有效:

public static class SomeGenericType<T> {
    private TypeLiteral<T> type; 

    @Inject
    public SomeGenericType(TypeLiteral<T> type) {
        this.type = type; 
    }

    public Class<? super T> getType() {
        return type.getRawType();
    }
}
Run Code Online (Sandbox Code Playgroud)

当我这样做时,Guice自动注入表示字符串的TypeLiteral:

@Inject SomeGenericType<String> foo;
Run Code Online (Sandbox Code Playgroud)

但是在使用Assisted Inject尝试相同的操作时:

public static interface FooFactory<T> {
    Foo<T> create(String name);
}

public static class Foo<T> {

    @AssistedInject
    public Foo(TypeLiteral<T> type, @Assisted String name) {
        ....
Run Code Online (Sandbox Code Playgroud)

我的模块如下所示:

public static class TestMod extends AbstractModule {
    @Override
    protected void configure() {
        install(new FactoryModuleBuilder().build(new TypeLiteral<FooFactory<String>>(){}));
    }   
}
Run Code Online (Sandbox Code Playgroud)

安装模块时出现异常:

TypeLiteral<T> cannot be used …
Run Code Online (Sandbox Code Playgroud)

guice guice-3

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