ormfux-simple-di

A small library for simple application that want to use dependency injection

License

License

Categories

Categories

ORM Data
GroupId

GroupId

com.github.orm-fux
ArtifactId

ArtifactId

ormfux-simple-di
Last Version

Last Version

1.5
Release Date

Release Date

Type

Type

jar
Description

Description

ormfux-simple-di
A small library for simple application that want to use dependency injection
Project URL

Project URL

https://github.com/orm-fux/ormfux-simple-di
Source Code Management

Source Code Management

https://github.com/orm-fux/ormfux-simple-di.git

Download ormfux-simple-di

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.orm-fux/ormfux-simple-di/ -->
<dependency>
    <groupId>com.github.orm-fux</groupId>
    <artifactId>ormfux-simple-di</artifactId>
    <version>1.5</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.orm-fux/ormfux-simple-di/
implementation 'com.github.orm-fux:ormfux-simple-di:1.5'
// https://jarcasting.com/artifacts/com.github.orm-fux/ormfux-simple-di/
implementation ("com.github.orm-fux:ormfux-simple-di:1.5")
'com.github.orm-fux:ormfux-simple-di:jar:1.5'
<dependency org="com.github.orm-fux" name="ormfux-simple-di" rev="1.5">
  <artifact name="ormfux-simple-di" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.orm-fux', module='ormfux-simple-di', version='1.5')
)
libraryDependencies += "com.github.orm-fux" % "ormfux-simple-di" % "1.5"
[com.github.orm-fux/ormfux-simple-di "1.5"]

Dependencies

test (2)

Group / Artifact Type Version
junit : junit jar 4.11
org.assertj : assertj-core jar 3.12.0

Project Modules

There are no modules declared in this project.

About

This is a small library providing some dependency injection capabilities. Use this when you have a small application, want DI comfort, but don't want the large pool of features like they are provided by Spring.

Available on MavenCentral:

<dependency>
  <groupId>com.github.orm-fux</groupId>
  <artifactId>ormfux-simple-di</artifactId>
  <version>1.5</version>
</dependency>

Usage

This library provides a set of annotations, with which you can annotate classes and their properties.

Retrieving bean instances

To manually retrieve a bean instance simply call:

YourBean bean = InjectionContext.getBean(YourBean.class);

This call looks up the bean cache in the context. When there is already an instance of the bean, then this instance is returned. Otherwise a new instance is created and values are injected into the new instance. When the injected values are not in the bean context a new bean instance is created for them automatically as well.

The type of the bean must have been explicitly declared as a bean type!

Marking Classes as Beans

There are two options, which can make a class a bean:

@Bean Annotation

To make a class a bean with injected values add the @Bean annotation to the class and the @BeanConstructor annotation one of its constructors. That's it!

Manual Registration

You can make non-annotated classes bean types by manually adding them as a simple descriptor to the InjectionContext:

InjectionContext.addBeanDescriptor(YourBean.class, true|false)

Make sure to register beans this way only during application startup! That way you can be sure that an instance of these beans will be available during injection into other beans.

This method of bean registration is supposed to be used, when you don't have control over the source code of the class you want to use as a bean.

Defining Injected Values

This library injects values via the constructor with the @BeanConstructor annotation. For each parameter a value is injected. These values can be other beans (either annotated or manually registered) and values from configuration files that have been added to the @InjectionContext.

For configuration values a number of simple types (String, number, Class, enum) are supported. The values from the configuration files are converted automatically to the type of the respective parameter. To declare that a parameter gets its value from a configuration file use the @ConfigValue annotation.

To add a configuration file to the context simply call one of the two methods InjectionContext.addConfigValueSet(..) or InjectionContext.addExternalConfigValueSet(..). Supported files are plain old *.properties files (including their XML format).

Example Application:

@Bean
public class Application {
    
    public static main(String... args) {
        InjectionContext.addExternalConfigValueSet("myConfig", "/fully/path/to/config.properties");
        
        Application application = InjectionContext.getBean(Application.class);
        
        application.injectedBean.sayHello();
    }
    
    private OtherBean injectedBean;
    
    private int version;
    
    @BeanConstructor
    public Application(OtherBean otherBean,
                       @ConfigValue("config.version") int version) {
       this.otherBean = otherBean;
       this.version = version;
    }
}

@Bean
public class OtherBean {
    public void sayHello() {
        System.out.println("Hello from an injected bean!");
    }
}

Versions

Version
1.5