Didums is a facade for Dependency Injection frameworks that implement JSR330.
Dependency Injection frameworks (e.g. Guice, Weld, HK2) have existed for years with a variety of APIs for configuring and injecting. Didums serves as a simple facade or abstraction for these DI frameworks allowing the end user to plug in the desired DI framework at deployment time.
Even if full blown Dependency Injection is not needed, Didums provides a flexible Factory pattern for binding implementations to interfaces.
Add didums-core
dependency:
<project>
....
<dependency>
<groupId>com.github.bordertech.didums</groupId>
<artifactId>didums-core</artifactId>
<version>1.0.5</version>
</dependency>
....
</project>
Create an interface Foo
:
package my.example;
public interface Foo {
}
Create an implementation FooImpl
:
package my.example;
public class FooImpl implements Foo {
}
Use Didums to create the instance of Foo
:
Foo foo = Didums.getService(Foo.class);
However, as the default implementation is already available, it can be provided on the getService
method:
Foo foo = Didums.getService(Foo.class, FooImpl.class);
The runtime implementation can be set or overridden via Factory Binding or Didums Binding.
Didums can be used without a DI backing framework as it provides a fallback Factory pattern. The only JSR330 annotation supported is Singleton
.
Didums will check for a runtime property to get the required implementation class name.
The property used is the prefix bordertech.factory.impl
combined with the interface class name:
bordertech.factory.impl.my.example.Foo=my.example.FooImpl
Refer to Config on how to set runtime properties.
Using a backing DI Framework via a provider allows the full range of JSR330 annotations to be used.
Add a DI Provider
dependency:
<project>
....
<dependency>
<groupId>com.github.bordertech.didums</groupId>
<artifactId>didums-hk2</artifactId>
<version>1.0.4</version>
</dependency>
....
</project>
When using a DI provider, a DidumsBinder
implementation can be used to bind interfaces and implementations:
package my.example;
public class MyDidumsBinder implements DidumsBinder {
@Override
public void configBindings(final DidumsProvider provider) {
provider.bind(Foo.class, FooImpl.class, false);
provider.bind(AnotherFoo.class, AnotherFooImpl.class, false);
}
}
Set the DidumsBinder
factory property:
bordertech.factory.impl.com.github.bordertech.didums.DidumsBinder=my.example.MyDidumsBinder
bordertech.factory.impl.com.github.bordertech.didums.DidumsBinder+=my.example.AnotherDidumsBinder
As shown in the example above, multiple DidumsBinder
implementations can be set.
Projects can also use the Binding method supported by its selected DI framework.
If an interface has not been bound via a DidumsBinder
, then Didums will fallback to the Factory pattern binding.
Refer to Config on how to set runtime properties.
Implementations of the DidumsProvider
interface are the bridge between the Didums API and the backing DI framework.
If creating a custom provider, set the DidumsProvider
implementation via the factory property:
bordertech.factory.impl.com.github.bordertech.didums.DidumsProvider=my.didums.DidumsProviderImpl
Or add a predefined DI Provider
dependency:
<project>
....
<dependency>
<groupId>com.github.bordertech.didums</groupId>
<artifactId>didums-hk2</artifactId>
<version>1.0.4</version>
</dependency>
....
</project>
- https://github.com/javax-inject/javax-inject
- https://stackoverflow.com/questions/2652126/google-guice-vs-jsr-299-cdi-weld
- https://ceylon-lang.org/blog/2015/12/01/weld-guice/
- https://medium.com/@varago.rafael/introduction-to-google-guice-for-di-43b63a48f231
- https://dzone.com/articles/an-opinionless-comparison-of-spring-and-guice
- https://www.mkyong.com/spring3/spring-3-and-jsr-330-inject-and-named-example/
- https://www.martinfowler.com/articles/injection.html
- https://medium.com/@varago.rafael/managing-coupling-with-dependency-injection-46157eb1dc4d
Dependency Injection is defined via JSR330. However there are other JSRs that expand JSR330 and can cause some confusion.
How are JSR330, JSR299, JSR250, JSR365, JSR346 related:
- https://jakarta.ee/specifications/dependency-injection/
- https://blogs.oracle.com/javamagazine/post/java-jakarta-cdi-interceptor-qualifier-producer
- http://www.adam-bien.com/roller/abien/entry/what_is_the_relation_between
- https://dzone.com/articles/what-relation-betwe-there
- https://dzone.com/articles/jsr-365-update-digging-into-cdi-20
- https://www.javacodegeeks.com/2017/03/jsr-365-update-digging-cdi-2-0.html
- https://en.wikipedia.org/wiki/JSR_250
HK2 has been taken over by the eclipse EE4J initiative and is the implementation of the Jakarta Dependency Injection.
The new Dependency Injection API provided by Jakarta has renamed the javax.inject
package to jakarta.inject
.
Like HK2, Didums was originally created to help use frameworks that implemented the JSR330 spec.
Didums will continue to use the V1 javax.inject
package name and consider changing to the V2 jakarta.inject
package name in the future.
If your project uses Jersey, then using Didums makes it even easier to define your bindings as Jersey uses HK2.