spring-boot 在配置上相比spring要简单许多, 其核心在于spring-boot-starter, 在使用spring-boot来搭建一个项目时, 只需要引入官方提供的starter, 就可以直接使用, 免去了各种配置, 原因在于spring-boot的自动发现,比如当classpath下面有tomcat-embedded.jar 时,对应的bean就会被加载.下面会介绍如何自己写一个简单的starter,并在自己的工程中使用这个starter
新建一个maven project结构如下
其中
GreetorService
是我们要提供给外部使用的bean, GreetorProperties
包含了这个bean需要的信息, GreetorAutoConfiguration
负责提供这个bean下面依次看一下各个class
public class GreetorService {
private GreetorProperties properties;
public GreetorService(GreetorProperties properties) {
this.properties = properties;
}
public String greet() {
return "greet by" + properties.getName() + " msg " + properties.getMsg();
}
}
GreetorService , 一个简单的service
@ConfigurationProperties(prefix="greetor")
public class GreetorProperties {
private String name;
private String msg;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
properties bean, 会从application.properties 或 yml中把对应的properties映射到bean中
@ConditionalOnClass(GreetorService.class)
@EnableConfigurationProperties(GreetorProperties.class)
@Configuration
public class GreetorAutoConfiguration {
@Autowired
private GreetorProperties greetorProperties;
@Bean
@ConditionalOnMissingBean(GreetorService.class)
public GreetorService greetorService() {
return new GreetorService(greetorProperties);
};
}
最为重要的class, @ConditionalOnClass 只有当特定的class在classpath上时, BeanDefination才会被注册, @ConditionalOnMissingBean 同理, @EnableConfigurationProperties 这个用于激活PropertyBean
在META-INF中新建一个名为spring.factories的文件 (下文介绍其作用),内容为
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.iplay.springtest.configuration.GreetorAutoConfiguration
最后看一下依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.iplay.springtest</groupId>
<artifactId>greeter-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>starter-greetor</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
之后我们新建另一个maven project, 并依赖我们刚才建立的starter
测试类 如下
@SpringBootApplication
public class StarterGreetorUsageApplication implements CommandLineRunner {
@Autowired
private GreetorService greetorService;
@Override
public void run(String... args) throws Exception {
System.out.println(greetorService.greet());
}
public static void main(String args[]) {
new SpringApplication(StarterGreetorUsageApplication.class).run(args);
}
}
在application.properties中增加如下内容
greetor.name=test
greetor.msg=hello
运行application 可以看到以下信息
greet bytest msg hello
下面我们看一下为什么GreetorAutoConfiguration
会得到处理
@SpringBootApplication 包含了@EnableAutoConfiguration, @EnableAutoConfiguration包含了@Import(AutoConfigurationImportSelector.class)
@Import可以接受一个ImportSelector
而AutoAutoConfigurationImportSelector
是ImportSelector
的实现
其中核心代码为
决定了哪些configuration会被import
这里调用了
getCandidateConfigurations
DeepinScrot-1252.png
SpringFactoriesLoader
会从META-INF/spring.factories中读取对应的factory, 所以当我们启动项目时,会检查META-INF/spring.factories key为org.springframework.boot.autoconfigure.EnableAutoConfiguration
的值.在spring-boot-autoconfigure这个包中也包含了spring.factories, 部分值为下
DeepinScrot-2119.png
对应了各个starter的autoconfiguration, 这些autoconfiguration使我们只需引入各个starter就能够快速搭建一个spring-boot app
网友评论