Guice

Dependency Injection in OSGI environments

不羁岁月 提交于 2019-12-02 16:12:47
First some background: I'm working on some webapp prototype code based on Apache Sling which is OSGI based and runs on Apache Felix. I'm still relatively new to OSGI even though I think I've grasped most concepts by now. However, what puzzles me is that I haven't been able to find a "full" dependency injection (DI) framework. I've successfully employed rudimentary DI using Declarative Services (DS). But my understanding is that DS are used to reference -- how do I put this? -- OSGI registered services and components together. And for that it works fine, but I personally use DI frameworks like

Scala Dependency injection with generic class

…衆ロ難τιáo~ 提交于 2019-12-02 10:46:09
Using Guice in Scala, I'm trying to reproduce the following Java code. Foo interface and class declaration: public interface Foo[T] {} public class FooImpl[T] implements Foo[T] {} Guice binding code: bind(Foo.class).to(FooImpl.class); And one use example would be; @Inject public class Bar(Foo<String> foo) {} In scala, my first bet was: bind(classOf[Foo]).to(classOf[FooImpl]) But it's complaining about 'Type Foo takes type parameter' How do I achieve this in scala? Thank you Your question has an error and thus it allows you for a wrong answer. Let's first fix your concept idea. Having trait

Guice ignoring @Nullable on injected constructor parameter

℡╲_俬逩灬. 提交于 2019-12-02 10:23:42
问题 I'm using Guice (v 3.0) and have a value that is being injected into a constructor. This value can be null, so I have annotated the parameter in the constructor with @Nullable (from javax.annotations). public MyClass(Parameter1 p1, @Nullable Parameter2 p2) { } However, Guice is complaining with provision errors when it comes to initialise the class: parameter 2 of com.abc.MyClass.<init>() is not @Nullable I don't understand why this is not working, is there something else I need to do? 回答1:

Embedded Jetty handling urls to serve content

冷暖自知 提交于 2019-12-02 10:03:44
I am using embedded Jetty with Guice and am wondering the best way to handle serving my single page application. I wish for Jetty to handle requests like so (in priority order): /socket must be handled by the websocket servlet /fs/read/* , anything that matches this url I need to be handled by a custom servlet /* , anything that matches this url should be served from the /web on the classpath of my Java application assuming it isn't handled by the above. If the resource does not exist, then it serves /web/index.html Now I am wondering the best way to handle this? It seems heavy handed to use a

Dynamically generating injections by annotation

試著忘記壹切 提交于 2019-12-02 06:48:45
Suppose I have a class that looks like this: public class MyClass { @Inject public MyClass(@Foo("whatever") Bar dependency) { // ... } } And I wanted to have some custom logic that can see we're injecting an object of type Bar with an annotation of type @Foo("whatever") and construct a corresponding Bar object...something like a Guice Provider , but that gets more context information about the injection site. Does Guice let me do something like that? What you're describing isn't possible through normal Guice: Providers are intended to be zero-argument pure functions and there's no way to plumb

Configure an object provided by a Guice Module

谁说胖子不能爱 提交于 2019-12-02 05:57:21
I have a Module that provides a JDBI DBI instance like this: @Provides @Singleton DBI dbi(DataSource dataSource) { return new DBI(dataSource); } In another module I want to call some initialization methods on that DBI instance (configuring support for particular data types). It's not appropriate logic to put in the JDBI module itself as it's application specific not general to any application using JDBI. Is there a hook for me to do that kind of "extra" configuration? I tried using the bindListener method but it seems like that's not invoked for objects provided in this way. The Guice

Publishing JAX-WS Webservice with Guice in a Servlet Application

大兔子大兔子 提交于 2019-12-02 04:12:21
问题 We are currently porting an existing JBoss EJB application to a pure servlet solution which is supposed to run in a Jetty (we're currently using version 6, but the version is mostly irrelevant) and which uses Guice for dependency injection and AOP. Despite the huge complexity we've been quite succesfully so far. The persistence layer and most of our services are up and running, including JAX-RS REST services. However, when we started porting our existing JAX-WS SOAP services we ran into

How to inject dependency into Jackson Custom deserializer

*爱你&永不变心* 提交于 2019-12-02 03:54:26
问题 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 {

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

眉间皱痕 提交于 2019-12-02 02:44:58
问题 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

Publishing JAX-WS Webservice with Guice in a Servlet Application

大城市里の小女人 提交于 2019-12-02 01:46:19
We are currently porting an existing JBoss EJB application to a pure servlet solution which is supposed to run in a Jetty (we're currently using version 6, but the version is mostly irrelevant) and which uses Guice for dependency injection and AOP. Despite the huge complexity we've been quite succesfully so far. The persistence layer and most of our services are up and running, including JAX-RS REST services. However, when we started porting our existing JAX-WS SOAP services we ran into difficulties. We already spent about a day searching the web and it seems that many people ran into the same