Coder Social home page Coder Social logo

dject's Introduction

Dject · Build Status

Guice extensions, help to improve the developer experience of guice.

Design Principles

Keep the core small and scalable, and provide rich extensions arround the core.

Lifecycle support

You can use @PostConstruct and @PreDestroy on the method.

public class PostConstructTest {
    @PostConstruct
    public void postConstruct() {
        // More code here 
    }
    
    @PreDestroy
    public void PreDestroy() {
        // More code here 
    }
}

You can also implements LifecycleListener.

public class ListenerTest implements LifecycleListener {

    @Override
    public void onStarted() {
        // More code here
    }

    @Override
    public void onStopped(Throwable error) {
        // More code here
    }
}
  • @PostConstruct excute at guice object provision time, LifecycleListener.onStarted excute when create guice injecter finished. @PostConstruct will fail fast at guice provision time.
  • LifecycleListener.onStopped excuted before @PreDestroy when object destroyed.

Be Carefull
Any Error(but not Exception) from the lifecycle method will stop executing more lifecycle methods directly. Which means LifecycleListener.onStopped and @PreDestroy will not be excuted if any destroy lifecycle method before throws Error.

Usage

User builder pattern to start up Dject.

public class DjectTest {
    @Test
    public void testStartUp() {
        Dject.newBuilder().withModule(
                new AbstractModule() {
                    @Override
                    protected void configure() {
                        bind(String.class).toInstance("Hello world");
                    }
                })
                .build();
    }
}

If you do not want to abort main thread, call awaitShutdown() after Dject build.

public class DjectTest {
    @Test
    public void testAwaitShutdown() {
        Dject.newBuilder().withModule(
                new AbstractModule() {
                    @Override
                    protected void configure() {
                        bind(String.class).toInstance("Hello world");
                    }
                })
                .build().awaitShutdown();
    }
}

Contribution

PR is very welcome.

dject's People

Stargazers

 avatar

Watchers

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