美文网首页
自定义springboot starter

自定义springboot starter

作者: engineer_tang | 来源:发表于2020-07-21 08:29 被阅读0次

    1. 自定义springboot starter

    (1) 在pom.xml进行如下配置

        <properties>
            <java.version>1.8</java.version>
            <spring-boot.version>2.1.7.RELEASE</spring-boot.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-autoconfigure</artifactId>
                <version>${spring-boot.version}</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-configuration-processor</artifactId>
                <version>${spring-boot.version}</version>
            </dependency>
        </dependencies>
    

    (2) 创建属性配置文件

    package com.music.starter.configuration;
    
    import org.springframework.boot.context.properties.ConfigurationProperties;
    
    @ConfigurationProperties(prefix = "music")
    public class MusicProperties {
    
        private Integer price;
    
        private String projectName;
    
        public Integer getPrice() {
            return price;
        }
    
        public void setPrice(Integer price) {
            this.price = price;
        }
    
        public String getProjectName() {
            return projectName;
        }
    
        public void setProjectName(String projectName) {
            this.projectName = projectName;
        }
    }
    
    

    (3) 创建业务类

    package com.music.starter.service;
    
    public class MusicService {
    
        private String projectName;
    
        private Integer price;
    
        public MusicService(String projectName, Integer price) {
            this.projectName = projectName;
            this.price = price;
        }
    
        public String getDetail() {
            return String.format("%s 项目的价格为: %s", projectName, price);
        }
    }
    
    

    (4) 创建配置类

    package com.music.starter.configuration;
    
    import com.music.starter.service.MusicService;
    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;
    
    @Configuration
    @ConditionalOnWebApplication
    @EnableConfigurationProperties(MusicProperties.class)
    public class MusicConfig {
    
        @Autowired
        private MusicProperties musicProperties;
    
        @Bean
        public MusicService musicService(){
            System.out.println("加载music");
            return new MusicService(musicProperties.getProjectName(), musicProperties.getPrice());
        }
    }
    

    (5) 在resources目录下创建文件夹META-INF及其文件spring.factories

    # Auto Configure
    org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
    com.music.starter.configuration.MusicConfig
    

    (6) 在music-spring-boot-starter工程中执行mvn clean install 一个自定义的starter就可以使用了。

    2. 使用自定义的starter

    (1) 在项目里引入starter

            <dependency>
                <groupId>com.music</groupId>
                <artifactId>music-spring-boot-starter</artifactId>
                <version>0.0.1</version>
            </dependency>
    

    (2) 在项目配置文件中进行参数配置

    music:
      project-name: 音乐项目
      price: 305
    

    (3) 在代码中调用starter里的业务

        @ApiOperation("测试")
        @GetMapping("/say")
        public String sayWhat(){
            return musicService.getDetail();
        }
    

    相关文章

      网友评论

          本文标题:自定义springboot starter

          本文链接:https://www.haomeiwen.com/subject/jcmbkktx.html