3.10 Bean Configurations

A bean @Configuration is a grouping of multiple bean definitions within a package.

The @Configuration annotation is applied at the package level and informs Micronaut that the beans defined with the package form a logical grouping.

The @Configuration annotation is typically applied to package-info class. For example:

package-info.groovy

  1. @Configuration
  2. package my.package
  3. import io.micronaut.context.annotation.Configuration

Where this grouping becomes useful is when the bean configuration is made conditional via the @Requires annotation. For example:

package-info.groovy

  1. @Configuration
  2. @Requires(beans = javax.sql.DataSource)
  3. package my.package

In the above example, all bean definitions within the annotated package will only be loaded and made available if a javax.sql.DataSource bean is present. This allows you to implement conditional auto-configuration of bean definitions.

Java and Kotlin also support this functionality via package-info.java. Kotlin does not support a package-info.kt as of version 1.3.