femto

A teeny-tiny dependency injection library

License

License

MIT
GroupId

GroupId

uk.co.probablyfine
ArtifactId

ArtifactId

femto
Last Version

Last Version

0.2.0
Release Date

Release Date

Type

Type

jar
Description

Description

femto
A teeny-tiny dependency injection library
Project URL

Project URL

https://github.com/mrwilson/femto
Source Code Management

Source Code Management

https://github.com/mrwilson/femto

Download femto

How to add to project

<!-- https://jarcasting.com/artifacts/uk.co.probablyfine/femto/ -->
<dependency>
    <groupId>uk.co.probablyfine</groupId>
    <artifactId>femto</artifactId>
    <version>0.2.0</version>
</dependency>
// https://jarcasting.com/artifacts/uk.co.probablyfine/femto/
implementation 'uk.co.probablyfine:femto:0.2.0'
// https://jarcasting.com/artifacts/uk.co.probablyfine/femto/
implementation ("uk.co.probablyfine:femto:0.2.0")
'uk.co.probablyfine:femto:jar:0.2.0'
<dependency org="uk.co.probablyfine" name="femto" rev="0.2.0">
  <artifact name="femto" type="jar" />
</dependency>
@Grapes(
@Grab(group='uk.co.probablyfine', module='femto', version='0.2.0')
)
libraryDependencies += "uk.co.probablyfine" % "femto" % "0.2.0"
[uk.co.probablyfine/femto "0.2.0"]

Dependencies

There are no dependencies for this project. It is a standalone project that does not depend on any other jars.

Project Modules

There are no modules declared in this project.

femto

Maven Central

Femto is a teeny-tiny dependency injection library written in pure standard Java.

Installing

Maven:

<dependency>
  <groupId>uk.co.probablyfine</groupId>
  <artifactId>femto</artifactId>
  <version>${version}</version>
</dependency>

Gradle:

implementation "uk.co.probablyfine:femto:${version}"

Usage

class Example {

    public static void main(String... args) {
        // Initialise a new injector
        var injector = new FemtoInjector();
        
        // Bind a class
        injector.bind(One.class); 

        // Bind a class to an implementing class
        injector.bind(Two.class, TwoImplementation.class); 

        // Bind a class to an instance
        injector.bindInstance(Three.class, new ThreeImplementation()); 

        // Depends on One, Two, Three
        injector.bind(Four.class); 
        
        // Create a new object
        Four four = injector.get(Four.class);
    }
}

Design Decisions

  • Singleton by default: The first call to FemtoInjector#get caches the result for each class and returns the same value afterwards
  • Lazy by default: FemtoInjector does not initialise any classes until .get() is called.
  • No annotation "magic": Clients register components directly with the injector rather than using JSR 330 (@Inject) annotations.
  • Prefer standard Java over dependencies: Keeps the library small, and the source code easy to read.
  • No named bindings: Usd the type system to extend and name classes instead.

Versions

Version
0.2.0
0.1.0