Monday 6 February 2023

Micronaut: Working with bean events

Following interfaces are used to handle bean initialize and creation events.

 

Interface

Description

BeanInitializedEventListener

Allows modifying or replacing a bean after properties have been set but prior to @PostConstruct event hooks.

BeanCreatedEventListener

Allows modifying or replacing a bean after the bean is fully initialized and all @PostConstruct hooks called.

 

Example

@Singleton
public class ConnectionBeanEvents
		implements BeanInitializedEventListener<Connection>, BeanCreatedEventListener<Connection> {

	@Override
	public Connection onCreated(BeanCreatedEvent<Connection> event) {
		System.out.println("onCreated: set the port to 3456");
		Connection connection = event.getBean();
		connection.setPort(3456);
		return connection;
	}

	@Override
	public Connection onInitialized(BeanInitializingEvent<Connection> event) {
		System.out.println("onInitialized: set the port to 3456");
		
		Connection connection = event.getBean();
		connection.setPort(1234);
		return connection;
	}

}

 

Find the below working application.

 

Step 1: Create new maven project ‘micronaut-bean-events-demo’.

 

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-events-demo</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 Connection model class.

 

Connection.java

 

package com.sample.app.model;

import io.micronaut.context.annotation.Prototype;
import jakarta.annotation.PostConstruct;

@Prototype
public class Connection {

	private Integer port;

	public Connection() {
		System.out.println("Connection constructor called");
	}

	public Integer getPort() {
		return port;
	}

	public void setPort(Integer port) {
		this.port = port;
	}

	@Override
	public String toString() {
		return "Connection [port=" + port + "]";
	}
	
	@PostConstruct
	public void initialize() {
		System.out.println("inside PostConstruct, Port has value " + this.port);
	}

}

 

Step 4: Define ConnectionBeanEvents class.

 

ConnectionBeanEvents.java

package com.sample.app.events;

import io.micronaut.context.event.BeanCreatedEvent;
import io.micronaut.context.event.BeanCreatedEventListener;
import io.micronaut.context.event.BeanInitializedEventListener;
import io.micronaut.context.event.BeanInitializingEvent;
import jakarta.inject.Singleton;

import com.sample.app.model.Connection;

@Singleton
public class ConnectionBeanEvents
		implements BeanInitializedEventListener<Connection>, BeanCreatedEventListener<Connection> {

	@Override
	public Connection onCreated(BeanCreatedEvent<Connection> event) {
		System.out.println("onCreated: set the port to 3456");
		Connection connection = event.getBean();
		connection.setPort(3456);
		return connection;
	}

	@Override
	public Connection onInitialized(BeanInitializingEvent<Connection> event) {
		System.out.println("onInitialized: set the port to 3456");
		
		Connection connection = event.getBean();
		connection.setPort(1234);
		return connection;
	}

}

Step 5: Define main application class.

 

App.java

package com.sample.app;

import io.micronaut.context.ApplicationContext;
import com.sample.app.model.Connection;

public class App {

	public static void main(String[] args) {
		try (ApplicationContext applicationContext = ApplicationContext.run()) {
			Connection connection = applicationContext.getBean(Connection.class);

			System.out.println(connection);
		}

	}
}

 

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-events-demo-1-jar-with-dependencies.jar’ in project target folder.

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

Execute below command to run the application.

java -jar ./target/micronaut-bean-events-demo-1-jar-with-dependencies.jar

$ java -jar ./target/micronaut-bean-events-demo-1-jar-with-dependencies.jar
Connection constructor called
onInitialized: set the port to 3456
inside PostConstruct, Port has value 1234
onCreated: set the port to 3456
Connection [port=3456]

You can download this application from this link.


 

Previous                                                    Next                                                    Home

No comments:

Post a Comment