SpringBoot自定义starter

江枫思渺然 提交于 2019-12-01 07:55:06

SpringBoot 提供了众多的starter简化我们开发,为了更好地使用这些starter,了解其原理是很重要的,今天就来自定义一个starter。

1,如何自定义 

①,我们首先要有一个意识,springboot自己适配的一般以:spring-boot-starter-xxx命名,比如:spring-boot-starter-amqp

②,自己去适配springboot一般以:xxx-spring-boot-starter命名,比如:mybatis-spring-boot-starter

③,springboot自动适配的在依赖时一般不用加版本号,自动适配springboot 的需要版本号。因为sprinboot根本就不知道它的版本

③,通过观察mybatis-spring-boot-starter,我们发现mybatis-spring-boot-starter并没有java代码,通过观察其pom.xml我们发现,它引入了mybatis-spring-boot-autoconfigure

④,所以我们可以仿照mybatis自定义我们的starter

2,创建工程

①,创建一个空的mystart工程,新建两个模块,1,mystart;2,autoconfigurable

3,mystart模块配置

①,mystart依赖于autoconfigurable模块

 <groupId>com.spring.start</groupId>
    <artifactId>mystart</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>com.spring.start.autoconfigurable</groupId>
            <artifactId>autoconfigurable</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

4,autoconfigurable工程代码

①,pom.xml

    <groupId>com.spring.start.autoconfigurable</groupId>
	<artifactId>autoconfigurable</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>autoconfigurable</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.0.RELEASE</version>
	</parent>

	<dependencies>
        <--依赖于springboot starter-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>
	</dependencies>

②,编写xxxProperties类,类似于ServerProperties

import org.springframework.boot.context.properties.ConfigurationProperties;

//这样才能读取到application.properties 中的相应配置
@ConfigurationProperties("spring.su")
public class SuProperties {

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

③,编写功能类

public class SuService {
    //这里不用@Autowired注入,会在自动配置类注入时给设置,所以才有setSuProperties 方法
    private SuProperties suProperties;

    public String hello(String name){
        return suProperties.getPrefix()+"--"+name+"=="+suProperties.getSuffix();
    }


    public SuProperties getSuProperties() {
        return suProperties;
    }

    public void setSuProperties(SuProperties suProperties) {
        this.suProperties = suProperties;
    }
}

④,编写自动配置类

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
//在web应用中起效
@ConditionalOnWebApplication
//指明配置类,这样SuProperties 才能被注入
@EnableConfigurationProperties({SuProperties.class})
public class SuAutoConfiguration {

    @Autowired
    private SuProperties suProperties;

    @Bean
    public SuService suService(){
        SuService suService = new SuService();
        suService.setSuProperties(suProperties);
        return suService;
    }
}

⑤,添加对应的 META-INF/spring.factories

⑥,spring.factories 内容,这样springboot在启动时会就扫描对应的AutoConfiguration,为什么会这样大家知道吗?参考

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.spring.start.autoconfigurable.SuAutoConfiguration

5,mystart工程代码

①,pom.xml 依赖于autoConfiguration 工程即可

    <groupId>com.spring.start</groupId>
    <artifactId>mystart</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>com.spring.start.autoconfigurable</groupId>
            <artifactId>autoconfigurable</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

6,测试工程代码编写

①,pom.xml

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.0.RELEASE</version>
	</parent>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
        <--依赖mystart 即可,mystart 会依赖autoconfigurable 工程,注意这里要指定版本号-->
		<dependency>
			<groupId>com.spring.start</groupId>
			<artifactId>mystart</artifactId>
			<version>1.0-SNAPSHOT</version>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

②,application.properties

spring.su.prefix=小美女
spring.su.suffix=错过了

③,测试类

import com.spring.start.autoconfigurable.SuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HomeController {

    @Autowired
    private SuService suService;

    @RequestMapping("/")
    public String index(){
        return suService.hello("小苏");
    }
}

④,启动工程,然后测试即可,

⑤,遗留问题,在application.properties 写的中文会乱码,暂时没有找到解决的办法,知道如何的同学欢迎留言,感谢!!!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!