reguice

Resource Injection for Google Guice

Лицензия

Лицензия

Категории

Категории

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

Группа

de.skuzzle.inject
Идентификатор

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

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

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

0.3.0
Дата

Дата

Тип

Тип

jar
Описание

Описание

reguice
Resource Injection for Google Guice

Скачать reguice

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

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

Зависимости

compile (7)

Идентификатор библиотеки Тип Версия
com.google.guava : guava jar
com.google.code.gson : gson jar
org.slf4j : slf4j-api jar
com.google.inject : guice jar
javax.inject : javax.inject jar
junit : junit jar
org.mockito : mockito-all jar

provided (1)

Идентификатор библиотеки Тип Версия
javax.servlet : servlet-api jar

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

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

Build Status Maven Central Coverage Status

reguice

... allows you to simply bind resources for injecting them with Google's Guice.

Features:

  • Bind from several sources (class path, file system, URL, web application context)
  • Bind several content types (String, properties, json, xml and custom)
  • Bind properties directly to a Java interface
  • Allow reloading of bound Objects by injecting a Provider
  • Fluent API for configuring the binding

Maven artifact

reguice is available from Maven Central:

<dependency>
    <groupId>de.skuzzle.inject</groupId>
    <artifactId>reguice</artifactId>
    <version>0.3.0</version>
</dependency>

Quickstart

The fluent API is accessible through the Resources class and may be used in your Guice Module. You can find some additional examples of how to bind resources here. The following example will bind the content of the MANIFEST.MF to a String annotated with @Named("manifestContent").

@Override
public void configure() {
    Resources.bind()
            .classPathResource("META-INF/MANIFEST.mf")
            .encodedWith(StandardCharset.UTF_8)
            .containingText()
            .to(Key.get(String.class, Names.named("manifestContent")))
            .in(Singleton.class)
            .using(binder());
}

Please note that the last call of the fluently chained methods must always be to using(binder()).

The text content of the file can automatically be mapped to a Manifest (provided by java.util.jar) instance by implementing your own TextContentType:

public class ManifestContent implements TextContentType {

    @Override
    public <T> T createInstance(Class<T> type, TextResource resource) {
        checkArgument(Manifest.class.isAssignableFrom(type));
        try (InputStream in = resource.openBinaryStream()) {
            final Manifest mf = new Manifest();
            mf.read(in);
            return type.cast(mf);
        } catch (final IOException e) {
            throw new ProvisionException("Error while reading manifest", e);
        }
    }
}

The resource can then be bound to Manifest.class like:

@Override
public void configure() {
    Resources.bind()
            .classPathResource("META-INF/MANIFEST.mf")
            .encodedWith(StandardCharset.UTF_8)
            .containing(new ManifestContent())
            .to(Manifest.class)
            .in(Singleton.class)
            .using(binder());
}

You can also bind properties from a .properties file directly to getter methods of an interface.

threadCount=4
dataFolder=data/
public interface AppSettings {
    int getThreadCount();
    String getDataFolder();
}
@Override
public void configure() {
    Resources.bind()
            .changing()
            .classPathResource("config/appSettings.properties")
            .encodedWith(StandardCharset.UTF_8)
            .containingProperties()
            .to(AppSettings.class)
            .using(binder());
}

In this example, reguice will automatically create an implementation of the provided interface and map its getters to the properties of the specified resource.

The same will work for json content type too.

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

Версия
0.3.0
0.2.0
0.1.0