Coder Social home page Coder Social logo

kappadi's Introduction

kappaDI

Easy to use Scala/Java DI library

Usage

The code below presents a simple coffee machine model written with kappaDI and Java. All classes are located in a package 'example'

package example;
import pl.jaca.kappadi.service.Qualified;
import pl.jaca.kappadi.service.Service;
import pl.jaca.kappadi.service.ServiceBuilder;

@Service //service declaration
public class CoffeeMaker {

    private Heater heater;
    private Pump pump;

    @ServiceBuilder  // There is no need to use this annotation, if only one constructor is present
    public CoffeeMaker( // Dependencies are listed in a constructor
            @Qualified(qualifier = "water") Heater heater,
            Pump pump
    ) {
        this.heater = heater;
        this.pump = pump;
    }
    
    // Constructor is not annotated with a @ServiceBuilder, and thus it is ignored
    public CoffeeMaker() {
    
    }

    public void makeCoffee() {
        heater.heat();
        pump.pump();
        System.out.println("Coffee!");
    }
}
package example;
import pl.jaca.kappadi.service.Service;

@Service
public class Pump {

    public void pump() {
        System.out.println("Pumping...");
    }

}
package example;

public interface Heater {
    void heat();
}
package example;
import pl.jaca.kappadi.service.Qualified;
import pl.jaca.kappadi.service.Service;

@Service
@Qualified(qualifier = "water")
public class WaterHeater implements Heater {

    public void heat() {
        System.out.println("Heating with water...");
    }

}
package example;
import pl.jaca.kappadi.service.Qualified;
import pl.jaca.kappadi.service.Service;

@Service
// You can create qualifiers to allow resolving services with the same type
@Qualified(qualifier = "hotPlate")
public class HotPlateHeater implements Heater {

    public void heat() {
        System.out.println("Heating with a hot plate...");
    }

}
package example;

import pl.jaca.kappadi.Context;

public class Example {

    public static void main(String[] args) {
        // application root package is passed as argument, creates the whole service graph
        Context context = Context.create("example"); 
        CoffeeMaker maker = context.findOne(CoffeeMaker.class).get();
        maker.makeCoffee();
    }
}

The resulting output is:

heating with water...
Pumping...
Coffee!

kappadi's People

Contributors

szymon-rd avatar

Stargazers

 avatar Lukas avatar Wojciech Olech avatar

Watchers

 avatar Lukas avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.