If you work in a company that develops shared libraries, or if you work on an open-source or commercial library, you might want to develop your own auto-configuration. Auto-configuration classes can be bundled in external jars and still be picked-up by Spring Boot.
Under the hood, auto-configuration is implemented with standard @Configuration
classes.
Additional @Conditional
annotations are used to constrain when the auto-configuration
should apply. Usually auto-configuration classes use @ConditionalOnClass
and
@ConditionalOnMissingBean
annotations. This ensures that auto-configuration only
applies when relevant classes are found and when you have not declared your own
@Configuration
.
You can browse the source code of spring-boot-autoconfigure
to see the @Configuration
classes that we provide (see the META-INF/spring.factories
file).
Spring Boot checks for the presence of a META-INF/spring.factories
file within your
published jar. The file should list your configuration classes under the
EnableAutoConfiguration
key.
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.mycorp.libx.autoconfigure.LibXAutoConfiguration,\ com.mycorp.libx.autoconfigure.LibXWebAutoConfiguration
You can use the
@AutoConfigureAfter
or
@AutoConfigureBefore
annotations if your configuration needs to be applied in a specific order. For example,
if you provide web-specific configuration, your class may need to be applied after
WebMvcAutoConfiguration
.
You almost always want to include one or more @Condition
annotations on your
auto-configuration class. The @ConditionalOnMissingBean
is one common example that is
used to allow developers to ‘override’ auto-configuration if they are not happy with
your defaults.
Spring Boot includes a number of @Conditional
annotations that you can reuse in your own
code by annotating @Configuration
classes or individual @Bean
methods.
The @ConditionalOnClass
and @ConditionalOnMissingClass
annotations allows configuration
to be skipped based on the presence or absence of specific classes. Due to the fact that
annotation metadata is parsed using ASM you can actually use the
value
attribute to refer to the real class, even though that class might not actually
appear on the running application classpath. You can also use the name
attribute if you
prefer to specify the class name using a String
value.
The @ConditionalOnBean
and @ConditionalOnMissingBean
annotations allow configurations
to be skipped based on the presence or absence of specific beans. You can use the value
attribute to specify beans by type, or name
to specify beans by name. The search
attribute allows you to limit the ApplicationContext
hierarchy that should be considered
when searching for beans.
Note | |
---|---|
|
The @ConditionalOnResource
annotation allows configuration to be included only when a
specific resource is present. Resources can be specified using the usual Spring
conventions, for example, file:/home/user/test.dat
.
The @ConditionalOnWebApplication
and @ConditionalOnNotWebApplication
annotations
allow configuration to be skipped depending on whether the application is a
'web application'. A web application is any application that is using a Spring
WebApplicationContext
, defines a session
scope or has a StandardServletEnvironment
.
The @ConditionalOnExpression
annotation allows configuration to be skipped based on the
result of a SpEL expression.