Skip to content

Facade for Dependency Injection Frameworks based on JSR330

License

Notifications You must be signed in to change notification settings

BorderTech/didums

Repository files navigation

Didums

Status

Build Status Quality Gate Status Reliability Rating Coverage Codacy Badge Javadocs Maven Central

Content

What is Didums

Didums is a facade for Dependency Injection frameworks that implement JSR330.

Why use Didums

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.

Getting started

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.

Factory 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.

Didums Binding

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.

Configuration

DidumsProvider

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>

Links

DI Frameworks

Interesting links

DI and Related JSRs

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:

HK2 and Jakarta EE4J Initiative

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.

Using Didums and Jersey

If your project uses Jersey, then using Didums makes it even easier to define your bindings as Jersey uses HK2.

Contributing

Refer to these guidelines for Workflow and Releasing.

About

Facade for Dependency Injection Frameworks based on JSR330

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages