关于java:手写一个简单的SpringBoot-Starter

36次阅读

共计 2974 个字符,预计需要花费 8 分钟才能阅读完成。

当初各种自定义的 starter 不足为奇,兴许就须要本人来自定义一个 starter 了。学习一下 starter 的构建必备

starter- 定义

首先须要自顶一个配置 bean

package com.zhao.starter;

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

@EnableConfigurationProperties(StarterBean.class)
@ConfigurationProperties(prefix = "starterbean")
public class StarterBean {

    private String id;
    private String name;

    public String getId() {return id;}

    public void setId(String id) {this.id = id;}

    public String getName() {return name;}

    public void setName(String name) {this.name = name;}

    @Override
    public String toString() {
        return "StarterBean{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                '}';
    }
}

而后须要通过 Springboot 主动注入的注解,以及 Spring spi 的 spring.factories 将配置 bean 退出主动配置的治理

@Configuration
@ConditionalOnClass
public class StartBeanAutoConfiguration {
    static {System.out.println("custom auto configuration init");
    }
    @Bean
    public StarterBean starterBean(){return  new StarterBean();
    }
}

资源目录下新建 META-INF 文件夹下 spring.factories 文件,

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.zhao.starter.StartBeanAutoConfiguration

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>com.zhao</groupId>
    <artifactId>starter-demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>2.2.2.RELEASE</version>
        </dependency>
    </dependencies>

</project>

应用 maven install 之后就能够在新建我的项目时应用了。

starter 应用测试

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>com.zhao</groupId>
    <artifactId>starter-use-demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>com.zhao</groupId>
            <artifactId>starter-demo</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.2.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>2.2.2.RELEASE</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

启动类

@SpringBootApplication
public class StarterUseApplication {public static void main(String[] args) {SpringApplication.run(StarterUseApplication.class,args);
    }
}

配置 starter 须要的 bean 的配置

starterbean.id=test123
starterbean.name=zhaozhen

测试类

@RunWith(SpringRunner.class)
@SpringBootTest(classes = StarterUseApplication.class)
public class StarterUseTest {
    @Autowired
    StarterBean starterBean;
    @Test
    public void starUse(){System.out.println(starterBean);
    }
}

测试后果,失常应用

欢送搜寻关注自己与敌人共同开发的微信面经小程序【大厂面试助手】和公众号【微瞰技术】

正文完
 0