启动器模块是一个 空 jar 文件,仅提供辅助性依赖管理,这些依赖可能用于自动装配或者其他类库;
官方命名:
- 前缀:spring-boot-starter-xxx
- 比如:spring-boot-starter-web…
自定义命名
- xxx-spring-boot-starter
- 比如:mybatis-spring-boot-starter
2.编写启动器
- 在idea新建一个空项目:
- 在空项目中新建一个普通的maven模块:chen-spring-boot-starter2
- 新建一个Springboot模块(用于自动配置):chen-spring-boot-starter-autoconfigurer
点击apply即可
- 在启动器starter中导入autoconfigurer的依赖
- 在autoconfigurer项目中的依赖都删除,只留下一个starter。
6.在autoconfigurer项目中编写自己的服务
package com.chen.chenspringbootstartreautoconfigure.service;
/**
* Created on 2020/8/14.
*
* @author Chen_Peng
*/
public class HelloService {
HelloProperties helloProperties;
public HelloProperties getHelloProperties() {
return helloProperties;
}
public void setHelloProperties(HelloProperties helloProperties) {
this.helloProperties = helloProperties;
}
public String sayHello(String name){
return helloProperties.getPrefix() + name + helloProperties.getSuffix();
}
}
- 编写HelloProperties配置类
package com.chen.chenspringbootstartreautoconfigure.service;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* Created on 2020/8/14.
*
* @author Chen_Peng
*/
@ConfigurationProperties(prefix = "chen.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;
}
}
- 编写自动配置类注入bean
package com.chen.chenspringbootstartreautoconfigure.service;
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;
}
}
- 在resources编写一个自己的 META-INF\spring.factories将配置类的路径写入
- 完成之后,使用maven工具安装到maven仓库
新建项目测试我们自己写的启动器
- 新建一个SpringBoot 项目
- 导入我们自己写的启动器
<dependency>
<groupId>org.example</groupId>
<artifactId>chen-spring-boot-starter2</artifactId>
</dependency>
- 编写一个HelloController接口,
package com.chen.springboot.controller;
import com.chen.chenspringbootstartreautoconfigure.service.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(){
String hello = helloService.sayHello("张三");
return hello;
}
}
- 编写配置文件 application.properties
- 启动项目测试
成功!