Manually define a Bean - Spring Boot to Micronaut Framework

This guide compares how to create a bean in a Spring Boot application with @Configuration vs. in a Micronaut application with @Factory.

Authors: Sergio del Amo

Micronaut Version: 4.6.1

1. Sample Project

You can download a sample application with the code examples shown in this article.

2. Introduction

Both Spring and Micronaut frameworks are dependency injection engines. In this tutorial, we will manually create a bean, a class managed by the bean context.

3. An Interface and an Implementation

With an interface such as:

springboot/src/main/java/example/micronaut/Greeter.java
package example.micronaut;

public interface Greeter {
    String greet();
}

and an implementation such as:

springboot/src/main/java/example/micronaut/HelloGreeter.java
package example.micronaut;

public class HelloGreeter implements Greeter {
    @Override
    public String greet() {
        return "Hello";
    }
}

We want to be able to inject a bean of the type Greeter into our application.

There are several ways to do it. In this tutorial, we will manually instantiate the bean in both frameworks.

4. Spring @Configuration

Create a @Configuration class to declare Greeter bean through a @Bean-annotated method.

springboot/src/main/java/example/micronaut/GreeterFactory.java
package example.micronaut;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration (1)
class GreeterFactory {

    @Bean
    Greeter helloGreeter() {
        return new HelloGreeter();
    }
}
1 @Configuration is a class-level annotation indicating that an object is a source of bean definitions.

4.1. Spring Boot Test

The following test verify it is possible to inject a bean of type Greeter in a Spring Boot application.

springboot/src/test/java/example/micronaut/GreeterTest.java
package example.micronaut;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import static org.junit.jupiter.api.Assertions.*;

@SpringBootTest (1)
class GreeterTest {

    @Autowired (2)
    Greeter greeter;

    @Test
    void helloGreeterIsInjectedAsBeanOfTypeGreeter() {
        assertNotNull(greeter);
        assertEquals("Hello", greeter.greet());
    }

}
1 The @SpringBootTest annotation tells Spring Boot to look for a main configuration class (one with @SpringBootApplication, for instance) and use that to start a Spring application context.
2 Inject a bean of type Greeter by using @Autowired on the field definition.

5. Micronaut @Factory

micronautframework/src/main/java/example/micronaut/GreeterFactory.java
package example.micronaut;

import io.micronaut.context.annotation.Factory;
import io.micronaut.context.annotation.Bean;

@Factory (1)
class GreeterFactory {
    @Bean
    Greeter helloGreeter() {
        return new HelloGreeter();
    }
}
1 A factory is a class annotated with the @Factory annotation that provides one or more methods annotated with a bean scope annotation.

5.1. Micronaut Test

The following test verify it is possible to inject a bean of type Greeter in a Micronaut application.

micronautframework/src/test/java/example/micronaut/GreeterTest.java
package example.micronaut;

import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import jakarta.inject.Inject;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

@MicronautTest (1)
class GreeterTest {

    @Inject (2)
    Greeter greeter;

    @Test
    void helloGreeterIsInjectedAsBeanOfTypeGreeter() {
        assertNotNull(greeter);
        assertEquals("Hello", greeter.greet());
    }

}
1 Annotate the class with @MicronautTest so the Micronaut framework will initialize the application context and the embedded server. More info.
2 Injection for Greeter.

6. Conclusion

This guide illustrate that the APIs @Configuration and @Factory of both frameworks are almost identical.

7. Next steps

Read more Spring Boot to Micronaut guides.

8. Help with the Micronaut Framework

The Micronaut Foundation sponsored the creation of this Guide. A variety of consulting and support services are available.

9. License

All guides are released with an Apache license 2.0 license for the code and a Creative Commons Attribution 4.0 license for the writing and media (images…​).