SpringBoot自定义Starter

SpringBoot自定义Starter,第1张

SpringBoot自定义Starter 如何实现自定义Starter 先创建一个父工程

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

自定义Starter的命名规范
  • 官方命名空间

    • 前缀:spring-boot-starter-

    • 模式:spring-boot-starter-模块名

    • 举例:spring-boot-starter-web、spring-boot-starter-jdbc

  • 自定义命名空间

    后缀:-spring-boot-starter

    模式:模块-spring-boot-starter

    举例:mybatis-spring-boot-starter

开始创建自定义Starter

引入依赖

<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.0modelVersion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.6.4version>
    parent>
    <groupId>linc.fungroupId>
    <artifactId>linc-spring-boot-starterartifactId>
    <version>1.0-SNAPSHOTversion>

    <properties>
        <spring-boot.version>2.6.4spring-boot.version>
    properties>
    <dependencies>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-configuration-processorartifactId>
            <optional>trueoptional>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
            <version>${spring-boot.version}version>
        dependency>
    dependencies>
    <repositories>
        <repository>
            <id>springsource-reposid>
            <name>SpringSource Repositoryname>
            <url>http://repo.spring.io/release/url>
        repository>
        <repository>
            <id>activiti-repos2id>
            <name>Activiti Repository 2name>
            <url>https://app.camunda.com/nexus/content/groups/publicurl>
        repository>
        <repository>
            <id>central-reposid>
            <name>Central Repositoryname>
            <url>http://repo.maven.apache.org/maven2url>
        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就写完了

我们进行打包

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

引入依赖

<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.0modelVersion>

    <groupId>linc.fungroupId>
    <artifactId>linc-projectartifactId>
    <version>1.0-SNAPSHOTversion>

    <properties>
        <maven.compiler.source>8maven.compiler.source>
        <maven.compiler.target>8maven.compiler.target>
    properties>
    <dependencies>
        <dependency>
            <groupId>linc.fungroupId>
            <artifactId>linc-spring-boot-starterartifactId>
            <version>1.0-SNAPSHOTversion>
        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

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

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

代码下载地址

下载地址: https://linq-cool.oss-cn-shanghai.aliyuncs.com/20220426/eaedc35d680f467c8ecab516bc332ff7.zip

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/755905.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-30
下一篇 2022-04-30

发表评论

登录后才能评论

评论列表(0条)

保存