自定义一个简单的starter
1.创建一个空项目 分别添加maven和spring Initializr模块
maven模块作为启动器用来做依赖导入
springInitializr模块作为自动配置模块
注:启动器模块是一个空 JAR 文件,仅提供辅助性依赖管理,这些依赖可能用于自动 装配或者其他类库
命名规约: 非官方自定义命名空间 模块-spring-boot-starter
创建空项目
添加maven模块和spring Initializr模块
maven模块作为启动器只做依赖导入
启动器pom文件引入自动配置模块:(引入自动配置模块)
<?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>
<groupId>study</groupId>
<artifactId>study-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 这是启动器 -->
<dependencies>
<dependency>
<!-- 引入自动配置模块 -->
<groupId>study</groupId>
<artifactId>study-spring-boot-starter-autoconfigurer</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
spring Initializr模块作为自动配置模块
自动配置模块pom文件:(只保留了spring-boot-starter的引入)
<?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 https://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.2.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>study</groupId>
<artifactId>study-spring-boot-starter-autoconfigurer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>study-spring-boot-starter-autoconfigurer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- spring-boot-starter是所有starter的基本配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
</project>
2.编写自动配置
编写后的结构如下:
HelloService
package study.starter;
public class HelloService {
HelloProperties helloProperties;
public String hello(){
return helloProperties.getMsg();
}
public HelloProperties getHelloProperties() {
return helloProperties;
}
public void setHelloProperties(HelloProperties helloProperties) {
this.helloProperties = helloProperties;
}
}
HelloProperties
package study.starter;
import org.springframework.boot.context.properties.ConfigurationProperties;
//绑定配置文件以study.hello开头的所有配置
//告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定
//从配置文件中获取指定的值和bean的属性进行绑定
@ConfigurationProperties(prefix = "study.hello")
public class HelloProperties {
private String msg;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
HelloServiceAutoConfiguration
package study.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;
//指明当前类是一个配置类;就是来替代之前的Spring配置文件
@Configuration
//Spring底层@Conditional注解,判断当前应用是否是web应用,若是则当前配置类生效
@ConditionalOnWebApplication
//将配置文件中对应的值和HelloProperties绑定起来;并把HelloProperties加入到ioc容器中
//绑定配置
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfiguration {
//已和SpringBoot的配置文件映射完成
@Autowired
HelloProperties helloProperties;
//类似于在配置文件中用<bean><bean/>标签添加组件
//容器中这个组件默认的id就是方法名
//这个组件的某些值需从properties中获取
@Bean
public HelloService helloService(){
HelloService service = new HelloService();
service.setHelloProperties(helloProperties);
return service;
}
}
将标注@Configuration的自动配置类,放在classpath下METAINF/spring.factories文件中
spring.factories
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
study.starter.HelloServiceAutoConfiguration
3.安装至仓库
4.在springweb项目中引入使用
<!-- 引入自定义start -->
<dependency>
<groupId>study</groupId>
<artifactId>study-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
引入效果:
application.properties:
study.hello.msg=DayDayUp
HelloContrller:
package cronos.study_demo_test.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import study.starter.HelloService;
@RestController
public class HelloController {
@Autowired
HelloService helloService;
@GetMapping("/hello")
public String hello(){
return helloService.hello();
}
}
结果: