starter:
Starter
分析:①、编写一个自动配置类
@Configuration //指定这个类是一个配置类
@ConditionalOnXXX //在指定条件成立的情况下自动配置类生效
@AutoConfigureAfter//指定自动配置类的顺序
@Bean //给容器中添加组件
@ConfigurationPropertie //结合相关xxxProperties类来绑定相关的配置
@EnableConfigurationProperties//让xxxProperties生效加入到容器中
②、自动配置要生效需要配置在META-INF/spring.factories
#类型指定为自动配置接口
#第二行配置具体实现类
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
3、Starter
启动器的实现模式:
①、启动器只用来做依赖导入,即整个模块只需要导入其依赖的jar;
②、编写一个自动配置模块,以jar方式打包;
③、启动器依赖自动配置模块,用户只需要引入启动器(starter)就可以实现自动配置;
④、启动器模块命名规则:XXX(自定义启动器名)-spring-boot-starter(如:mybatis-spring-boot-starter;)
⑤、自动配置模块命名规则:XXX(自定义启动器名)-spring-boot-starter-autoconfigurer
starter
场景:目前某个SpringBoot应用,有一个对外的请求处理,即当用户访问 " /hello?name = xxx "时,其需要返回字符串,
" AAA xxx BBB ",即在参数name前加上 `prefix`:AAA 和 后面加上 `suffix` : BBB。
并且AAA、BBB的值可配置在application.yml配置文件中。
即需要有个启动器可以将我们需要实现我们要的业务场景,并把业务类自动配置进容器中,方便其他应用使用从容器中获取使用。
spring-boot-starter
(所有starter的基本配置)
<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.0modelVersion>
<groupId>com.wanngcw.startergroupId>
<artifactId>wanngcw-spring-boot-starter-autoconfigurerartifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>jarpackaging>
<name>wanngcw-spring-boot-starter-autoconfigurername>
<description>Wanngcw Autoconfigurer Module for Spring Bootdescription>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>1.5.10.RELEASEversion>
<relativePath/>
parent>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
dependencies>
project>
import org.springframework.boot.context.properties.ConfigurationProperties;
//需要加入该注解指定配置前缀
@ConfigurationProperties(prefix = "wangcw.hello")
public class HelloProperties {
private String prefix;
private String suffix;
public String getPrefix() {
return prefix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public String getSuffix() {
return suffix;
}
public void setSuffix(String suffix) {
this.suffix = suffix;
}
}
public class HelloService {
private HelloProperties helloProperties;
public HelloProperties getHelloProperties() {
return helloProperties;
}
public void setHelloProperties(HelloProperties helloProperties) {
this.helloProperties = helloProperties;
}
/* 实现我们的业务场景 */
public String appendString(String name){
return helloProperties.getPrefix()+":" + name + ":" + helloProperties.getSuffix();
}
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConditionalOnWebApplication //web应用才生效
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfiguration {
@Autowired
private HelloProperties helloProperties;
@Bean
public HelloService helloService(){
HelloService service = new HelloService();
service.setHelloProperties(helloProperties);
return service;
}
}
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.wangcw.starter.HelloServiceAutoConfiguration
mvn install
安装到仓库中。(1)、创建启动器配置模块,并引入wanngcw-spring-boot-starter-autoconfigurer
自动配置的依赖。
<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.0modelVersion>
<groupId>com.wangcw.startergroupId>
<artifactId>wangcw-spring-boot-starterartifactId>
<version>1.0-SNAPSHOTversion>
<dependencies>
<dependency>
<groupId>com.wanngcw.startergroupId>
<artifactId>wanngcw-spring-boot-starter-autoconfigurerartifactId>
<version>0.0.1-SNAPSHOTversion>
dependency>
dependencies>
project>
mvn install
安装到仓库中。到此,其他用户就可以通过引入该Starter
依赖,然后从容器中获取HelloService
组件实现该业务。
starter
:wangcw-spring-boot-starter
。
<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.0modelVersion>
<groupId>com.wangcw.springbootdemogroupId>
<artifactId>springbootdemoartifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>jarpackaging>
<name>springbootdemoname>
<description>Demo project for Spring Bootdescription>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.0.3.RELEASEversion>
<relativePath/>
parent>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>com.wangcw.startergroupId>
<artifactId>wangcw-spring-boot-starterartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
wangcw:
hello:
prefix: Welcome
suffix: ----HelloWorld!
@RestController
public class HelloController {
@Autowired
private HelloService helloService;
@GetMapping("/hello")
public String hello(String name){
return helloService.appendString(name);
}
}
SpringBoot官方提供了许多整合其他框架的源码,都在其GitHub中:
https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples