Guice

Dependency injection using Guice with the DAO pattern

大城市里の小女人 提交于 2019-12-02 01:34:19
For a small side project I'm working on I've been trying to implement something of a DAO pattern for my interactions with the DB, and have started using Guice (for my first time) to handle the DI for me. Right now I have this class hierarchy: DAOImpl takes a reference to a class type so my database client (mongo/morphia) can do some initialization work and instantiate a BasicDAO provided by morphia. Here's snippets of the relevant classes: public class DAOImpl<T> implements DAO<T> { private static final Logger LOG = LoggerFactory.getLogger(DAOImpl.class); private static final String ID_KEY =

How to inject dependency into Jackson Custom deserializer

柔情痞子 提交于 2019-12-02 01:12:07
I want to enable a custom jackson deserializer of some fields of type String. The deserializer also needs to be injected with a guice based dependency bean. SampleCode below: public class CustomDeserializer extends StdDeserializer<String> { private SomeDependecy dependency; public StringDeserializer() { this(null); } public StringDeserializer(Class<?> vc) { super(vc); } @Override public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException { return dependency.perform(p.getValueAsString()); } } I cannot register a module based on Class type as

Guice: How to get instance of Singleton without injector or using Constructor Injection

有些话、适合烂在心里 提交于 2019-12-02 00:29:56
I have got an singleton class defined as: @Singleton class MySingletonClass{ .... } I have another class which uses this singleton Class but this class has to be created using new operator. Thus I cannot use constructor injection or setter injection etc. class MyClass { public void method() { // Uses instnace of MySingletonClass } } I can certainly pass an instance of this into the constructor of MyClass but it does not quite a good design from the context of my program. Another solution will be to create an static getInstance method for MySingletonClass so that I can get instance from

Guice: inject different implementation depending on who is getting it?

最后都变了- 提交于 2019-12-01 21:43:20
I have two third-party classes, both of which take an implementation of an Authorizer interface. I need to inject each with a different implementation. If I do an @Provides , how can I implement it so that it provides the implementation required at run time? The provider has no idea who is asking for the injection. In theory I could use @Named , but I can't modify the code being injected. I want to do something like: bind(Authorizer.class).to(ImplA.class).for(SomeClass.class) bind(Authorizer.class).to(ImplB.class).for(SomeOtherClass.class) Obviously, the "for" code doesn't exist, but is there

Google Guice desktop application - how to make it work?

岁酱吖の 提交于 2019-12-01 18:55:23
I have used Guice in my web app without problems and I wanted to use it in desktop app. I am certainly missing one thing - some way to tell my app how to bind everything and know what is what. In web app, I had declaration for that in Application class, how should I do it in my desktop app? Here is relevant code that I am using: public class GuiceModule extends AbstractModule { @Override protected void configure() { // Enable per-request-thread PersistenceManager injection. install(new PersistenceManagerFilter.GuiceModule()); // Business object bindings go here. bind(ProjectQueries.class).to

Why is a lambda expression breaking guice error handling when i try to start jetty?

ぐ巨炮叔叔 提交于 2019-12-01 18:53:19
问题 I face the following problem where i try to start jetty, i get the following exeption: 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

Why is a lambda expression breaking guice error handling when i try to start jetty?

倾然丶 夕夏残阳落幕 提交于 2019-12-01 18:23:21
I face the following problem where i try to start jetty, i get the following exeption: 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

Weblogic 12c : Prefer-web-inf-classes and prefer-application-packages for Jersey

不打扰是莪最后的温柔 提交于 2019-12-01 17:52:15
I have to use both (oddly enough ..) " prefer-web-inf-classes " and " prefer-application-packages properties of weblogic.xml on a Weblogic 12c Server (12.2.1) It is REST application based on Jersey 1.9. * ( Jersey 1.x JAX-RS RI) and Guice. 1. Why use :prefer-web-inf-classes If you have more than one WAR you have to place at the level of war/lib the libraries for guice-jersey / guice , other way you get an Multibindings Error . It must be indicate also the prefer-web-inf-classes to true . This way works properly! I have tried to work in the same way using prefer-application-packages with

Guice dynamic inject with custom annotation

三世轮回 提交于 2019-12-01 16:44:57
问题 I have some resource, but I can not iterator it and bind them all, I have to use the key to request the resource.So, I have to dynamic inject. I define an annotation like @Target({ METHOD, CONSTRUCTOR, FIELD }) @Retention(RUNTIME) @Documented @BindingAnnotation public @interface Res { String value();// the key of the resource } use like this public class Test { @Inject @Res("author.name") String name; @Inject @Res("author.age") int age; @Inject @Res("author.blog") Uri blog; } I have to handle

Guice dynamic inject with custom annotation

强颜欢笑 提交于 2019-12-01 16:38:32
I have some resource, but I can not iterator it and bind them all, I have to use the key to request the resource.So, I have to dynamic inject. I define an annotation like @Target({ METHOD, CONSTRUCTOR, FIELD }) @Retention(RUNTIME) @Documented @BindingAnnotation public @interface Res { String value();// the key of the resource } use like this public class Test { @Inject @Res("author.name") String name; @Inject @Res("author.age") int age; @Inject @Res("author.blog") Uri blog; } I have to handle the injection annotated by @Res and I need to know the inject field and the annotation. Is this