Thursday, 26 January 2023

Micronaut: replace default implementation of an interface

@DefaultImplementation annotation is applied on an interface to indicate which implementation is the default implementation.

 

But there are cases, where you might need to replace the default implementation of the interface due to visibility restrictions of the class.

@DefaultImplementation(FileDataSource.class)
public interface DataSource {

}

@Singleton
class FileDataSource implements DataSource {
	public String toString() {
		return "MySQLDataSource";
	}

}

@Singleton
@Replaces(DataSource.class)
public class MySQLDataSource implements DataSource {

	@Override
	public String toString() {
		return "MySQLDataSource";
	}

}

In the above example, the MySQLDataSource replaces the FileDataSource because the DefaultImplementation annotation points to the FileDataSource.

 

Find the below working application.

 

Step 1: Create new maven project ‘micronaut-default-implementation-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-default-implementation-demo</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 DataSource interface.

 

DataSource.java

package com.sample.app.interfaces;

import io.micronaut.context.annotation.DefaultImplementation;
import jakarta.inject.Singleton;

@DefaultImplementation(FileDataSource.class)
public interface DataSource {

}

@Singleton
class FileDataSource implements DataSource {
	public String toString() {
		return "FileDataSource";
	}

}

Step 4: Define MySQLDataSource class.

 

MySQLDataSource.java

package com.sample.app.interfaces.impl;

import com.sample.app.interfaces.DataSource;

import io.micronaut.context.annotation.Replaces;
import jakarta.inject.Singleton;

@Singleton
@Replaces(DataSource.class)
public class MySQLDataSource implements DataSource {

	@Override
	public String toString() {
		return "MySQLDataSource";
	}

}

Step 5: Define main application class.

 

App.java

package com.sample.app;

import com.sample.app.interfaces.DataSource;

import io.micronaut.context.ApplicationContext;

public class App {

	public static void main(String[] args) {
		try (ApplicationContext applicationContext = ApplicationContext.run()) {
			DataSource dataSource = applicationContext.getBean(DataSource.class);
			
			System.out.println(dataSource);
		}
	}
}

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

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

Execute below command to run the application.

java -jar ./target/micronaut-default-implementation-demo-1-jar-with-dependencies.jar
$ java -jar ./target/micronaut-default-implementation-demo-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.
MySQLDataSource

You can download this application from this link.



Previous                                                    Next                                                    Home

No comments:

Post a Comment