Mark a class as a bean.- Spring Boot

This guide compares how to mark a class as a bean in a Spring Boot application with @Component vs. in a Micronaut application with @Singleton.

Authors: Sergio del Amo

Micronaut Version: 4.6.3

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 create a bean by "marking" a class as a bean.

3. An Interface

With an interface such as:

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

public interface Greeter {
    String greet();
}

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

4. Spring Boot @Component

In Spring, we can create an implementation and annotate it with @Component.

springboot/src/main/java/example/micronaut/HelloGreeter.java
package example.micronaut;
import org.springframework.stereotype.Component;

@Component (1)
public class HelloGreeter implements Greeter {
    @Override
    public String greet() {
        return "Hello";
    }
}
1 The @Component annotation indicates that the annotated class is a component.

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 @Singleton

In Micronaut framework, we can create an implementation and annotate it with @Singleton.

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

import jakarta.inject.Singleton;

@Singleton (1)
public class HelloGreeter implements Greeter {
    @Override
    public String greet() {
        return "Hello";
    }
}
1 Use jakarta.inject.Singleton to designate a class as a singleton.

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 marking a class as a bean is similar in both frameworks. Micronaut Framework uses the standard jakarta.inject.Singleton and annotation while Spring uses custom annotations.

Micronaut Framework generates the necessary information to fulfill the injection points at compilation time.
Spring relies on classpath scanning to find classes annotated with @Component. In the Spring Boot application, HelloGreeter is detected because the application contains an Application with the @SpringBootApplication annotation. The @SpringBootApplication annotation applies the @ComponentScan annotation, which tells Spring to scan the package where the Application class is located and its sub-packages

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…​).