1.概述
大多数Spring Boot开源项目都提供了Spring Boot Starters,我们也可以创建自定义的starters,通过starter能够使库的使用者避免去写冗长的配置文件,并能够快速进行开发。
2.Spring Boot自动配置揭秘
2.1 自动配置类
这种类的作用是完成对象的创建及配置。Spring boot启动后,会在classpath中寻找spring.factories文件。这个文件在META-INF目录下。该文件的配置示例如下:
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\
org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration,\
org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration,\
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration
- 1
- 2
- 3
- 4
- 5
- 6
如上的配置例子指定了多个配置类,并通过逗号(,)分隔。Spring Boot启动后会运行RabbitMQ,Cassandra,MongoDB及Hibernate的配置类,以创建这些类的实例。
最终是否通过这些配置类完成自动配置及bean的创建取决于在classpath中能否找到这些类。例如,在classpth中如果找到了MongoDB的类,那么通过MongoAutoConfiguration完成MongoDB的创建。
若创建bean时希望满足条件时才创建,则可以通过条件注解来完成。下面的例子中条件初始化是通过 @ConditionalOnClass注解来启用的。
例:
@Configuration
@ConditionalOnClass(MongoClient.class)
@EnableConfigurationProperties(MongoProperties.class)
@ConditionalOnMissingBean(type = "org.springframework.data.mongodb.MongoDbFactory")
public class MongoAutoConfiguration {
// configuration code
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
@ConditionalOnBean(仅仅在当前上下文中存在某个对象时,才会实例化一个Bean)
@ConditionalOnClass(某个class位于类路径上,才会实例化一个Bean)
@ConditionalOnExpression(当表达式为true的时候,才会实例化一个Bean)
@ConditionalOnMissingBean(仅仅在当前上下文中不存在某个对象时,才会实例化一个Bean)
@ConditionalOnMissingClass(某个class类路径上不存在的时候,才会实例化一个Bean)
@ConditionalOnNotWebApplication(不是web应用)
2.2. 通过 application.properties自定义属性
当创建一个实例时,希望通过配置文件为其指定属性,那么可以通过在application.properties定义属性配置来完成。
例:
@ConfigurationProperties(prefix = "spring.data.mongodb")
public class MongoProperties {
private String host;
// other fields with standard getters and setters
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
#application.properties
spring.data.mongodb.host = localhost
- 1
- 2
例子中,在MongoProperties类上使用了@EnableConfigurationProperties注解,最终会在application.properties中寻找前缀为spring.data.mongodb的配置信息,并注入MongoProperties的成员中。
3.创建自定义starter
创建自定义的starter需要完成下面两项工作。
- 编写自动配置类
- 编写starter pom
现在名为autoConfigure项目下有ServerX类,现需要创建starter提供给第3方使用。
public class ServerX {
private String name = "ServerX";
public ServerX(String name) {
this.name = name;
}
public ServerX() {
}
@Override
public String toString() {
return "ServerX{" + "name='" + name + '\'' + '}';
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
3.1 创建配置类ServerConfiguration
public class Server {
private String name = "Server";
public Server() {
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
@Configuration
@ConditionalOnClass(ServerX.class)
public class ServerConfiguration {
@Bean
public ServerX server() {
return new ServerX();
}
}
/***
*
*@ConfigurationProperties(prefix = "xxx.com")
*public class XXXProperties {
* private String host;
* // standard getters and setters
*
*}
*@EnableConfigurationProperties(XXXProperties.class)
*public class GreeterAutoConfiguration {
*
* @Autowired
* private XXXProperties xxxProperties;
*
* @Bean
* @ConditionalOnMissingBean
* public ServerX serverX(XXXProperties xxxProperties) {
* return new Greeter(xxxProperties);
* }
*/
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
如果希望通过配置文件来设置属性,那么给Server类指定一个属性对象即可,然后通过ServerConfiguration类创建ServerX时为其设置属性对象即可。
在src/main/resource/META-INF目录下添加spring.factories文件,然后配置好自定义的配置类。
org.springframework.boot.autoconfigure.EnableAutoConfiguration=service.ServerConfiguration
- 1
3.2 创建pom
依据命名规范,所有的不是通过Spring boot 团队管理的starter,命名时需要以包名开头,并带上-spring-boot-starter后缀。因此将我们的starter命名为serverx-spring-boot-starter。
<groupId>com.xxx</groupId>
<artifactId>serverx-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
- 1
- 2
- 3
剩余的配置就是该项目所需要的依赖,例如spring-boot基础依赖,和你自己开发所需要的一些依赖。
3.3 使用
引入starter依赖
<groupId>com.xxx</groupId>
<artifactId>serverx-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
- 1
- 2
- 3
使用
@SpringBootApplication
public class StartupRunner implements CommandLineRunner {
@Autowired
ServerX server;
@Override
public void run(String... strings) throws Exception {
System.out.println(server);
}
public static void main(String[] args) {
SpringApplication.run(StartupRunner.class, args);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
4.参考
1.http://www.baeldung.com/spring-boot-custom-starter
2.https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/
原文地址:https://blog.csdn.net/revivedsun/article/details/77151899