Skip to content

Dependency Injection Library for Java that uses constructor invocation to support immutable instances.

License

Notifications You must be signed in to change notification settings

Pyknic/stiletto

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

52 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

stiletto

Build Status Javadocs Maven Central License

Dependency Injection Library for Java that uses constructor invocation to support immutable instances.

Features

  • Minimal Overhead
  • final members
  • Non-default constructors
  • Interface/Implementation separation
  • Named Instances using Qualifiers
  • Optional classpath scanning

Installation

Using Maven

Add the following to your pom.xml-file:

<dependency>
    <groupId>com.github.pyknic</groupId>
    <artifactId>stiletto</artifactId>
    <version>1.0.4</version>
</dependency>

Using Gradle

Add the following to your build.gradle-file:

compile group: 'com.github.pyknic', name: 'stiletto', version: '1.0.4'

Usage

The main package for the dependency injector interfaces. The dependency injection system follows a builder pattern where the two interfaces Injector and InjectorBuilder are central.

Configuration

The builder pattern allows the dependency injector to be built by appending types available for dependency injection. An optional qualifier string can be specified to help identify different implementations. If the dependency graph is incomplete or contains cyclic dependencies when the InjectorBuilder.build()-method is invoked, then an InjectorException is thrown.

// Create a new dependency injector with a number of injectable
// implementations.
Injector injector = Injector.builder()
    .withType(CommentComponentImpl.class)
    .withType(TopicComponentImpl.class)
    .withType(MySQLComponentImpl.class, "mysql")
    .withType(PostgreSQLComponentImpl.class, "postgreSQL")
    .build();

Annotation Injection

Fields and/or constructors can be annotated to direct the dependency injector. A qualifier string can be specified in the annotation to control which instance is loaded if there are multiple implementations of the same interface.

// Custom class
class TopicComponentImpl implements TopicComponent {

    private final CommentComponent comments;

    @Inject("mysql")
    private DatabaseComponent database;

    // Don't use this constructor
    TopicComponentImpl() {
        comments = new DummyCommentComponent();
    }

    // Instead, use this constructor:
    @Inject
    TopicComponentImpl(CommentComponent comments) {
        this.comments = requireNonNull(comments);
    }
}

Programmatic Injection

The Injector instance can also be invoked programmatically to obtain instances from the graph.

// Get a type based on the interface (or the implementation)
final CommentComponent comments =
    injector.getOrThrow(CommentComponent.class);

// Get an instance if one is available
final Optional<OtherComponent> optional =
    injector.get(OtherComponent.class);

// See if an implementation for a particular interface is available.
final boolean hasTopicComponent =
    injector.has(TopicComponent.class);

License

Copyright 2017 Emil Forslund

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.