package example.micronaut;
public interface Greeter {
String greet();
}
Manually define a Bean - Spring Boot
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: 5.2.0
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:
and an implementation such as:
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.
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 verifies that it is possible to inject a bean of type Greeter in a Spring Boot application.
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
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 verifies that it is possible to inject a bean of type Greeter in a Micronaut application.
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 illustrates that the @Configuration and @Factory APIs of both frameworks are almost identical.
7. Next Steps
Read more Spring Boot to Micronaut guides.
8. License
| All guides are released with an Apache License 2.0 for the code and a Creative Commons Attribution 4.0 license for the writing and media (images). |