Skip to content
/ femto Public

A teeny-tiny dependency injection library written in pure standard Java

License

Notifications You must be signed in to change notification settings

mrwilson/femto

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

41 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

A teeny-tiny dependency injection library written in pure standard Java

Topics

Resources

License

Stars

Watchers

Forks

Languages