Git Product home page Git Product logo

grape's Introduction

Grape

Basic proof of concept implementation of the IoC container.

Build Status codecov Codacy Badge Quality Gate Status Maintainability Rating

Dependency

This project is distributed via JitPack. Register a JitPack repository at your pom.xml:

<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>

And add the following dependency:

<dependency>
    <groupId>com.github.jolice</groupId>
    <artifactId>Grape</artifactId>
    <version>v1.0</version>
</dependency>

Usage

Grape grape = new Grape(new GrapeConfiguration()
                        .scan("your.packages.to.scan")
                        .configurations(Collections.singletonList(new TestConfiguration()))
                        .classes(Arrays.asList(Sample.class, Component.class))
);
                        
Context context = grape.createContext();
Sample sample = context.getBean(Sample.class);

Defining a bean

There are three ways to register a component in the Grape container.

Component scanning

Annotate a class with @Component so it will be detected by the component scanning

package com.example.beans;

@Component
public class SomeComponent {

And instruct Grape to scan a certain package and its subpackages:

Grape grape = new Grape(new GrapeConfiguration().scan("com.example.beans"));

Context context = grape.createContext();

Manual registration

public class SomeComponent {

Register a class in the configuration

Grape grape = new Grape(new GrapeConfiguration().classes(
     Collections.singletonList(SomeComponent.class)
));

Context context = grape.createContext();

Configuration

A configuration is intended for defining beans through the factory methods. This is also known as a Java Configuration in Spring Framework.

To define a bean, add an appropriate factory method in the configuration and annotate it with @Produces:

public class SomeConfiguration implements Configuration {

    @Produces
    public Bean bean() {
        return new Bean();
    }

}

Lifecycle callbacks

Grape supports standard @PostConstruct and @PreDestroy annotations.

Injection point

Constructor injection

@Inject
public YourComponent(DependencyA dependencyA, DependencyB dependencyB) {
    this.dependencyA = dependencyA;
    this.dependencyB = dependencyB;
}

Setter injection

@Inject
public void setDependency(Dependency dependency) {
    this.dependency = dependency;
}

Field injection

@Inject
private Dependency dependency;

Qualifying bean

If there are multiple beans of the same type defined, the injection point must qualify the bean to be injected by specifying its name using @Named annotation.

@Inject
public YourComponent(@Named("ImplementationOne") Interface someInterface) {
    this.someInterface = someInterface;
}

This annotations is supported by setters and fields as well.

In turn, the named bean must be defined as follows:

@Named("ImplementationOne")
public class ImplementationOne {

Or add the bean annotation to the configuration method:

@Produces
@Named("ImplementationOne")
public Interface implementationOne() {
    return new ImplementationOne();
}

The named bean may manually obtained from the context as follows:

Interface i = context.getBean(Interface.class, new NamedLookup("ImplementationOne"));

Bean priority

@Primary annotation is another way to resolve the bean ambiguity. If there are multiple bean candidates for the injection point and one of them is defined with this annotation, it should be given a preference. That is, this annotation makes a bean a priority one.

@Produces
public Interface one() {
    return new One();
}

@Primary
@Produces
public Interface two() {
     return new Two();
}

Troubleshooting

AmbiguousDependencyException

This exception is fired when there are multiple candidates for the injection point and none of them is qualified at this point. Specify a bean to be injected through the @Named mark one of the candidates as a priority one using @Primary annotation.

CircularDependencyException

This exception indicates a circular constructor dependency between two beans. It means that constructor of bean A accepts bean of type B as a parameter and constructor of bean B accepts bean of type A as a parameter. This may be eliminated by injecting one of dependencies involved in a loop through the setter.

UnsatisfiedDependencyException

This exception is raised when there are no candidates for the injection in the conatiner, i.e Grape is unable to resolve the dependency for some reason. It may happen because there are no beans of the dependency type defined or there is no bean with the specified name. Examine the exception message for more details.

InvocationException

This is a reflection-related exception indicating that an invocation of a reflective component is failed due to some reason. As usual, this exception is caused by insufficient access privileges or by a error raised within the invoked constructor or method.

grape's People

Contributors

jolice 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.