一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務器之家 - 編程語言 - Java教程 - SpringBoot自定義starter實例代碼

SpringBoot自定義starter實例代碼

2021-07-20 16:09幻楚 Java教程

這篇文章主要給大家介紹了關于SpringBoot自定義starter的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者使用SpringBoot具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧

一、簡介

springboot 最強大的功能就是把我們常用的場景抽取成了一個個starter(場景啟動器),我們通過引入springboot 為我提供的這些場景啟動器,我們再進行少量的配置就能使用相應的功能。即使是這樣,springboot也不能囊括我們所有的使用場景,往往我們需要自定義starter,來簡化我們對springboot的使用。

下面話不多說了,來一起看看詳細的介紹吧

二、如何自定義starter

1.實例

如何編寫自動配置 ?

我們參照@webmvcautoconfiguration為例,我們看看需要準備哪些東西,下面是webmvcautoconfiguration的部分代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@configuration
@conditionalonwebapplication
@conditionalonclass({servlet.class, dispatcherservlet.class, webmvcconfigureradapter.class})
@conditionalonmissingbean({webmvcconfigurationsupport.class})
@autoconfigureorder(-2147483638)
@autoconfigureafter({dispatcherservletautoconfiguration.class, validationautoconfiguration.class})
public class webmvcautoconfiguration {
 
    @import({webmvcautoconfiguration.enablewebmvcconfiguration.class})
 @enableconfigurationproperties({webmvcproperties.class, resourceproperties.class})
 public static class webmvcautoconfigurationadapter extends webmvcconfigureradapter {
 
 @bean
 @conditionalonbean({view.class})
 @conditionalonmissingbean
 public beannameviewresolver beannameviewresolver() {
  beannameviewresolver resolver = new beannameviewresolver();
  resolver.setorder(2147483637);
  return resolver;
 }
 }
}

我們可以抽取到我們自定義starter時,同樣需要的一些配置。

?
1
2
3
4
5
6
7
8
9
10
11
@configuration //指定這個類是一個配置類
@conditionalonxxx //指定條件成立的情況下自動配置類生效
@autoconfigureorder //指定自動配置類的順序
@bean //向容器中添加組件
@configurationproperties //結合相關xxxproperties來綁定相關的配置
@enableconfigurationproperties //讓xxxproperties生效加入到容器中
 
自動配置類要能加載需要將自動配置類,配置在meta-inf/spring.factories中
org.springframework.boot.autoconfigure.enableautoconfiguration=\
org.springframework.boot.autoconfigure.admin.springapplicationadminjmxautoconfiguration,\
org.springframework.boot.autoconfigure.aop.aopautoconfiguration,\

模式

我們參照 spring-boot-starter 我們發現其中沒有代碼:

SpringBoot自定義starter實例代碼

我們在看它的pom中的依賴中有個 springboot-starter

?
1
2
3
4
<dependency>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-starter</artifactid>
</dependency>

我們再看看 spring-boot-starter 有個 spring-boot-autoconfigure

?
1
2
3
4
<dependency>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-autoconfigure</artifactid>
</dependency>

關于web的一些自動配置都寫在了這里 ,所以我們有以下總結:

啟動器starter只是用來做依賴管理
需要專門寫一個類似spring-boot-autoconfigure的配置模塊
用的時候只需要引入啟動器starter,就可以使用自動配置了

命名規范

官方命名空間

  • 前綴:spring-boot-starter-
  • 模式:spring-boot-starter-模塊名
  • 舉例:spring-boot-starter-web、spring-boot-starter-jdbc

自定義命名空間

  • 后綴:-spring-boot-starter
  • 模式:模塊-spring-boot-starter
  • 舉例:mybatis-spring-boot-starter

三、自定義starter實例

我們需要先創建兩個工程 hello-spring-boot-starter 和 hello-spring-boot-starter-autoconfigurer

1. hello-spring-boot-starter

1.pom.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?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.gf</groupid>
    <artifactid>hello-spring-boot-starter</artifactid>
    <version>0.0.1-snapshot</version>
    <packaging>jar</packaging>
 
    <name>hello-spring-boot-starter</name>
 
    <!-- 啟動器 -->
    <dependencies>
        <!-- 引入自動配置模塊 -->
        <dependency>
            <groupid>com.gf</groupid>
            <artifactid>hello-spring-boot-starter-autoconfigurer</artifactid>
            <version>0.0.1-snapshot</version>
        </dependency>
    </dependencies>
</project>

同時刪除 啟動類、resources下的文件,test文件。

2. hello-spring-boot-starter-autoconfigurer

1. pom.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?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.gf</groupid>
    <artifactid>hello-spring-boot-starter-autoconfigurer</artifactid>
    <version>0.0.1-snapshot</version>
    <packaging>jar</packaging>
 
    <name>hello-spring-boot-starter-autoconfigurer</name>
    <description>demo project for spring boot</description>
 
    <parent>
        <groupid>org.springframework.boot</groupid>
        <artifactid>spring-boot-starter-parent</artifactid>
        <version>1.5.9.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>
 
    <dependencies>
        <!-- 引入spring-boot-starter,所有starter的基本配合 -->
        <dependency>
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-starter</artifactid>
        </dependency>
 
    </dependencies>
</project>

2. helloproperties

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.gf.service;
 
import org.springframework.boot.context.properties.configurationproperties;
 
@configurationproperties(prefix = "gf.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;
 }
}

3. helloservice

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.gf.service;
 
 
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();
 }
}

4. helloserviceautoconfiguration

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.gf.service;
 
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)
public class helloserviceautoconfiguration {
 
 @autowired
 helloproperties helloproperties;
 
 @bean
 public helloservice helloservice() {
  helloservice service = new helloservice();
  service.sethelloproperties( helloproperties );
  return service;
 }
}

5. spring.factories

在 resources 下創建文件夾 meta-inf 并在 meta-inf 下創建文件 spring.factories ,內容如下:

?
1
2
3
# auto configure
org.springframework.boot.autoconfigure.enableautoconfiguration=\
com.gf.service.helloserviceautoconfiguration

到這兒,我們的配置自定義的starter就寫完了 ,我們把 hello-spring-boot-starter-autoconfigurer、hello-spring-boot-starter 安裝成本地jar包。

三、測試自定義starter

我們創建個項目 hello-spring-boot-starter-test,來測試系我們寫的stater。

1. pom.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?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.gf</groupid>
    <artifactid>hello-spring-boot-starter-test</artifactid>
    <version>0.0.1-snapshot</version>
    <packaging>jar</packaging>
 
    <name>hello-spring-boot-starter-test</name>
    <description>demo project for spring boot</description>
 
    <parent>
        <groupid>org.springframework.boot</groupid>
        <artifactid>spring-boot-starter-parent</artifactid>
        <version>1.5.9.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>
 
    <dependencies>
        <dependency>
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-starter-web</artifactid>
        </dependency>
 
        <!-- 引入自定義starter -->
        <dependency>
            <groupid>com.gf</groupid>
            <artifactid>hello-spring-boot-starter</artifactid>
            <version>0.0.1-snapshot</version>
        </dependency>
 
        <dependency>
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-starter-test</artifactid>
            <scope>test</scope>
        </dependency>
    </dependencies>
 
    <build>
        <plugins>
            <plugin>
                <groupid>org.springframework.boot</groupid>
                <artifactid>spring-boot-maven-plugin</artifactid>
            </plugin>
        </plugins>
    </build>
</project>

2. hellocontroller

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.gf.controller;
 
import com.gf.service.helloservice;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.web.bind.annotation.getmapping;
import org.springframework.web.bind.annotation.pathvariable;
import org.springframework.web.bind.annotation.restcontroller;
 
@restcontroller
public class hellocontroller {
 
 @autowired
 helloservice helloservice;
 
 @getmapping("/hello/{name}")
 public string hello(@pathvariable(value = "name") string name) {
  return helloservice.sayhello( name + " , " );
 }
}

3. application.properties

?
1
2
gf.hello.prefix = hi
gf.hello.suffix = what's up man ?

我運行項目訪問 http://127.0.0.1:8080/hello/zhangsan,結果如下:

hi-zhangsan , what's up man ?

源碼下載: https://github.com/gf-huanchupk/springbootlearning

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對服務器之家的支持。

原文鏈接:https://juejin.im/post/5c81dda6e51d4539ab383e3a

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 涩涩成人 | 欧美人xxxxxbbbb| 99久久中文字幕伊人 | 亚洲欧美成人综合久久久 | 紧身裙女教师miad711在线 | 欧美亚洲国产一区二区三区 | 国产精品嫩草影院一二三区 | 精品国产乱码久久久人妻 | 波多野结衣教师未删减版 | 国产成人福利免费观看 | 999任你躁在线精品免费不卡 | 精品午夜久久福利大片免费 | 国产精品99精品久久免费 | 日韩国产欧美成人一区二区影院 | 欧美一区二区三区视视频 | h动态图男女啪啪27报 | 从后面撕开老师的丝袜动态图 | 色多多视频网站 | 我与恶魔的h生活ova | 学校捏奶揉下面污文h | 国产欧美精品一区二区三区–老狼 | 牛牛在线观看 | 亚洲精品在线免费 | 91精品大神国产在线播放 | 洗濯屋H纯肉动漫在线观看 武侠艳妇屈辱的张开双腿 午夜在线观看免费观看 视频 | 亚洲国产精品久久精品怡红院 | 女教师的一级毛片 | 免费观看欧美成人h | 日本视频高清免费观看xxx | 久久日本片精品AAAAA国产 | 欧美va免费精品高清在线 | 按摩院已婚妇女中文字幕 | 91精品91久久久久久 | 亚洲国产精品综合一区在线 | 91精品综合久久久久久五月天 | 精彩国产萝视频在线 | 亚洲精品国产乱码AV在线观看 | 久久夜色噜噜噜亚洲AV0000 | 国产精选之刘婷野战 | 亚洲欧美一区二区久久 | 欧美╳bbbb |