Springboot自定义starter(启动器)

Springboot自定义starter(启动器)

1. 创建一个空的project

在这里插入图片描述

2. 在空的project下创建两个module:一个为maven项目,一个为springboot项目(为了方便)

在这里插入图片描述

3. 在maven项目的pom依赖中引入springboot项目自动配置(这其实就是一个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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.xiaobai</groupId>
    <artifactId>xiaobai-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>

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

        <!--引入自动配置  -->
        <dependency>
            <groupId>com.xiaobai</groupId>
            <artifactId>xiaobai-spring-boot-starter-autoconfig</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

    
</project>

4. 在springboot项目中创建如下的三个类和spring.factories配置文件

在这里插入图片描述

5. 删除掉springboot启动类并创建好上述三个类

HelloService类

/**
 * @author 小白学长
 * @create 2020-08-08 11:32
 *  这是一个service类 创建了一个helloWorld方法
 */
public class HelloService {
	// 引入了helloproperties 并提供了get set方法
    HelloProperties properties;

    public HelloProperties getProperties() {
        return properties;
    }

    public void setProperties(HelloProperties properties) {
        this.properties = properties;
    }
    // 返回前缀+name+后缀
    public String helloWorld(String name){
        return properties.getPerfix() + name + properties.getSuffix();
    }
}

HelloProperties类

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

/**
 * @author 小白学长
 * @create 2020-08-08 11:33
 */
 // 指明配置内容的前缀
@ConfigurationProperties(prefix = "xiaobai.hello")
public class HelloProperties {

    private String perfix;
    private String suffix;

    public String getPerfix() {
        return perfix;
    }

    public void setPerfix(String perfix) {
        this.perfix = perfix;
    }

    public String getSuffix() {
        return suffix;
    }

    public void setSuffix(String suffix) {
        this.suffix = suffix;
    }
}

ServiceAutoConfiguration类

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;

/**
 * @author 小白学长
 * @create 2020-08-08 11:37
 */
@Configuration // 表明是个配置类
@ConditionalOnWebApplication // 表示在web应用情况下才生效
@EnableConfigurationProperties(HelloProperties.class) // 自动配置
public class ServiceAutoConfiguration {

    @Autowired
    HelloProperties properties;
    @Bean
    public HelloService service(){
        // 创建一个helloservice
        HelloService service = new HelloService();
        // 将properties的信息set进去
        service.setProperties(properties);
        // 将service返回
        return service;
    }
}

6. 在resources下创建META-INF文件加并创建spring.factories文件

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
// 自己的自动配置类的全路径
com.xiaobai.xiaobaispringbootstarterautoconfig.start.ServiceAutoConfiguration

7. install两个module

在这里插入图片描述

8. 创建一个新的springboot项目,测试

引入自己的启动器依赖

		<dependency>
			<groupId>com.xiaobai</groupId>
			<artifactId>xiaobai-spring-boot-starter-autoconfig</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>

在配置文件中配置如下内容
在这里插入图片描述
编写controller类,测试,查看输出内容

import com.xiaobai.xiaobaispringbootstarterautoconfig.start.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author 小白学长
 * @create 2020-08-08 11:58
 */
@Controller
@ResponseBody
public class HelloController {

    @Autowired
    HelloService helloService;

    @GetMapping("/hello")
    public String hello(){
        return helloService.helloWorld("小白学长");
    }

}

运行测试结果如下:
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值