设为首页 加入收藏

TOP

SpringBoot系列三:SpringBoot自定义Starter(一)
2018-11-21 22:08:37 】 浏览:584
Tags:SpringBoot 系列 定义 Starter

在前面两章 SpringBoot入门SpringBoot自动配置原理 的学习后,我们对如何创建一个 SpringBoot 项目、SpringBoot 的运行原理以及自动配置等都有了一定的了解。如果我们系统中也想要拥有自动配置的功能,可以自己编写一个starter (启动器),想想就觉得很酷,因为这意味着我们不仅有自己定义的自动配的功能,而且具有更通用的耦合度更低的配置。

还是以第一章开头的简单功能为例:浏览器发送 sayHello 请求,服务器接受请求并处理,响应 Hello 。

首先我们看下工程结构:

helloworld-spring-boot-starter-autoconfigure(以下简称autoconfigure):该模块用来实现 Helloworld 的自动配置功能,它的打包方式为 jar;

helloworld-spring-boot-starter(以下简称starter):该模块的打包方式是 jar,依赖 autoconfigure 模块,它不写任何代码,只做自动配置包的自动引入,如果要使用 helloworld 自动配置功能,只要在 pom 文件中引入 starter 模块即可:

<dependency>
    <groupId>com.seagetech.spring.boot</groupId>
    <artifactId>helloworld-spring-boot-starter</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

1 项目工程的创建

1.1 创建Empty Project

1) 新建一个空的项目,如下图,点击Next

2) 输入项目名称以及选择项目存放路径,点击Finish

1.2 创建starter模块

1) 在1.1节中新建的空项目基础上新建一个Module

2) 选择Maven,点击Next

3) 输入GroupId、ArtifactId 和 Version 信息,点击Finish

4) 由于这个模块只做自动配置包的引入,所有删除 src 下的包,最终项目结构如下:

pom.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>

    <groupId>com.seagetech.spring.boot</groupId>
    <artifactId>helloworld-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
</project>

1.3 创建 autoconfigure 模块

1) 按1.2节第1步,新建一个 Module,并选择 Spring Initializr,点击Next

2) 输入项目相关信息,如下图所示:

3) 选择依赖,我们这里只选择web场景的依赖,点击Next

4) 默认,不需要改动,点击Finish

4) 删除 SpringBoot 自动创建的主配置类、resources 下所有文件(夹)以及 test 文件夹,最终项目结构如下:

pom.xml(已删除 test 测试模块的自动配置包以及 build 下的 Maven 插件)

<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>

   <groupId>com.seagetech.spring.boot</groupId>
   <artifactId>helloworld-spring-boot-starter-autoconfigure</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>jar</packaging>

   <name>helloworld-spring-boot-starter-autoconfigure</name>
   <description>Demo project for Spring Boot</description>

   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.1.0.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>

   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
      <java.version>1.8</java.version>
   </properties>

   <de
首页 上一页 1 2 3 4 下一页 尾页 1/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇让SpringBoot启动更快一点 下一篇SpringBoot系列二:SpringBoot自..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目