手把手教你写一个springboot的start

一、准备部分

  • 开发工具: IDEA
  • 注意事项:官方的start都是spring-boot-start-xxx,非官方的建议是以xxx-spring-boot-start命名
  • 事情起因: 为什么自己做start,因为上周面试腾讯Csig二面被问自闭了。当然也就没有三面了,岂可修!面试官问到如果自己实现start该怎么办,完全不会备受打击。上了一周网,打了几天csgo。今天才静下心来看了看别人怎么做的,怎么实现的。目前还是对其中的一些概念还是不懂,但是能跑出来了,四舍五入就是我会了。

二、创建项目

image.png

1. pom引入的依赖

<!-- 引入依赖 -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
<!-- 增加configuration -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <!-- -->
                <configuration>
                    <!-- 启动类的路径-->
                    <mainClass>com.youchuan.test.TestApplication</mainClass>
                    <skip>true</skip>
                </configuration>
            </plugin>
        </plugins>
    </build>
复制代码

2. 项目结构

image.png

3. 新建一个TestProperties在properties文件夹下

@ConfigurationProperties 这是关键的注释

@ConfigurationProperties(prefix = "test")
public class TestProperties {

    private String name;
    private String words;
    private String toWho;
    
    // 省略getter和setter
}
复制代码

4. 新建一个TestService在service文件夹下

public class TestService {

    private String name;
    private String word;
    private String toWho;

    // 实现简单的业务: 字符串拼接
    public String OutputOnView() {
        return getName() + "say: " + getWord() + "to ---> " + getToWho();
    }
    
    // 省略getter和setter
}
复制代码

5. 新建一个TestAutoConfiguration在config文件夹下

使用注解 @Configuration 配合 @Bean 注册一个拼接字符串的bean对象
@EnableConfigurationProperties(TestProperties.class) 启用 JavatipProperties 配置类

@Configuration
@EnableConfigurationProperties(TestProperties.class)
public class TestAutoConfiguration {

    @Autowired
    private TestProperties testProperties;

    @Bean
    public TestService testService(){
        TestService service = new TestService();
        service.setName(testProperties.getName());
        service.setToWho(testProperties.getToWho());
        service.setWord(testProperties.getWords());
        return service;
    }


}
复制代码

6.最关键的部分: 在META-INF创建spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.youchuan.test.config.TestAutoConfiguration
复制代码

三、导出jar包

在终端输入mvn install打jar包

image.png

在项目文件可以找到打好的jar包

image.png

四、 新建另一个项目demo进行测试

  1. 添加test自定义的jar包
<!--省略springboot工程常规依赖-->
<dependency>
   <groupId>com.youchuan</groupId>
   <artifactId>test</artifactId>
   <version>0.0.1-SNAPSHOT</version>
</dependency>
复制代码
  1. 在yml文件中添加配置
test:
  name: youchuan
  words: hello mother f**k
  to-who: Mawangcai
复制代码

或者properties文件也行

test.name=youchuan
test.words=hello mother f**k
test.to-who=Mawangcai
复制代码
  1. 添加测试类
@RestController
public class TestMyJar {

    @Autowired
    private TestService testService;

    @GetMapping("/test")
    public String test() {
        String str = testService.OutputOnView();
        return str;
    }
}
复制代码
  1. 查看

image.png

五、进阶

来试试@ConditionalOnProperty

prefix 配置属性名称的前缀
name 配置属性完整名称或部分名称
havingValue 比较获取到的属性值与havingValue给定的值是否相同,相同才加载配置
复制代码

在TestAutoConfiguration文件上进行添加

@Configuration
@EnableConfigurationProperties(TestProperties.class)

// ========================================增加部分=====================================
@ConditionalOnProperty(
        // 配置属性名称的前缀,比如spring.http.encoding
        prefix = "test",
        // 配置属性完整名称或部分名称
        // 可与prefix组合使用,组成完整的配置属性名称,与value不可同时使用
        name = "isopen",
        // 比较获取到的属性值与havingValue给定的值是否相同,相同才加载配置
        havingValue = "true"
)
// =====================================================================================

public class TestAutoConfiguration {

    @Autowired
    private TestProperties testProperties;

    @Bean
    public TestService testService() {
        TestService service = new TestService();
        service.setName(testProperties.getName());
        service.setToWho(testProperties.getToWho());
        service.setWord(testProperties.getWords());
        return service;
    }


}
复制代码

yml文件中新增isopen属性即可

test:
  name: youchuan
  words: hello mother f**k
  to-who: Mawangcai
  isopen: true
复制代码

因为前边我们设定havingValue等于true才开始配置,所以如果isopen不为true那么配置就不会加就会报错。

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享