guice-bootstrap

Guice-bootstrap adds JSR 250 Life Cycle annotations to Google Guice

License

License

Categories

Categories

GUI User Interface Guice Application Layer Libs Dependency Injection Embulk General Purpose Libraries Utility
GroupId

GroupId

org.embulk
ArtifactId

ArtifactId

guice-bootstrap
Last Version

Last Version

0.3.2
Release Date

Release Date

Type

Type

jar
Description

Description

guice-bootstrap
Guice-bootstrap adds JSR 250 Life Cycle annotations to Google Guice
Project URL

Project URL

http://guice.embulk.org/
Source Code Management

Source Code Management

https://github.com/embulk/guice-bootstrap

Download guice-bootstrap

How to add to project

<!-- https://jarcasting.com/artifacts/org.embulk/guice-bootstrap/ -->
<dependency>
    <groupId>org.embulk</groupId>
    <artifactId>guice-bootstrap</artifactId>
    <version>0.3.2</version>
</dependency>
// https://jarcasting.com/artifacts/org.embulk/guice-bootstrap/
implementation 'org.embulk:guice-bootstrap:0.3.2'
// https://jarcasting.com/artifacts/org.embulk/guice-bootstrap/
implementation ("org.embulk:guice-bootstrap:0.3.2")
'org.embulk:guice-bootstrap:jar:0.3.2'
<dependency org="org.embulk" name="guice-bootstrap" rev="0.3.2">
  <artifact name="guice-bootstrap" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.embulk', module='guice-bootstrap', version='0.3.2')
)
libraryDependencies += "org.embulk" % "guice-bootstrap" % "0.3.2"
[org.embulk/guice-bootstrap "0.3.2"]

Dependencies

compile (1)

Group / Artifact Type Version
com.google.inject : guice jar 4.2.0

test (1)

Group / Artifact Type Version
org.testng : testng jar 6.9.9

Project Modules

There are no modules declared in this project.

Guice-bootstrap

Guice-bootstrap is an extension of Guice that adds support for JSR 250 Life Cycle annotations.

@PostConstruct annotation is used on methods that need to get executed after dependency injection is done to perform any initialization.

@PreDestroy annotation is used on methods that are called before the application shuts down.

PostConstruct

Methods with @PostConstruct annotation are called after all depedency injection is done.

import javax.annotation.PostConstruct;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

class MyService implements Runnable
{
    private final ExecutorService executor = Executors.newSingleThreadExecutor();

    @PostConstruct
    public void start() {
        executor.submit(() -> { ... });
    }
}

PreDestroy

Methods with @PreDestroy annotation are called after the application shuts down.

import javax.annotation.PreDestroy;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

class MyService implements Runnable
{
    private final List<File> files = new ArrayList<>();

    @PreDestroy
    public void cleanup() {
        files.stream().forEach(file -> file.delete());
    }
}

Bootstrap

Bootstrap is the main class to use guice-bootstrap.

Injector injector = new Bootstrap()
    .addModules(new MyGuiceModule1(), new MyGuiceModule2(), ...)
    .addModules(new MyGuiceModule3(), new MyGuiceModule4(), ...)
    .initialize();

Overriding bindings

Bootstrap.overrideModulesWith method allows you to override bindings. This is useful to customize bindings defined by a base class.

// The base class
public class MyService {
    public Bootstrap bootstrap()
    {
        return new Bootstrap()
            .addModules(new MyGuiceModule1(), new MyGuiceModule2(), ...)
            .addModules(new MyGuiceModule3(), new MyGuiceModule4(), ...)
            ;
    }

    public void start()
    {
        Injector injector = bootstrap().initialize();
        ...
    }
}

// Extending class that overrides some bindings
public class MyExtendedService extend MyService {
    @Override
    public Bootstrap bootstrap()
    {
        return super()
            .overrideModulesWith(new MyGuiceModule4(), ...)
            ;
    }
}

CloseableInjector

Bootstrap.initialize() sets up a shutdown hook to the Java VM (@Runtime.addShutdownHook`). It ensures that PostDestroy methods are called when Java VM exits even if it's killed by a SIGTERM or Ctrl-C.

But if you want to control the exact timing of shutdown, you can use Bootstrap.initializeCloseable() instead. It returns CloseableInjector which implements Injector and Closeable interfaces.

Bootstrap bootstrap = new Bootstrap()
    .addModules(...);

try (CloseableInjector injector = bootstrap.initializeCloseable()) {
    ...
}
org.embulk

Embulk

Versions

Version
0.3.2
0.3.1
0.3.0