Configuration Requirements

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

Table 1. Using @Requires
RequirementExample

Require the presence of one or more classes

@Requires(classes=javax.servlet.Servlet)

Require the absence of one or 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 it is 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