Skip to content
Erhan Bağdemir edited this page Mar 2, 2024 · 52 revisions

Mikron is a minimalistic general purpose IoC container for dependency injection and externalized configuration management for Java. It allows you to harness the convenience of dependency injection and configuration management facility. Mikron applications instantiate a container object, which is created during the application start-up. Managed objects reside in the context. To define a managed instance, you need to declare it with the @Managed annotation on the class, whose instance the life-cycle is under the control of the Mikron context. The managed objects will get wired together at injection points.

To start using Mikron, you can add the Maven dependency into your project:

  <dependency>  
    <groupId>net.reevik</groupId>  
    <artifactId>mikron</artifactId>  
    <version>${latest}</version>  
  </dependency>  

Mikron Application

By using @ManagedApplication, we declare Mikron applications. The annotation takes a list of packages as parameter, in which the managed instances need to be scanned:

@ManagedApplication(packages = {"your.package.to.scan"})
public class MainApplication {
}

You should either set the exact package name where the managed instances will be looked up, the scan is not recursive, but you can use "*" wildcard to initiate a recursive search, e.g., "net.reevik.foo.*".

Application Context

Mikron applications must instantiate Mikron context, that is the IoC container, where managed instances reside. The context scans through the packages for managed objects on start-up:

  public static void main(String[] args) {
    Optional<AnnotatedManagedClass> instance;
    try (MikronContext context = MikronContext.init(AnnotatedManagedClass.class)) {
      instance = context.getInstance(AnnotatedManagedClass.class.getName());
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }

The context controls the managed object's life-cycle, i.e., it creates, initializes and eventually destroys them. As you may notice, the MikronContext implements AutoCloseable interface. If the IoC container gets closed, the managed instances will be destroyed.

Managed Objects

Managed instances are declared by using @Managed annotation on classes. By doing so, you make them candidates for the scan process. Let's take a look at the following example:

@Managed
public class ManagedObject {

  @Wire
  private ManagedDependency managedDependency;
}

Whenever the MikronContext detects a managed object definitions, it creates a new instance by calling the default constructor by default. The managed object must be within one of the packages, which are declared in @ManagedApplication.

You can use @Wire annotation to declare dependency injection points on class fields, where others managed objects get injected. If the dependency cannot be found or out of the scope, the field remains null.

Only concrete classes can be declared as managed.

Configuration Management for Managed Objects

Managed objects may require externalized configurations, e.g., in property files. You can create property files containing configurations for the manage instances, which can be injected into "Configurable" class fields:

package net.reevik.example;

// imports statements

@Managed
public class ConfigurableManagedObject {

  @Configurable(name = "config.temp")
  private Integer temperature;

  public Integer getTemperature() {
    return temperature;
  }
}

If there is a property-file in the classpath with the name "net.reevik.example.ConfigurableManagedObject.properties" containing the configuration property,

config.temp=42

The value "42" will be set to the temperature field. The name of the property-file must consist of the package and the class name, e.g., <package name>.<class name>.properties.

List of Mikron Annotations

Clone this wiki locally