1,创建一个空工程  new -  project - emptyproject

2,new一个Modules  ---------------- maven (启动器) :

springboottest-spring-boot-starter

3,new一个Modules  ---------------- spring(做自动配置的):

springboottest-spring-boot-starter-autoconfigurer   spring版本:1.5.10

4,启动器pom文件中引入自动配置模块:

    <!--启动器-->
<dependencies>
<!--引入自动配置模块-->
<dependency>
<groupId>com.springboottest.starter</groupId>
<artifactId>springboottest-spring-boot-starter-autoconfigurer</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>

5,自动配置器中,删除主主程序等不需要的内容,以及项目的启动类。并编写启动器:

  1. pom文件中引入启动器(所有starter的基本配置):

    <dependencies>
    <!--引入spring-boot-starter-->
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    </dependency> </dependencies>
  2. 编写会被调用的service
  3. package com.springboottest.starter;
    
    import org.springframework.boot.context.properties.ConfigurationProperties;
    
    //绑定文件中所有以springboottest.hello 开始的配置
    @ConfigurationProperties(prefix = "springboottest.hello")
    public class HelloProperties {
    private String prefix;
    private String suffix; public String getPrefix() {
    return prefix;
    } public void setPrefix(String prefix) {
    this.prefix = prefix;
    } public String getSuffix() {
    return suffix;
    } public void setSuffix(String suffix) {
    this.suffix = suffix;
    }
    }
    package com.springboottest.starter;
    
    public class HelloService {
    HelloProperties helloProperties; public HelloProperties getHelloProperties() {
    return helloProperties;
    } public void setHelloProperties(HelloProperties helloProperties) {
    this.helloProperties = helloProperties;
    } public String sayHello(String name){
    return helloProperties.getPrefix()+"-"+ name + helloProperties.getSuffix();
    };
    }
    package com.springboottest.starter;
    
    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//web应用才生效
    @EnableConfigurationProperties(HelloProperties.class)//让属性生效 HelloProperties helloProperties;
    public class HelloServiceAutoConfiguration { //让属性生效 HelloProperties helloProperties;
    @Autowired
    HelloProperties helloProperties;
    @Bean
    public HelloService helloService(){
    HelloService service = new HelloService();
    service.setHelloProperties(helloProperties);
    return service;
    } }
  4. 配置spring.factories让自动配置类生效
    org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
    com.springboottest.starter.HelloServiceAutoConfiguration
  5. springboottest-spring-boot-starter  启动器编写完成
  6. 两个项目分别install
  7. 加载完成,现在可以在别的项目里面调用

6,新建项目,调用自定义启动器的方法

  1. pom文件引入自定义启动器

    <!--引入自定义starter-->
    <dependency>
    <groupId>com.springboottest.starter</groupId>
    <artifactId>springboottest-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
    </dependency>
  2. 按照规则编写properties,增加前后缀
    springboottest.hello.prefix=SPRINGBOOT
    springboottest.hello.suffix=HELLO WORLD
  3. 调用方法                     

springboot自定义starter的更多相关文章

  1. SpringBoot --- 自定义 Starter

    SpringBoot --- 自定义 Starter 创建 1.需要创建一个新的空工程 2.新的工程需要引入两个模块 一个Maven 模块 作为启动器 一个SpringBoot 模块 作为自动配置模块 ...

  2. SpringBoot自定义starter及自动配置

    SpringBoot的核心就是自动配置,而支持自动配置的是一个个starter项目.除了官方已有的starter,用户自己也可以根据规则自定义自己的starter项目. 自定义starter条件 自动 ...

  3. SpringBoot自定义Starter实现

    自定义Starter: Starter会把所有用到的依赖都给包含进来,避免了开发者自己去引入依赖所带来的麻烦.Starter 提供了一种开箱即用的理念,其中核心就是springboot的自动配置原理相 ...

  4. springboot 自定义starter之AutoConfiguration【原】

    八.自定义starter AutoConfiguration: 1.这个场景需要使用到的依赖是什么? 没有特别依赖的配置 2.如何编写自动配置 @Configuration //指定这个类是一个配置类 ...

  5. SpringBoot自定义starter开发分布式任务调度实践

    概述 需求 在前面的博客<Java定时器演进过程和生产级分布式任务调度ElasticJob代码实战>中,我们已经熟悉ElasticJob分布式任务的应用,其核心实现为elasticjob- ...

  6. SpringBoot系列三:SpringBoot自定义Starter

    在前面两章 SpringBoot入门 .SpringBoot自动配置原理 的学习后,我们对如何创建一个 SpringBoot 项目.SpringBoot 的运行原理以及自动配置等都有了一定的了解.如果 ...

  7. Springboot自定义starter打印sql及其执行时间

    前面写到了通过实现mybatis提供的org.apache.ibatis.plugin.Interceptor接口实现了打印SQL执行时间,并格式化SQL及其参数,如果我们使用的是ssm还得再配置文件 ...

  8. Spring-Boot自定义Starter实践

    此文已由作者王慎为授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. disconf-spring-boot-starter 使用方法: 引入maven依赖: <depen ...

  9. SpringBoot之旅第六篇-启动原理及自定义starter

    一.引言 SpringBoot的一大优势就是Starter,由于SpringBoot有很多开箱即用的Starter依赖,使得我们开发变得简单,我们不需要过多的关注框架的配置. 在日常开发中,我们也会自 ...

随机推荐

  1. Python基础之数组和向量化计算总结

    一.多维数组 1.生成ndarray     (array函数) .np.array()生成多维数组 例如:import numpy as npdata1=[6,7.5,8,0,1]     #创建简 ...

  2. DeleteFile 删除文件

    #include <Windows.h> #include <tchar.h> int WINAPI _tWinMain(HINSTANCE hInstance, HINSTA ...

  3. docker容器的实践——综合项目一

                    Docker 综合实验   实验拓扑:   [调度器] Keepalived + nginx 一.Keepalived服务的安装配置: 关闭LVS服务器的ipv4代理和 ...

  4. c++ 单元测试框架 gmock 深度剖析

    c++ 单元测试框架 gmock 深度剖析 随着微服务和CI的流行,在目前的软件工程领域中单元测试可以说是必不可少的一个环节,在TDD中,单元测试更是被提高到了一个新的高度.但是很多公司由于很多不同的 ...

  5. conn.encoders[SafeBytes] = conn.encoders[bytes] KeyError: <class 'bytes'>

    问题描述:Django连接mysql数据库,修改了setting.py文件后,启动服务器报错 错误截图如下: 解决方法: 1.pip install pymsql 2.在setting.py同目录下的 ...

  6. t-SNE完整笔记

    http://www.datakit.cn/blog/2017/02/05/t_sne_full.html t-SNE(t-distributed stochastic neighbor embedd ...

  7. jmeter中文乱码及Unicode转中文

    在测试的过程中传入中文测试,乱码问题 1.get请求传入中文参数address:中国云南省 request请求中已经正确显示参数(可能跟我之前配置过有关系),如果没有正确显示,在http取样器后面新建 ...

  8. Navicat Premium 简体中文版 12.0.16 以上版本国外官网下载地址(非国内)

    国内Navicat网址是:http://www.navicat.com.cn 国外Navicat网址是:http://www.navicat.com 国外的更新比国内的快,而且同一个版本,国内和国外下 ...

  9. ROS机器人编程实践----琐碎知识点

    amcl原理: amcl将激光传感器数据与从地图预估的传感器数据相比较,给出可能的位姿.如果传感器数据和某个候选位姿处的预测数据相同,amcl就会给这个位姿一个较高的概率,反之,就会降低这个概率.概率 ...

  10. 转载-《Python学习手册》读书笔记

    转载-<Python学习手册>读书笔记 http://www.cnblogs.com/wuyuegb2312/archive/2013/02/26/2910908.html