Tuesday 7 February 2023

Micronaut: Introduction to Bean introspection api

In this post, let’s quickly discuss how to introspect a bean in Micronaut.

 

Step 1: Annotate the bean with @Introspected annotation.

@Introspected
public class Person {

	private Integer id;

	private String name;

	public Person(Integer id, String name) {
		this.id = id;
		this.name = name;
	}

	............
	............
}

 

Step 2: Retrieve the introspection data using BeanIntrospection class.

try (ApplicationContext applicationContext = ApplicationContext.run()) {
	final BeanIntrospection<Person> introspection = BeanIntrospection.getIntrospection(Person.class);
	Person person = introspection.instantiate(1, "Krishna");
	
	System.out.println("id : " + person.getId());
	System.out.println("name : " + person.getName());

	final BeanProperty<Person, String> property = introspection.getRequiredProperty("name", String.class); 
	property.set(person, "Ram"); //
	String name = property.get(person);

	System.out.println("\nname : " + name);	
	System.out.println("\nid : " + person.getId());
	System.out.println("name : " + person.getName());

}

 

Find the below working application.

 

Step 1: Create new maven project ‘micronaut-bean-introspection-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-bean-introspection-hello-world</artifactId>
	<version>1</version>

	<properties>
		<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
		<micronaut.version>3.7.2</micronaut.version>
		<slf4j.version>2.0.3</slf4j.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>
		<dependency>
			<groupId>io.micronaut</groupId>
			<artifactId>micronaut-runtime</artifactId>
			<version>${micronaut.version}</version>
		</dependency>


		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>${slf4j.version}</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-simple</artifactId>
			<version>${slf4j.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 Person class.

 

Person.java

package com.sample.app.model;

import io.micronaut.core.annotation.Introspected;

@Introspected
public class Person {

	private Integer id;

	private String name;

	public Person(Integer id, String name) {
		this.id = id;
		this.name = name;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}

Step 4: Define main application class.

 

App.java

package com.sample.app;

import com.sample.app.model.Person;

import io.micronaut.context.ApplicationContext;
import io.micronaut.core.beans.BeanIntrospection;
import io.micronaut.core.beans.BeanProperty;

public class App {

	public static void main(String[] args) {
		try (ApplicationContext applicationContext = ApplicationContext.run()) {
			final BeanIntrospection<Person> introspection = BeanIntrospection.getIntrospection(Person.class);
			Person person = introspection.instantiate(1, "Krishna");
			
			System.out.println("id : " + person.getId());
			System.out.println("name : " + person.getName());

			final BeanProperty<Person, String> property = introspection.getRequiredProperty("name", String.class); 
			property.set(person, "Ram"); //
			String name = property.get(person);
		
			System.out.println("\nname : " + name);	
			System.out.println("\nid : " + person.getId());
			System.out.println("name : " + person.getName());

		}

	}
}

Total project structure looks like below.




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-bean-introspection-hello-world-1-jar-with-dependencies.jar’ in project target folder.

 

$ls ./target/
archive-tmp
classes
generated-sources
generated-test-sources
maven-archiver
maven-status
micronaut-bean-events-demo-1-jar-with-dependencies.jar
micronaut-bean-events-demo-1.jar
micronaut-bean-introspection-hello-world-1-jar-with-dependencies.jar
micronaut-bean-introspection-hello-world-1.jar
test-classes

 

Execute below command to run the application.

$java -jar ./target/micronaut-bean-introspection-hello-world-1-jar-with-dependencies.jar
id : 1
name : Krishna

name : Ram

id : 1
name : Ram

 

You can download this application from application this link.


 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment