Giter VIP home page Giter VIP logo

syringe's Introduction

Codacy Badge

Syringe Dependency Injection Framework

A fast and (very) lightweight Dependency Injection Framework for Java

How to use

Use InjectorFactory.create to create an Injector. Example:

Injector injector = InjectorFactory.create(modules);

Where "modules" is a Module array or Iterable<Module> And get the instance of a class by doing this:

Foo foo = injector.getInstance(Foo.class);

Or injecting dependencies to already instantiated objects

Foo foo = new Foo();
injector.injectMembers(foo);

Create a module

Creating a module is as simple as creating a class that extends Module and override configure method Linker is like Guice's Binder The way to configure links is very similar to the way to configure bindings in Guice

import me.yushust.inject.bind.Module;
import me.yushust.inject.bind.Binder;

public class MySimpleModule implements Module {

    @Override
    public void configure(Binder binder) {

    }

}

So, we will make a binding, from Foo to Bar Foo must implement Bar

@Override
public void configure(Binder binder) {
    binder.bind(Foo.class).to(Bar.class);
}

We can also make generic links using Token

@Override
public void configure(Binder binder) {
    // The {} are important!
    binder.bind(new Token<List<String>>() {}).toInstance(new ArrayList<>());
    // The links to instance are Singleton by default
}

It is also possible to make links with annotations (called Qualifiers)

@Override
public void configure(Binder binder) {
    binder.bind(Foo.class)
        .qualified(FooAnnotation.class)
        .to(Bar.class);
}

So the injection would look like this:

@Inject @FooAnnotation
private Foo foo;

(FooAnnotation must be annotated with @BindingAnnotation or @Qualifier) Something like this:

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.FIELD, ElementType.PARAMETER })
@BindingAnnotation
public @interface FooAnnotation {

}

Notes

Syringe does not detect cycle dependencies. So, you should avoid making confusing links.

Download

# Clone the repository
git clone https://github.com/yusshu/syringe
# Move into the folder
cd syringe
# Install syringe using Maven
mvn clean install 

Maven dependency

<dependency>
    <groupId>me.yushust.inject</groupId>
    <artifactId>syringe-core</artifactId>
    <version>0.2.1</version>
</dependency>

syringe's People

Contributors

patothebest avatar yusshu avatar

Watchers

 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.