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.
1. We need to create four maven project, one of them is pom project as parent, the rest of them are modules within parent.
- mybatis-spring-boot
- mybatis-spring-boot-starter
- mybatis-spring-boot-autoconfigue
- mybatis-spring-boot-example
2. foo-spring-boot-starter is a module which is an empty maven project. It is responsible for managing the dependency and loading.
Create a spring.providers file in the project path: src/main/resources/META-INF
Fill spring.providers with provides: mybatis-spring-boot-autoconfigure,mybatis,mybatis-spring
The starter is really an empty jar. Its only purpose is to provide the necessary dependencies to work with the library. You can think of it as an opinionated view of what is required to get started.
If the library you are auto-configuring typically requires other starters, mention them as well. Providing a proper set of default dependencies may be hard if the number of optional dependencies is high, as you should avoid including dependencies that are unnecessary for a typical usage of the library. In other words, you should not include optional dependencies.
So, if the starter need others depeneces, they should be mentioned in here.
Either way, your starter must reference the core Spring Boot starter (spring-boot-starter
) directly or indirectly (i.e. no need to add it if your starter relies on another starter). If a project is created with only your custom starter, Spring Boot’s core features will be honoured by the presence of the core starter.
3. mybatis-spring-boot-autoconfigue is main depenecy needed by starter
3.1 Fill file spring.factories of path src/main/resources/META-INF with
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration= org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration
SpringBoot will read this file and get the first initialization class for the autoconfigure.Once the SpringBoot has the config class, it will master or manage you library in Spring way.
4. Annotations in Our own Auto Configuration, Use annotations and follow the principles of Spring to make our starter work.
@Configuration
@ConditionalOnClass({ SqlSessionFactory.class, SqlSessionFactoryBean.class })
@ConditionalOnBean(DataSource.class)
@EnableConfigurationProperties(MybatisProperties.class)
@AutoConfigureAfter(DataSourceAutoConfiguration.class)
public class MybatisAutoConfiguration{
.......
}
@Bean
@ConditionalOnMissingBean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception{
.....
}
@ConfigurationProperties(prefix = MybatisProperties.MYBATIS_PREFIX)
public class MybatisProperties{
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(MapperScannerRegistrar.class)
public @interface MapperScan{
}
5. Annotation:
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 applies only when relevant classes are found and when you have not declared your own @Configuration
You can use the @AutoConfigureAfter
or @AutoConfigureBefore
annotations if your configuration needs to be applied in a specific order.
If you want to order certain auto-configurations that should not have any direct knowledge of each other, you can also use @AutoConfigureOrder
. That annotation has the same semantic as the regular @Order
annotation but provides a dedicated order for auto-configuration classes.
The @ConditionalOnClass
and @ConditionalOnMissingClass
annotations let configuration be included based on the presence or absence of specific classes.
The @ConditionalOnBean
and @ConditionalOnMissingBean
annotations let a bean be included 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 lets you limit the ApplicationContext
hierarchy that should be considered when searching for beans.
he @ConditionalOnProperty
annotation lets configuration be included based on a Spring Environment property. Use the prefix
and name
attributes to specify the property that should be checked. By default, any property that exists and is not equal to false
is matched. You can also create more advanced checks by using the havingValue
and matchIfMissing
attributes
The @ConditionalOnResource
annotation lets configuration be included only when a specific resource is present.
The @ConditionalOnWebApplication
and @ConditionalOnNotWebApplication
annotations let configuration be included depending on whether the application is a “web application”. A web application is any application that uses a Spring WebApplicationContext
, defines a session
scope, or has a StandardServletEnvironment
Sometimes only starter is OK