SpringBoot 中最强大的一个特点即是将所有的场景都抽取成了 starters
(场景启动器)
官方文档:https://docs.spring.io/spring-boot/docs/1.5.22.BUILD-SNAPSHOT/reference/html/using-boot-build-systems.html#using-boot-starter
以下是来自官网的 starters 描述:
Starters are a set of convenient dependency descriptors that you can include in your application. You get a one-stop-shop for all the Spring and related technology that you need, without having to hunt through sample code and copy paste loads of dependency descriptors. For example, if you want to get started using Spring and JPA for database access, just include the spring-boot-starter-data-jpa dependency in your project, and you are good to go.
The starters contain a lot of the dependencies that you need to get a project up and running quickly and with a consistent, supported set of managed transitive dependencies.
starters
是一组方便的依赖性描述符,可以包括在应用程序中。您可以为所有Spring和所需的相关技术提供一站式服务,而无需搜索示例代码并复制粘贴的依赖描述符负载。例如,如果您想开始使用 Spring 和 JPA 进行数据库访问,只需在项目中包含SpringBootStarte r数据 JPA 依赖项,就可以开始了。
自定义 starter 需要考虑的流程:
以 WebMvcAutoConfiguration
为例了解 starter 流程:
1、@Configuration:指定这个类是一个配置类
@ConditionalOnXXX:在指定条件成立的情况下自动配置类生效
@AutoConfigureOrder、@AutoConfigureAfter:指定自动配置类的顺序
package org.springframework.boot.autoconfigure.web;
import ... ...;
@Configuration
@ConditionalOnWebApplication
@ConditionalOnClass({Servlet.class, DispatcherServlet.class, WebMvcConfigurerAdapter.class})
@ConditionalOnMissingBean({WebMvcConfigurationSupport.class})
@AutoConfigureOrder(-2147483638)
@AutoConfigureAfter({DispatcherServletAutoConfiguration.class, ValidationAutoConfiguration.class})
public class WebMvcAutoConfiguration {
2、@Bean:给容器中添加各种组件
@Bean
@ConditionalOnMissingBean({HiddenHttpMethodFilter.class})
public OrderedHiddenHttpMethodFilter hiddenHttpMethodFilter() {
return new OrderedHiddenHttpMethodFilter();
}
@Bean
@ConditionalOnMissingBean({HttpPutFormContentFilter.class})
@ConditionalOnProperty(
prefix = "spring.mvc.formcontent.putfilter",
name = {"enabled"},
matchIfMissing = true
)
... ...
3、@ConfigurationProperties 结合相关 XXXProperties 类绑定相关配置属性
@EnableConfigurationProperties:让 XXXProperties 类生效,加入到容器中
@Configuration
@Import({WebMvcAutoConfiguration.EnableWebMvcConfiguration.class})
@EnableConfigurationProperties({WebMvcProperties.class, ResourceProperties.class})
public static class WebMvcAutoConfigurationAdapter extends WebMvcConfigurerAdapter {
@ConfigurationProperties(
prefix = "spring.mvc"
)
public class WebMvcProperties {
4、自动配置类要能加载,则需要将配置类配置在类路径下的 META-INF/spring.factories
文件中
配置规则示例:
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
starter 模式:
启动器:启动器模块是一个空的 JAR
文件,仅提供辅助性依赖管理,这些依赖可能用于自动装配或者其他类库
即:编写一个 xxx-starter
启动器,同时再编写一个 xxx-starter-autoconfigurer
作为自动配置
启动器只用来做依赖导入,专门写一个自动配置模块。同时,启动器依赖自动配置
命名规约:
官方命名空间
自定义命名空间:
1、创建一个空工程
创建 Maven 的 Modules,用作启动器
创建 spring工程的 Modules,用作自动配置
此时的工程目录如下:
2、在启动器的 pom.xml 中引入自动配置的依赖
4.0.0
com.jiker.starter
jiker-spring-boot-starter
1.0-SNAPSHOT
com.jiker.starter
jiker-spring-boot-starter-autoconfigurer
0.0.1-SNAPSHOT
3、在自动配置模块中,删除不需要的文件,如:application.properties
、主程序类
pom.xml 文件如下:
4.0.0
org.springframework.boot
spring-boot-starter-parent
1.5.21.RELEASE
com.jiker.starter
jiker-spring-boot-starter-autoconfigurer
0.0.1-SNAPSHOT
jiker-spring-boot-starter-autoconfigurer
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter
4、创建一个 Service 类
package com.jiker.starter;
public class HelloService {
HelloProperties helloProperties;
public HelloProperties getHelloProperties() {
return helloProperties;
}
public void setHelloProperties(HelloProperties helloProperties) {
this.helloProperties = helloProperties;
}
public String sayHelloJiker(String name){
return helloProperties.getPrefix() + "-" + name + helloProperties.getSuffix();
}
}
5、创建 Properties 类
package com.jiker.starter;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "jiker.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;
}
}
6、创建 AutoConfiguration 自动配置类
package com.jiker.starter;
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
HelloProperties helloProperties;
@Bean
public HelloService helloService(){
HelloService helloService = new HelloService();
helloService.setHelloProperties(helloProperties);
return helloService;
}
}
7、在类路径下创建 META-INF/spring.factories
文件
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.jiker.starter.HelloServiceAutoConfiguration
8、将 jiker-spring-boot-starter
、jiker-spring-boot-starter-autoconfigurer
安装到 Maven 仓库中
新项目需导入 Web 模块
1、在新项目 pom.xml 文件中引入自定义的 starter
org.springframework.boot
spring-boot-starter-web
com.jiker.starter
jiker-spring-boot-starter
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-test
test
2、查看 External Libraries 中是否成功导入
3、创建 Controller 类进行测试
发现 starter 中的 HelloService 类已存在提示,可以 @Autowired 自动注入
package com.jiker.springbootstartertest.controller;
import com.jiker.starter.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
HelloService helloService;
@GetMapping("/hello")
public String hello(){
return helloService.sayHelloJiker("JJJiker");
}
}
4、由于调用方法时有前后缀(prefix、suffix),可以直接在 application.properties 全局配置文件中配置
jiker.hello.prefix=Bobby
jiker.hello.suffix=Hello
5、启动应用,访问:http://localhost:8080/hello 进行测试
时间:2019.6.26 19:26