In this post, I am going to explain how can we manage a bean using Micronaut.
What is a bean?
A bean is a java object whose lifecycle (creation, execution and destruction) is managed by the Micronaut IoC container.
Since Micronaut implements ‘JSR-330: Dependency Injection for Java’, you can simply use the annotation in jakarta.inject package.
For example,
public interface Animal {
String color();
String owner();
}
@Singleton
public class Dog implements Animal{
@Override
public String color() {
return "Black";
}
@Override
public String owner() {
return "Krishna";
}
}
@Singleton
public class AnimalUtil {
private Animal dog;
public AnimalUtil(Animal dog) {
this.dog = dog;
}
public void aboutMe() {
System.out.println("Owner : " + dog.owner());
System.out.println("Color: " + dog.color());
}
}
Above snippet defines
a. Animal interface
b. A Dog implementation is defined and marked with Singleton scope.
c. The Animal is injected via constructor injection in AnimalUtil class.
How to perform dependency injection?
Get an instance of ApplicationContext.
ApplicationContext context = ApplicationContext.run();
'run' method run the ApplicationContext. This method will instantiate a new ApplicationContext and call start(). 'start()' method starts the application context.
Get an instance of AnimalUtil bean using getBean method.
AnimalUtil animalUtil = context.getBean(AnimalUtil.class);
Find the below working application.
Step 1: Create new maven project ‘micronaut-ioc-hello-world’.
Step 2: Update pom.xml with maven dependencies.
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sample.app</groupId>
<artifactId>micronaut-ioc-hello-world</artifactId>
<version>1</version>
<properties>
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
<micronaut.version>3.7.1</micronaut.version>
<maven.compiler.target>15</maven.compiler.target>
<maven.compiler.source>15</maven.compiler.source>
</properties>
<dependencies>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-inject-java</artifactId>
<version>${micronaut.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.sample.app.App</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Step 3: Define Animal interface.
Animal.java
package com.sample.app.interfaces;
public interface Animal {
String color();
String owner();
}
Step 4: Define Dog and AnimalUtil classes.
Dog.java
package com.sample.app.models;
import com.sample.app.interfaces.Animal;
import jakarta.inject.Singleton;
@Singleton
public class Dog implements Animal{
@Override
public String color() {
return "Black";
}
@Override
public String owner() {
return "Krishna";
}
}
AnimalUtil.java
package com.sample.app.util;
import com.sample.app.interfaces.Animal;
import jakarta.inject.Singleton;
@Singleton
public class AnimalUtil {
private Animal dog;
public AnimalUtil(Animal dog) {
this.dog = dog;
}
public void aboutMe() {
System.out.println("Owner : " + dog.owner());
System.out.println("Color: " + dog.color());
}
}
Step 5: Define main application class.
App.java
package com.sample.app;
import com.sample.app.util.AnimalUtil;
import io.micronaut.context.ApplicationContext;
public class App {
public static void main(String[] args) {
try (ApplicationContext context = ApplicationContext.run()) {
AnimalUtil animalUtil = context.getBean(AnimalUtil.class);
animalUtil.aboutMe();
}
}
}
Total project structure looks like below.
Run App.java, you will see below messages in the console.
Owner : Krishna Color: Black
Build the project using mvn package command.
Navigate to the folder where pom.xml is located and execute the command ‘mvn package’.
Upon command successful execution, you can see the jar file ‘micronaut-ioc-hello-world-1-jar-with-dependencies.jar’ in project target folder.
$ls ./target/
archive-tmp maven-status
classes micronaut-ioc-hello-world-1-jar-with-dependencies.jar
generated-sources micronaut-ioc-hello-world-1.jar
maven-archiver test-classes
Run the jar file by executing below command.
java -jar ./target/micronaut-ioc-hello-world-1-jar-with-dependencies.jar
$java -jar ./target/micronaut-ioc-hello-world-1-jar-with-dependencies.jar
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Owner : Krishna
Color: Black
You can download this application from this link.
At the time of writing this post, Micronaut supports the following types of dependency injection:
a. Constructor injection (must be one public constructor or a single constructor annotated with @Inject)
b. Field injection
c. JavaBean property injection
d. Method parameter injection
References
https://javax-inject.github.io/javax-inject/
No comments:
Post a Comment