Java

重温面试现场:如何实现 SpringBoot 自定义 Start

2022-05-14  本文已影响0人  熬夜不加班

如何实现自定义Starter

先创建一个父工程

image.png

推荐学习:谈起SpringBoot,面试官(Java)必问题:讲述一下 SpringBoot 自动装配原理?

我们会把自定义starter放到这个父工程里面,然后建立一个子工程去引入那个自定义starter

image.png

自定义Starter的命名规范

开始创建自定义Starter

image.png

引入依赖

<?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.6.4</version>
    </parent>
    <groupId>linc.fun</groupId>
    <artifactId>linc-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <spring-boot.version>2.6.4</spring-boot.version>
    </properties>
    <dependencies>
        <!-- 引入autoconfigure-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <!--SpringBootWeb-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>${spring-boot.version}</version>
        </dependency>
    </dependencies>
    <repositories>
        <repository>
            <id>springsource-repos</id>
            <name>SpringSource Repository</name>
            <url>http://repo.spring.io/release/</url>
        </repository>
        <repository>
            <id>activiti-repos2</id>
            <name>Activiti Repository 2</name>
            <url>https://app.camunda.com/nexus/content/groups/public</url>
        </repository>
        <repository>
            <id>central-repos</id>
            <name>Central Repository</name>
            <url>http://repo.maven.apache.org/maven2</url>
        </repository>
    </repositories>
</project>

我们自定义一个配置文件

@ConfigurationProperties("linc.properties")
public class MyProperties {
    @Value("${linc.properties.name}")
    private  String name;

    public String getName() {
        return name;
    }

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

自定义一个Controller

@RestController
public class MyController {
    @Resource
    private MyProperties myProperties;

    @RequestMapping("/my")
    public String my() {
        System.out.println("my....");
        return myProperties.getName() + ",正在测试...";
    }
}

定义一个自动配置类

@Configuration
@ConditionalOnProperty(value = "linc.properties.name")
@EnableConfigurationProperties(MyProperties.class)
public class MyAutoConfiguration {

}

在resources目录下创建META-INF/spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  linc.fun.MyAutoConfiguration

到这儿,我们的配置自定义的starter就写完了

image.png

我们进行打包

image.png

创建另外一个子项目,引入我们这个自定义Starter

image.png

引入依赖

<?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>linc.fun</groupId>
    <artifactId>linc-project</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>linc.fun</groupId>
            <artifactId>linc-spring-boot-starter</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

创建启动类以及配置文件填写

启动类

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

application.yml

server:
  port: 8080

linc:
  properties:
    name: linc

访问 http://localhost:8080/my

image.png

注意我们配置了@ConditionalOnProperty(value = "linc.properties.name")

所以在application.yml配置了这个配置信息才能成功启动

上一篇 下一篇

猜你喜欢

热点阅读