3.1 Defining Beans

A bean is an object that has its lifecycle controlled by the Micronaut IoC container. That lifecycle may include creation, execution, and destruction. Micronaut implements the JSR-330 (javax.inject) - Dependency Injection for Java specification hence to use Micronaut you simply use the annotations provided by javax.inject.

The following is a simple example:

  1. public interface Engine { (1)
  2. int getCylinders();
  3. String start();
  4. }
  5. @Singleton(2)
  6. public class V8Engine implements Engine {
  7. @Override
  8. public String start() {
  9. return "Starting V8";
  10. }
  11. @Override
  12. public int getCylinders() {
  13. return cylinders;
  14. }
  15. public void setCylinders(int cylinders) {
  16. this.cylinders = cylinders;
  17. }
  18. private int cylinders = 8;
  19. }
  20. @Singleton
  21. public class Vehicle {
  22. private final Engine engine;
  23. public Vehicle(Engine engine) {(3)
  24. this.engine = engine;
  25. }
  26. public String start() {
  27. return engine.start();
  28. }
  29. }
  1. interface Engine { (1)
  2. int getCylinders()
  3. String start()
  4. }
  5. @Singleton (2)
  6. class V8Engine implements Engine {
  7. int cylinders = 8
  8. @Override
  9. String start() {
  10. "Starting V8"
  11. }
  12. }
  13. @Singleton
  14. class Vehicle {
  15. final Engine engine
  16. Vehicle(Engine engine) { (3)
  17. this.engine = engine
  18. }
  19. String start() {
  20. engine.start()
  21. }
  22. }
  1. interface Engine {
  2. (1)
  3. val cylinders: Int
  4. fun start(): String
  5. }
  6. @Singleton(2)
  7. class V8Engine : Engine {
  8. override var cylinders = 8
  9. override fun start(): String {
  10. return "Starting V8"
  11. }
  12. }
  13. @Singleton
  14. class Vehicle(private val engine: Engine)(3)
  15. {
  16. fun start(): String {
  17. return engine.start()
  18. }
  19. }
1A common Engine interface is defined
2A V8Engine implementation is defined and marked with Singleton scope
3The Engine is injected via constructor injection

To perform dependency injection simply run the BeanContext using the run() method and lookup a bean using getBean(Class), as per the following example:

  1. final BeanContext context = BeanContext.run();
  2. Vehicle vehicle = context.getBean(Vehicle.class);
  3. System.out.println(vehicle.start());
  1. def context = BeanContext.run()
  2. Vehicle vehicle = context.getBean(Vehicle)
  3. println( vehicle.start() )
  1. val context = BeanContext.run()
  2. val vehicle = context.getBean(Vehicle::class.java)
  3. println(vehicle.start())

Micronaut will automatically discover dependency injection metadata on the classpath and wire the beans together according to injection points you define.

Micronaut supports the following types of dependency injection:

  • Constructor injection (must be one public constructor or a single constructor annotated with @Inject)

  • Field injection

  • JavaBean property injection

  • Method parameter injection