guice-3

How can i combine Guice and Mapstruct?

若如初见. 提交于 2021-01-28 05:50:34
问题 I'm using jersey and Guice DI and I want to use Mapstruct interfaces with @Inject annotation. So is there some way to force Guice to autowire Mapstruct interface implementations ? 回答1: You can configure the implementations of the Mappers to be annotated with JSR 330 annotation by using @Mapper(componentModel = "jsr330") . You can find more information in the reference documentation. You can then bind the Mapper interface with the implementation class in your modules. One way to bind them is

Binding a guava supplier using guice

一笑奈何 提交于 2020-12-26 11:04:50
问题 I want do a binding like this, bind(Supplier<TestClass>).toProvider(Provider<Supplier<TestClass>>).in(Singleton.class); The provider is returned by an external function so, inside the toProvider() , I call that function and it returns Provider <Supplier<TestClass>> . The supplier is from guava, the reason to do this kind of thing is, there is a file associated with the TestClass and I need to read that file and assign those values to the respective fields of TestClass. And that file change at

Binding a constructor argument based on the Annotation of the class

被刻印的时光 ゝ 提交于 2020-01-17 05:14:20
问题 I have an interface: InterfaceA . I have a class: ConcreteA . I also have two annotations: @AnnotA and @AnnotB . I have done the following bindings: bind(InterfaceA).annotatedWith(AnnotA).to(ConcreteA); bind(InterfaceA).annotatedWith(AnnotB).to(ConcreteA); Next, class ConcreteA has a constructor that takes a String argument called hostName . class ConcreteA { @Inject public ConcreteA(@Named("hostName") hostName) { } ... <rest of class> } I need code to describe the following: If ConcretaA is

Jersey, Guice using non-root request paths

流过昼夜 提交于 2020-01-06 14:19:13
问题 I'm using Jersey 1.11 over Guice 3.0 on Tomcat 6.0.32 in a standard configuration: configureServlets() { filter("/ws/*").through(GuiceContainer.class); } And a simple resource class: @Path("/resource") public class Resource { ... } Given that, I would suppose that accessing "/ws/resource" would work; but actually no resources are found. The problem seems to lie in the request path not being computed correctly. As a workaround I have set the parameter PROPERTY_FILTER_CONTEXT_PATH to /ws ,

Guice, Injecting TypeLiteral<T> when using @AssistedInject

杀马特。学长 韩版系。学妹 提交于 2019-12-24 02:17:05
问题 This works: 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(); } } Guice automatically injects the TypeLiteral representing String when I do: @Inject SomeGenericType<String> foo; But when trying the same thing with Assisted Inject: public static interface FooFactory<T> { Foo<T> create(String name); } public static class Foo<T> {

guice injection in static variable

女生的网名这么多〃 提交于 2019-12-23 07:31:38
问题 I Have doubt about guice injection. Is it possible to inject a @named variable value to a static variable? I have tried @Provides @Named("emp.id") public Integer getEmpId() { return 2; } and tried to inject this value to static variable such as @Inject @Named("emp.id") private static Integer id; But the id return value null, When I removed static modifier the id gave value 1. What is really happening here? 回答1: Guice does not inject static fields by design. You can request static injection

How to use Guice's Injector?

こ雲淡風輕ζ 提交于 2019-12-23 07:26:30
问题 I'm in process of learning Guice and I don't clearly understand how to use Injector instance. It's better to create Injector instance once on application bootstrap, and make it public singleton? And is it true that we always must use Injector#getInstance(SomeClass.class) to get classes where we putted Guice's @Inject annotations? 回答1: You should not pass the injector around as a global singleton. Have you looked at: https://github.com/google/guice/wiki/GettingStarted? Note that

How to use Guice's Injector?

北战南征 提交于 2019-12-23 07:25:05
问题 I'm in process of learning Guice and I don't clearly understand how to use Injector instance. It's better to create Injector instance once on application bootstrap, and make it public singleton? And is it true that we always must use Injector#getInstance(SomeClass.class) to get classes where we putted Guice's @Inject annotations? 回答1: You should not pass the injector around as a global singleton. Have you looked at: https://github.com/google/guice/wiki/GettingStarted? Note that

Play Framework: Dependency Inject Action Builder

白昼怎懂夜的黑 提交于 2019-12-17 23:27:34
问题 since Play Framework 2.4 there is the possibility to use dependency injection (with Guice). Before I used objects (for example AuthenticationService ) in my ActionBuilders: object AuthenticatedAction extends ActionBuilder[AuthenticatedRequest] { override def invokeBlock[A](request: Request[A], block: (AuthenticatedRequest[A]) => Future[Result]): Future[Result] = { ... AuthenticationService.authenticate (...) ... } } Now AuthenticationService is not an object anymore, but a class. How can I

Proper structure for dependency injection (using Guice)

安稳与你 提交于 2019-12-11 02:38:48
问题 I would like some suggestions and feedback on the best way to structure dependency injection for a system with the structure described below. I'm using Guice and thus would prefer solutions centered around it's annotation-based declarations, not XML-heavy Spring-style configuration. Consider a set of similar objects, Ball, Box, and Tube , each dependent on a Logger , supplied via the constructor. (This might not be important, but all four classes happen to be singletons --- of the application