开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第15天,点击查看活动详情
虽然SpringBoot官方给我提供了许多starter启动器,但是我们在工作开发中还是避免不了自定义启动器来简化我们的开发。
为此我们通过对SpringBoot自动配置原理的分析,我们要有
- 场景启动器starter
- 自动配置包autoconfigure
starter中没有任何代码,只是引入了当前场景我们所需的依赖。和最核心的一个自动配置包。
环境搭建
首先我们创建一个空项目boot-09-customer-starter
自动配置包
创建一个自动配置包kylin-hello-spring-boot-starter-autoconfigure模块。
这里我们没有其他功能,所以没有导入其他的启动器。
启动器
接着在该项目中,创建一个启动器模块kylin-hello-spring-boot-starter。这里我们就直接创建一个Maven项目就行了(因为并不要写代码),只需引入相关依赖就行了。
爆红是因为我事先已经创好了。
接着我们要在我们的启动器中导入我们的自动配置包依赖。
<dependency>
<groupId>com.kylin</groupId>
<artifactId>kylin-hello-spring-boot-starter-autoconfigure</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
此后别人只要引入我们的场景启动器,就会自动把自动配置包给一同引入。
编写自动配置包
自动配置包的代码编写,首先我们把一些没有的依赖给删除掉。
只留下了spring-boot-starter依赖。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
接着删除了主程序类,和Test包。(用不到)
service
我们编写一个HelloService类
/**
* 默认不用注入到Spring容器中
*/
public class HelloService {
@Autowired
HelloProperties helloProperties;
public String sayHello(String userName) {
return helloProperties.getPrefix() + ":" + userName + ":" + helloProperties.getSuffix();
}
}
properties
我们创建一个HelloProperties类,通过配置文件kylin.hello属性绑定给他赋值,被HelloService给使用。
@ConfigurationProperties("kylin.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;
}
}
auto
创建一个HelloServiceAutoConfiguration自动配置类。
@Configuration
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfiguration {
@ConditionalOnMissingBean(HelloService.class)//容器中没有时生效
@Bean
public HelloService helloService() {
return new HelloService();
}
}
值得注意的是我们的HelloService类是没有主动注册到Spring容器中的。所以当容器中没有该类时,自动注入。
META-INF
通过对SpringBoot自动配置原理的分析。我们要在该包下创建一个META-INF包,在包下创建一个spring.factories文件
#Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.kylin.auto.HelloServiceAutoConfiguration
给该属性org.springframework.boot.autoconfigure.EnableAutoConfiguration赋值为我们自动配置类的位置。
Install本地仓库
接着我们先后给自动配置包,和启动器。clean,install到我们本地的Maven仓库中。
测试
创建一个boot-09-hello-testSpringboot项目,导入web启动器。
导入了我们自定义的启动器kylin-hello-spring-boot-starter
接着我们编写一个controller。
@RestController
public class HelloController {
@Autowired
HelloService helloService;
@GetMapping("/hello/{username}")
public String sayHello(@PathVariable String username) {
return helloService.sayHello(username);
}
}
给properties赋值。通过hello-kylin
kylin.hello.prefix=KYLIN
kylin.hello.suffix=666
运行主程序,访问http://localhost:8080/hello/kylin