Guice

Inject a Class<T> with GIN

有些话、适合烂在心里 提交于 2019-12-12 17:03:51
问题 Is there a way to inject a class type Class<T> in gin? I can't seem to get it working, for example: class GenericFoo<T> { private final Class<T> klass; @Inject public GenericFoo(Class<T> klass) { this.klass = klass; } } class Bar { } with an instance injected somewhere: .. @Inject GenericFoo<Bar> instance; .. and a GinModule containing something along the lines of: bind(new TypeLiteral<Class<Bar>>() {}).to(Bar.class); Thanks 回答1: It's not possible. Reflection is forbidden on the client side,

Create @UnitOfWork-aware proxies of existing objects

一曲冷凌霜 提交于 2019-12-12 16:39:08
问题 I have the following classes: public FooDAO extends AbstractDAO<Foo> { // Dropwizard DAO @Inject FooDAO(SessionFactory sf) { super(sf); } public void foo() { /* use SessionFactory */ } } public class FooService { private final FooDAO fooDAO; // Constructor-injected dependency @Inject FooService (FooDAO fooDAO) { this.fooDAO = fooDAO; } @UnitOfWork public void foo() { this.fooDAO.foo(); System.out.println("I went through FooService.foo()"); } } Now, FooService is not a resource, so Dropwizard

Inject the same component with different parameters with Guice

徘徊边缘 提交于 2019-12-12 14:57:05
问题 I have a Class which represents a graphical Component. This component displays data from the database. Now I have some kind of dashboard, which has 12 of my Components above. Of course I want to reuse this component! So I thought, I could use a Custom Annotation with parameters and pass the parameters to a provider. I found some hack on Stackoverflow (http://stackoverflow.com/questions/5704918/custom-guice-binding-annot...) but to be honest, I did not find any documentation how to implement

How can Guice's @RequestScoped be done with Play 2.6

六眼飞鱼酱① 提交于 2019-12-12 14:04:23
问题 I need the authenticated User, to access REST services. My idea is to have an IdentityService that is RequestScoped and can be injected by all REST service clients. The setup is like: Controller -> BusinessLogic -> RESTServiceClient So to passing the User down would involve lots of code. As Guice's @RequestScoped is not supported (https://groups.google.com/forum/#!msg/play-framework/MEOAhdOJz5Q/-cukqS-ZBAAJ) How can this be done with Play? The Problem is also described here (without a

Java - java.lang.NoClassDefFoundError: com/google/inject/internal/util/$Preconditions

柔情痞子 提交于 2019-12-12 13:52:39
问题 I'm working on an extension for druid that uses jclouds for Rackspace Cloud Files and I encountered a problem with Google guice and I'm not very confident with Java. I already saw this question, but it doesn't seem that there's a conflict in guice versions. This is the code that is being executed: @Provides @LazySingleton public CloudFilesApi getCloudFilesApi(final CloudFilesAccountConfig config) { log.info("Provider: " + config.getProvider()); log.info("Username: " + config.getUserName());

Spring injection bind toInstance

故事扮演 提交于 2019-12-12 12:42:01
问题 Is there a way to bind an injected object to a specific instance using Spring DI similar to Google Guice's bind(MyClass.class).toInstance(myclassobject); 回答1: If the constructor or member variable is annotated with @Autowired , Spring will try to find a bean that matches the type of the Object. You can get similar functionality to the annotation using @Qualifier , for example: bind(MyClass.class).annotatedWith(Names.named("main")).toInstance(myclassobject); would become in Spring: @Autowired

Write JUnit for Guice bindings of Spring beans

蓝咒 提交于 2019-12-12 10:22:36
问题 My library uses both Guice and Spring. And I have class where Spring beans are "bind"-ed for the below usage @ModuleDescriptor( requires = { AnotherModule.class, SpringIntegrationModule.class, }) public class MyModule extends AbstractModule { @Override protected void configure() { bind(Command.class) .annotatedWith(Names.named("commandSpringBean")) .toProvider( SpringIntegration.fromSpring(Command.class, "commandSpringBean")); AnotherModule.exportFunctions(this.binder(), MyGuiceInjector.class

Using Guice Injection in custom Authenticator on Google Cloud Endpoints

和自甴很熟 提交于 2019-12-12 09:47:07
问题 I want to secure a google cloud endpoint with a custom com.google.api.server.spi.config.Authenticator. see this post (Google Cloud Endpoints and user's authentication). For example to authenticate via facebook oauth. The Authenticator must have a default constructor without any params otherwise the Authenticator does not work. So Constructor Injection is not possible like this: @Inject public apiMethod(Log logger, Datastore datastore, MemCacheManager cacheManager) { this.logger = logger; this

Play Framework, Akka, Guice : How to create multiple instances of an actor (which has dependencies) with Guice inside controller?

Deadly 提交于 2019-12-12 05:28:28
问题 There is an example in play framework doc (https://www.playframework.com/documentation/2.6.x/ScalaAkka) that explains how to inject actor (which has dependencies to be injected) into controller: @Singleton class Application @Inject() (@Named("configured-actor") configuredActor: ActorRef) (implicit ec: ExecutionContext) extends Controller { implicit val timeout: Timeout = 5.seconds def getConfig = Action.async { (configuredActor ? GetConfig).mapTo[String].map { message => Ok(message) } } } But

Binding Annotation in spring

▼魔方 西西 提交于 2019-12-12 04:07:02
问题 We are using guice for dependency injection. Now we want to write new project using spring boot. Since we are using Spring Boot, we think it is better to use Spring for dependency injection instead of guice. In guice we used Binding Annoation. This is very useful if we have multiple beans available and it can be injected according the annotations. Similar to that what we have in Spring? Do we need to name the bean accordingly and use it with @Autowire and @Qualifier ? 回答1: You can use