Thursday 19 January 2023

Micronaut: Specify custom scope on meta annotations

You can define scopes on meta annotation and can apply to your beans.

 

For example, I defined DataSource meta annotation with Singleton scope like below.

@Requires(property = "datasource.url")
@Requires(property = "datasource.port")
@Requires(property = "datasource.username")
@Singleton
@Documented
@Retention(RUNTIME)
public @interface DataSource {

}

 

In the above example, @Singleton annotation is applied to the @DataSource meta annotation, which results in every class that is annotated with @DataSource being treated as a singleton.

@DataSource
public class SqlDataSource {

}

 

Can I override the scope defined in Meta annotation @DataSource?

In the above example, we can't override the scope. If you want to enable the scope overridding capability, use the DefaultScope annotation on @DataSource.

@Requires(property = "datasource.url")
@Requires(property = "datasource.port")
@Requires(property = "datasource.username")
@DefaultScope(Singleton.class) 
@Documented
@Retention(RUNTIME)
public @interface DataSource {

}

If the no explicit scope is specified to the bean, then Singleton is used by default.

 

Find the below working application.

 

Step 1: Create new maven project ‘micronaut-meta-annotations’.

 

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-meta-annotations</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: Create application.yml file under src/main/resources folder.

 

application.yml

logger:
  levels:
    io.netty: ERROR
    io.micronaut: ERROR
    
datasource:
  url: abc.com
  port: 1234
  username: krishna

Step 4: Define DataSource meta annotation.

 

DataSource.java

package com.sample.app.annotations;

import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;

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

@Requires(property = "datasource.url")
@Requires(property = "datasource.port")
@Requires(property = "datasource.username")
@Singleton
@Documented
@Retention(RUNTIME)
public @interface DataSource {

}

Step 5: Define SqlDataSource class.

 

SqlDataSource.java

package com.sample.app.model;

import com.sample.app.annotations.DataSource;

import io.micronaut.context.annotation.Value;

@DataSource
public class SqlDataSource {
	@Value("${datasource.url}")
	private String dataSourceURL;

	@Value("${datasource.port}")
	private String dataSourcePort;

	@Value("${datasource.username}")
	private String dataSourceUsername;

	public String getDataSourceURL() {
		return dataSourceURL;
	}

	public void setDataSourceURL(String dataSourceURL) {
		this.dataSourceURL = dataSourceURL;
	}

	public String getDataSourcePort() {
		return dataSourcePort;
	}

	public void setDataSourcePort(String dataSourcePort) {
		this.dataSourcePort = dataSourcePort;
	}

	public String getDataSourceUsername() {
		return dataSourceUsername;
	}

	public void setDataSourceUsername(String dataSourceUsername) {
		this.dataSourceUsername = dataSourceUsername;
	}

}

Step 6: Define main application class.

 

App.java

package com.sample.app;

import com.sample.app.model.SqlDataSource;

import io.micronaut.context.ApplicationContext;;

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

			System.out.println("url : " + sqlDataSource.getDataSourceURL());
			System.out.println("username : " + sqlDataSource.getDataSourceUsername());
			System.out.println("port : " + sqlDataSource.getDataSourcePort());
		}
	}
}

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

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

Execute below command to run the application.

java -jar ./target/micronaut-meta-annotations-1-jar-with-dependencies.jar

$ java -jar ./target/micronaut-meta-annotations-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.
url : abc.com
username : krishna
port : 1234

You can download this application from this link.


 

Previous                                                    Next                                                    Home

No comments:

Post a Comment