3.8 Conditional Beans

At times you may want a bean to load conditionally based on various potential factors including the classpath, the configuration, the presence of other beans etc.

The Requires annotation provides the ability to define one or many conditions on a bean.

Consider the following example:

Using @Requires

  1. @Singleton
  2. @Requires(beans = DataSource.class)
  3. @Requires(property = "datasource.url")
  4. public class JdbcBookService implements BookService {
  5. DataSource dataSource;
  6. public JdbcBookService(DataSource dataSource) {
  7. this.dataSource = dataSource;
  8. }

Using @Requires

  1. @Singleton
  2. @Requires(beans = DataSource.class)
  3. @Requires(property = "datasource.url")
  4. class JdbcBookService implements BookService {
  5. DataSource dataSource

Using @Requires

  1. @Singleton
  2. @Requirements(Requires(beans = [DataSource::class]), Requires(property = "datasource.url"))
  3. class JdbcBookService(internal var dataSource: DataSource) : BookService {

The above bean defines two requirements. The first indicates that a DataSource bean must be present for the bean to load. The second requirement ensures that the datasource.url property is set before loading the JdbcBookService bean.

Kotlin currently does not support repeatable annotations. Use the @Requirements annotation when multiple requires are needed. For example, @Requirements(Requires(…​), Requires(…​)). See https://youtrack.jetbrains.com/issue/KT-12794 to track this feature.

If you have multiple requirements that you find you may need to repeat on multiple beans then you can define a meta-annotation with the requirements:

Using a @Requires meta-annotation

  1. @Documented
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Target({ElementType.PACKAGE, ElementType.TYPE})
  4. @Requires(beans = DataSource.class)
  5. @Requires(property = "datasource.url")
  6. public @interface RequiresJdbc {
  7. }

Using a @Requires meta-annotation

  1. @Documented
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Target([ElementType.PACKAGE, ElementType.TYPE])
  4. @Requires(beans = DataSource.class)
  5. @Requires(property = "datasource.url")
  6. @interface RequiresJdbc {
  7. }

Using a @Requires meta-annotation

  1. @Documented
  2. @Retention(AnnotationRetention.RUNTIME)
  3. @Target(AnnotationTarget.CLASS, AnnotationTarget.FILE)
  4. @Requirements(Requires(beans = [DataSource::class]), Requires(property = "datasource.url"))
  5. annotation class RequiresJdbc

In the above example an annotation called RequiresJdbc is defined that can then be used on the JdbcBookService instead:

Using a meta-annotation

  1. @RequiresJdbc
  2. public class JdbcBookService implements BookService {
  3. ...
  4. }

If you have multiple beans that need to fulfill a given requirement before loading then you may want to consider a bean configuration group, as explained in the next section.

Configuration Requirements

The @Requires annotation is very flexible and can be used for a variety of use cases. The following table summarizes some of the possibilities:

Table 1. Using @Requires
RequirementExample

Require the presence of one ore more classes

@Requires(classes=javax.servlet.Servlet)

Require the absence of one ore more classes

@Requires(missing=javax.servlet.Servlet)

Require the presence one or more beans

@Requires(beans=javax.sql.DataSource)

Require the absence of one or more beans

@Requires(missingBeans=javax.sql.DataSource)

Require the environment to be applied

@Requires(env=”test”)

Require the environment to not be applied

@Requires(notEnv=”test”)

Require the presence of another configuration package

@Requires(configuration=”foo.bar”)

Require the absence of another configuration package

@Requires(missingConfigurations=”foo.bar”)

Require particular SDK version

@Requires(sdk=Sdk.JAVA, value=”1.8”)

Requires classes annotated with the given annotations to be available to the application via package scanning

@Requires(entities=javax.persistence.Entity)

Require a property with an optional value

@Requires(property=”data-source.url”)

Require a property to not be part of the configuration

@Requires(missingProperty=”data-source.url”)

Require the presence of one or more files in the file system

@Requires(resources=”file:/path/to/file”)

Require the presence of one or more classpath resources

@Requires(resources=”classpath:myFile.properties”)

Require the current operating system to be in the list

@Requires(os={Requires.Family.WINDOWS})

Require the current operating system to not be in the list

@Requires(notOs={Requires.Family.WINDOWS})

Additional Notes on Property Requirements.

Adding a requirement on a property has some additional functionality. You can require the property to be a certain value, not be a certain value, and use a default in those checks if its not set.

  1. @Requires(property="foo") (1)
  2. @Requires(property="foo", value="John") (2)
  3. @Requires(property="foo", value="John", defaultValue="John") (3)
  4. @Requires(property="foo", notEquals="Sally") (4)
1Requires the property to be set
2Requires the property to be “John”
3Requires the property to be “John” or not set
4Requires the property to not be “Sally” or not set

Debugging Conditional Beans

If you have multiple conditions and complex requirements it may become difficult to understand why a particular bean has not been loaded.

To help resolve issues with conditional beans you can enable debug logging for the io.micronaut.context.condition package which will log the reasons why beans were not loaded.

logback.xml

  1. <logger name="io.micronaut.context.condition" level="DEBUG"/>