springboot自定义starter

springboot场景启动器starter

学习了springboot的知识,编写一个自定义的starter,把流程记录下来

场景启动器starter

引入这个场景启动器需要了解的信息

  1. 这个场景需要使用到的依赖是什么?需要导入那些jar包?
  2. 如何编写自动配置
参考WebMvcAutoConfiguration:
@Configuration
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class })
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
@AutoConfigureAfter({ DispatcherServletAutoConfiguration.class,
		TaskExecutionAutoConfiguration.class, ValidationAutoConfiguration.class })
  public class WebMvcAutoConfiguration {
    
  }

@Configuration  //指定这个类是一个配置类
@ConditionalOnXXX  //在指定条件成立的情况下自动配置类生效
@AutoConfigureAfter  //指定自动配置类的顺序
@Bean  //给容器中添加组件

@ConfigurationPropertie结合相关xxxProperties类来绑定相关的配置
@EnableConfigurationProperties //让xxxProperties生效加入到容器中
自动配置类要能加载
将需要启动就加载的自动配置类,配置在META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
  1. 模式
    启动器(starter)
    启动器模块是一个空的JAR文件,仅提供辅助性依赖管理,这些依赖可能用于自动装配或者其他类库。

    				XXX-starter
    					   |
    	XXX-starter-autoconfigure
    


starter:启动器只用来做依赖导入;
autoconfigure :自动配置模块;
启动器依赖自动配置;使用只需要引入启动器(starter)

命名规则:
以mybatis-spring-boot-starter为例;
通常为:自定义启动器名-spring-boot-starter

创建项目步骤

1.创建一个空的目录springboot自定义starter
2.分别创建一个starter项目和autoconfigure项目springboot自定义starter
3.启动器模块

<?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>com.mdh.starter</groupId>
    <artifactId>mdh-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!--启动器-->
    <dependencies>

        <!--引入自动配置模块-->
        <dependency>
            <groupId>com.mdh.starter</groupId>
            <artifactId>mdh-spring-boot-starter-autoconfigure</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

</project>

4.自动配置模块

<?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>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.3.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.mdh.starter</groupId>
	<artifactId>mdh-spring-boot-starter-autoconfigure</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>mdh-spring-boot-starter-autoconfigure</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>

删除test包,Application启动类和resource里的文件。
然后在resource文件夹下创建META-INF目录,里面创建spring.factories文件

5.整个的目录结构
springboot自定义starter
在spring.factories添加

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.mdh.starter.HelloServiceAutoConfiguration

6.在上面的红框里面填写自己需要实现的方法

(1) HelloProperties.java

@ConfigurationProperties(prefix = "mdh.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;
    }
}

(2) HelloService.java

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();
    }
}

(3) HelloProperties.java

@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.打包完成
springboot自定义starter
8.在新建一个项目
在pom文件中引入:

<!--引入自定义starter-->
		<dependency>
			<groupId>com.mdh.starter</groupId>
			<artifactId>mdh-spring-boot-starter</artifactId>
			<version>1.0-SNAPSHOT</version>
		</dependency> 

springboot自定义starter
编写一个用例测试:
HelloController.java

@RestController
public class HelloController {

    @Autowired
    HelloService helloService;

    @GetMapping(value = "hello")
    public String hello(){
        return helloService.sayHello("马东豪");
    }
}

application.properties

mdh.hello.prefix = hello!
mdh.hello.suffix = ,happy!

9.运行结果
springboot自定义starter

代码:https://github.com/mdh-git/spring-boot-starter.git