SpringBoot --- 自定义 Starter

创建

1、需要创建一个新的空工程

2、新的工程需要引入两个模块

一个Maven 模块 作为启动器

一个SpringBoot 模块 作为自动配置模块

3、在Starter 模块(即启动器模块)的 pom.xml 引入 自动配置模块

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

4、接下来主要的工作是编写自动配置包 ling-spring-boot-starter-autoconfigurer

首先,我们要明确,需要使用者配置的属性有哪些,需要编写一个类并用 @ConfigurationProperties 标注,用 prefix 明确配置的字首部分,约定后,使用者配置。

package com.ling.starter;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "ling.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;
}
}

5、接下来是编写Service 类,明确的是,以上的配置,用于哪些处理。

package com.ling.starter;

public class HelloService {

    HelloProperties helloProperties;

    public HelloProperties getHelloProperties() {
return helloProperties;
} public void setHelloProperties(HelloProperties helloProperties) {
this.helloProperties = helloProperties;
} public String syaHello(String name){ // 做业务处理
return helloProperties.getPrefix()+ "---" + name + "---" + helloProperties.getSuffix();
}
}

编写配置类(类似 xml ),注入Bean。

@Configuration     //表明这是一个配置类
@ConditionalOnWebApplication //判断是否是web 工程,是则配置
@EnableConfigurationProperties(HelloProperties.class) //需要注入哪些类到容器中
public class HelloAutoconfiguration { @Autowired
HelloProperties helloProperties; @Bean
public HelloService helloService(){
HelloService helloService = new HelloService();
helloService.setHelloProperties(helloProperties);
return helloService;
}
}

到这里自定义 starter 已经完成。

测试

1、创建一个web 工程,测试

2、导入自定义的 starter 依赖

<!--        测试 自定义 starter-->
<dependency>
<groupId>com.ling.starter</groupId>
<artifactId>ling-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>

3、导入成功后,可以在依赖库中找到自定义 starter 的包,因为 ling-spring-boot-starter 的 pom 文件已经导入了 ling-spring-boot-starter-autoconfigurer 的依赖。所以会自动导入ling-spring-boot-starter 所依赖的包。

4、创建并配置 properties.yml 文件(配置是没有相关提示属于正常,按照约定好的字首和属性名配置即可)

ling:
hello:
prefix: 你好呀
suffix: 欢迎你。。。

5、编写Contriller 类

@RestController  //非JSON 数据,可用 @RestController
/* @RestController== @ResponseBody +@Controller*/
public class HWcontroller { @Autowired
HelloService helloService; @RequestMapping("/hello")
public String hello(){
return helloService.syaHello("son");
}
}

6、启动工程,访问

http://localhost:8080/hello

SpringBoot --- 自定义 Starter的更多相关文章

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

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

  2. SpringBoot自定义Starter实现

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

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

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

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

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

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

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

  6. springboot自定义starter

    1,创建一个空工程 2,new一个Modules  ---------------- maven (启动器) : springboottest-spring-boot-starter 3,new一个M ...

  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. Java Object类中的equals方法

    Object类中的equals方法用于检测一个对象是否等于另外一个对象.在Object类中,这个方法将判断两个对象是否具有相同的引用.如果两个对象具有相同的引用,它们一定是相等的.从这点上看,将其作为 ...

  2. 循序渐进nginx(三):日志管理、http限流、https配置,http_rewrite模块,第三方模块安装,结语

    目录 日志管理 access_log error_log 日志文件切割 自定义错误页 http访问限流 限制请求数 语法 使用 限制连接数 语法 测试 补充: https配置 使用 生成证书 配置ng ...

  3. stm32-HAL库串口收发

    串口发送 重写fputc函数 /* 优点 直接使用printf函数,发送数据长度无限制,不需要额外的数组空间 缺点 只能对应一个串口,暂时没想到解决方案 */ //头文件中要包含 stdio.h 然后 ...

  4. justoj connect(边的处理)

    CONNECT https://oj.ismdeep.com/contest/problem?id=1702&pid=2 Problem Description 有nn个顶点,每个顶点有自己的 ...

  5. Java基础加强笔记——测试、反射、注解

    目录 1. Junit单元测试 2. 反射 3. 注解 Junit单元测试: 测试分类: 1. 黑盒测试:不需要写代码,给输入值,看程序是否能够输出期望的值. 2. 白盒测试:需要写代码的.关注程序具 ...

  6. jquery判断radio是否选中

    微交易-实体系统 微交易-虚拟系统   <div class="system"> <div class="systemt"> <l ...

  7. MacOS下Nginx安装

    1. 先安装homebrew 2. 安装Nginx,终端下执行: $ brew install nginx 安装过程中会自己安装依赖: 3. 启动nginx服务 $ nginx 成功后,使用浏览器打开 ...

  8. PHP fstat() 函数

    定义和用法 fstat() 函数返回关于一个打开的文件的信息. 该函数将返回一个包含下列元素的数组: [0] 或 [dev] - 设备编号 [1] 或 [ino] - inode 编号 [2] 或 [ ...

  9. PHP unserialize() 函数

    unserialize() 函数用于将通过 serialize() 函数序列化后的对象或数组进行反序列化,并返回原始的对象结构. PHP 版本要求: PHP 4, PHP 5, PHP 7高佣联盟 w ...

  10. HTTP Request Method(十五种)

    序号 方法 描述 1 GET 请求指定的页面信息,并返回实体主体. 2 HEAD 类似于get请求,只不过返回的响应中没有具体的内容,用于获取报头 3 POST 向指定资源提交数据进行处理请求(例如提 ...