guice-bootstrap

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

Лицензия

Лицензия

Категории

Категории

GUI Взаимодействие с пользователем Guice Библиотеки уровня приложения Dependency Injection Embulk Универсальные библиотеки Utility
Группа

Группа

org.embulk
Идентификатор

Идентификатор

guice-bootstrap
Последняя версия

Последняя версия

0.3.2
Дата

Дата

Тип

Тип

jar
Описание

Описание

guice-bootstrap
Guice-bootstrap adds JSR 250 Life Cycle annotations to Google Guice
Ссылка на сайт

Ссылка на сайт

http://guice.embulk.org/
Система контроля версий

Система контроля версий

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

Скачать guice-bootstrap

Как подключить последнюю версию

<!-- 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"]

Зависимости

compile (1)

Идентификатор библиотеки Тип Версия
com.google.inject : guice jar 4.2.0

test (1)

Идентификатор библиотеки Тип Версия
org.testng : testng jar 6.9.9

Модули Проекта

Данный проект не имеет модулей.

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

Версии библиотеки

Версия
0.3.2
0.3.1
0.3.0