zoukankan      html  css  js  c++  java
  • 创建自己的Spring Boot Starter

    抽取通用模块作为项目的一个spring boot starter。可参照mybatis的写法。

    IDEA创建Empty Project并添加如下2个module,一个基本maven模块,另一个引入spring-boot-starter依赖。

    1) xxx-spring-boot-starter - 引入依赖并管理依赖版本

    demo-spring-boot-starter

        <dependencies>
            <dependency>
                <groupId>org.chris</groupId>
                <artifactId>demo-spring-boot-autoconfigure</artifactId>
                <version>0.0.1-SNAPSHOT</version>
            </dependency>
        </dependencies>

    2) xxx-spring-boot-autoconfigure - xxx的自动配置类

     demo-spring-boot-autoconfigure

        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
        </dependencies>
    属性类DemoProperties
    @ConfigurationProperties(prefix = "chris.demo")
    public class DemoProperties {
        private String name;
        private String content;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getContent() {
            return content;
        }
    
        public void setContent(String content) {
            this.content = content;
        }
    }

      功能类DemoService

    public class DemoService {
    
        private DemoProperties demoProperties;
    
        public DemoProperties getDemoProperties() {
            return demoProperties;
        }
    
        public void setDemoProperties(DemoProperties demoProperties) {
            this.demoProperties = demoProperties;
        }
    
        public String demoShow(){
            return this.demoProperties.getName() + " ----- " + this.demoProperties.getContent();
        }
    }

    自动配置类DemoAutoConfiguration

    @Configuration
    @ConditionalOnWebApplication
    @EnableConfigurationProperties(DemoProperties.class)
    public class DemoAutoConfiguration {
    
        @Autowired
        private DemoProperties demoProperties;
    
        @Bean
        public DemoService demoService(){
            DemoService demoService = new DemoService();
            demoService.setDemoProperties(demoProperties);
            return demoService;
        }
    }

    最后添加DemoAutoConfiguration到EnableAutoConfiguration中

    org.springframework.boot.autoconfigure.EnableAutoConfiguration=
    org.chris.springboot.DemoAutoConfiguration

    在新的spring boot项目中如果需要引用以上starter,只需要在依赖中添加如下,

            <dependency>
                <groupId>org.chris</groupId>
                <artifactId>demo-spring-boot-starter</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>

    测试类DemoController

    @RestController
    public class DemoController {
    
        @Autowired
        private DemoService demoService;
    
        @GetMapping("demo")
        public String demo(){
            return demoService.demoShow();
        }
    }

    附上代码

    demo-starter, demo

  • 相关阅读:
    20145330 《网络攻防》 后门原理与实践
    20145330 《网络对抗》PC平台逆向破解:注入shellcode 和 Return-to-libc 攻击实验
    移动攻击实践
    安卓权限参考
    20145329 《网络对抗技术》Web安全基础实践
    20145329 《网络对抗技术》Web基础
    20145329 《网络对抗技术》网络欺诈技术防范
    20145329 《网络对抗技术》信息搜集与漏洞扫描
    20145329 《网络对抗技术》MSF基础应用
    20145329 《网络对抗技术》辅助模块使用
  • 原文地址:https://www.cnblogs.com/hello-yz/p/11058315.html
Copyright © 2011-2022 走看看