Wednesday 11 January 2023

Micronaut: @Factory: specify one or more bean implementations

@Factory annotation is a Singleton that produces one or many other bean implementations.

@Factory
public class DogBeansFactory {

	@Bean
	@Named("myBullDog1")
	public BullDog bullDog1() {
		BullDog bullDog = new BullDog();
		bullDog.setDescription("Bull Dog 1");
		return bullDog;
	}

	@Bean
	@Named("myBullDog2")
	public BullDog bullDog2() {
		BullDog bullDog = new BullDog();
		bullDog.setDescription("Bull Dog 2");
		return bullDog;
	}

}

 

 

a.   DogBeansFactory produces to BullDog implementaitons (myBullDog1, myBullDog2).

b.   Each produced bean is defined by method that is annotated with @Bean annotation. Methods defined within the body of the class that are annotated with Bean will be exposed as beans.

 

How to control the scope of a bean?

You can control the scope of a bean using Micronaut built in scopes.

 

a.   @Singleton

b.   @Context

c.    @Prototype

d.   @Infrastructure

e.   @ThreadLocal

f.     @Refreshable

g.   @RequestScope

@Singleton
@Named("myBullDog3")
public BullDog bullDog3() {
	BullDog bullDog = new BullDog();
	bullDog.setDescription("Bull Dog 3");
	return bullDog;
}

Find the below working application.

 

Step 1: Create new maven project ‘micronaut-factory-annotation-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-factory-annotation-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 Dog interface.

 

Dog.java

package com.sample.app.interfaces;

public interface Dog {

	String aboutMe();
}

 

Step 4: Define BullDog class.

 

BullDog.java
package com.sample.app.models;

import com.sample.app.interfaces.Dog;

public class BullDog implements Dog {

	private String description;

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	@Override
	public String aboutMe() {
		return description;
	}

}

 

Step 5: Define DogBeansFactory class.

DogBeansFactory.java

package com.sample.app.service;

import com.sample.app.models.BullDog;

import io.micronaut.context.annotation.Bean;
import io.micronaut.context.annotation.Factory;
import jakarta.inject.Named;
import jakarta.inject.Singleton;

@Factory
public class DogBeansFactory {

	@Bean
	@Named("myBullDog1")
	public BullDog bullDog1() {
		BullDog bullDog = new BullDog();
		bullDog.setDescription("Bull Dog 1");
		return bullDog;
	}

	@Bean
	@Named("myBullDog2")
	public BullDog bullDog2() {
		BullDog bullDog = new BullDog();
		bullDog.setDescription("Bull Dog 2");
		return bullDog;
	}
	
	@Singleton
	@Named("myBullDog3")
	public BullDog bullDog3() {
		BullDog bullDog = new BullDog();
		bullDog.setDescription("Bull Dog 3");
		return bullDog;
	}

}

 

Step 6: Define DogService class.

 

DogService.java
package com.sample.app.service;

import com.sample.app.interfaces.Dog;

import jakarta.inject.Inject;
import jakarta.inject.Named;
import jakarta.inject.Singleton;

@Singleton
public class DogService {

	@Inject
	@Named("myBullDog1")
	private Dog dog1;

	@Inject
	@Named("myBullDog2")
	private Dog dog2;

	@Inject
	@Named("myBullDog3")
	private Dog dog3;

	public void aboutMe() {

		System.out.println(dog1.aboutMe());
		System.out.println(dog2.aboutMe());
		System.out.println(dog3.aboutMe());

	}

}

 

Step 7: Define main application class.

 

App.java
package com.sample.app;

import com.sample.app.service.DogService;

import io.micronaut.context.ApplicationContext;

public class App {

	public static void main(String[] args) {
		try (ApplicationContext context = ApplicationContext.run()) {
			DogService animalUtil = context.getBean(DogService.class);
			animalUtil.aboutMe();
		}
	}
}

 

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

 

Execute below command to run the application.

java -jar ./target/micronaut-factory-annotation-demo-1-jar-with-dependencies.jar

$java -jar ./target/micronaut-factory-annotation-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.
Bull Dog 1
Bull Dog 2
Bull Dog 3

 

You can download this application from this link.



Previous                                                    Next                                                    Home

No comments:

Post a Comment