最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

淺談SpringBoot如何自定義Starters

 更新時間:2021年05月24日 10:22:14   作者:明了LM  
今天帶大家來學習SpringBoot如何自定義Starters,文中有非常詳細的圖文介紹及代碼示例,對正在學習java的小伙伴們很有幫助,需要的朋友可以參考下

一、Starters原理

1.1 Starters場景啟動器

1、場景需要用到的依賴是什么?

比如依賴的jar

2、如何編寫自動配置?

以WebMvcAutoConfiguration自動配置為例:

@Configuration
@ConditionalOnWebApplication
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class,
		WebMvcConfigurerAdapter.class })
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
@AutoConfigureAfter({ DispatcherServletAutoConfiguration.class,
		ValidationAutoConfiguration.class })
public class WebMvcAutoConfiguration {

	public static final String DEFAULT_PREFIX = "";

	public static final String DEFAULT_SUFFIX = "";

@Configuration指定這是一個配置類
@ConditionalOnXXX 在指定條件成立的情況下自動配置類生效

自動裝配順序
在特定自動裝配Class之前 @AutoConfigureBefore
在特定自動裝配Class之后@AutoConfigureAfter
指定順序@AutoConfigureOrder

@Bean 給容器中添加組件
@ConfigurationPropertie結合相關xxxProperties類來綁定相關的配置

@ConfigurationProperties(prefix = "spring.mvc")
public class WebMvcProperties {
}

@EnableConfigurationProperties 讓xxxProperties生效加入到容器中

@Configuration
@Import(EnableWebMvcConfiguration.class)
@EnableConfigurationProperties({ WebMvcProperties.class, ResourceProperties.class })
public static class WebMvcAutoConfigurationAdapter extends WebMvcConfigurerAdapter {
}

配置自動裝配Bean:
自動配置類要能加載
將需要啟動就加載的自動配置類,將標注@Configuration的自動配置類配置在META‐INF/spring.factories下,自動配置類就會生效

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\

3、模式

啟動器(starter)

qid

啟動器只用來做依賴導入
專門寫一個自動配置模塊
啟動器依賴自動配置,別人只需要引入啟動器(starters)

mybatis-spring-boot-starter 自定義啟動器名 -spring-boot-starter

二、自定義Starters

構建項目:
1.先創(chuàng)建一個空工程

112
s

2、創(chuàng)建兩個模塊分別是啟動器starter的maven模塊spring的初始化器創(chuàng)建的自動配置模塊

啟動器maven模塊

1

自定義的starters

在這里插入圖片描述

在這里插入圖片描述

spring的初始化器創(chuàng)建模塊(創(chuàng)建自動配置相關的模塊)

23

三、代碼步驟

在啟動器starter的pom文件中引入配置類的坐標ming-spring-boot-starter-autoconfigurer

<?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.ming.springboot</groupId>
    <artifactId>ming-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>com.ming.springboot</groupId>
            <artifactId>ming-spring-boot-starter-autoconfigurer</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

寫一個打招呼的功能

package com.ming.springboot;

/**
 * 打招呼的
 *
 */
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();
    }
}

HelloProperties 和Helloservice 進行屬性綁定的

package com.ming.springboot;

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

@ConfigurationProperties(prefix = "com.ming")
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.ming.springboot;

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  helloService = new HelloService();
        helloService.setHelloProperties(helloProperties);
        return  helloService;
    }

}

然后將這兩個模塊安裝到maven倉庫中
先安裝配置模塊因為starter模塊依賴配置模塊,別人調(diào)用我們的starter模塊就行了

2

然后將啟動器starter也裝到倉庫中,別人就可以用坐標引入了

在別的項目中引入自定義的啟動器starter

   <!--引入自定義的starter-->
        <dependency>
            <groupId>com.ming.springboot</groupId>
            <artifactId>ming-spring-boot-starter</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

配置application.properties

#自定義啟動器starter
com.ming.prefix=一起學習
com.ming.suffix=你學費了嗎

測試

  @Autowired
    HelloService helloService;

    @Test
    public void starterTest(){
        String sayHello = helloService.sayHello("自定義starter");
        System.out.println(sayHello);
    }

到此這篇關于淺談SpringBoot如何自定義Starters的文章就介紹到這了,更多相關Spring Boot自定義Starters內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Kotlin中的抽象類實現(xiàn)

    Kotlin中的抽象類實現(xiàn)

    這篇文章主要介紹了Kotlin中的抽象類實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-11-11
  • Spring?web開發(fā)教程之Request獲取3種方式

    Spring?web開發(fā)教程之Request獲取3種方式

    這篇文章主要給大家介紹了關于Spring?web開發(fā)教程之Request獲取3種方式的相關資料,request對象是從客戶端向服務器發(fā)出請求,包括用戶提交的信息以及客戶端的一些信息,需要的朋友可以參考下
    2023-11-11
  • Java獲取文件的hash值(SHA256)兩種方式

    Java獲取文件的hash值(SHA256)兩種方式

    這篇文章主要給大家介紹了關于Java獲取文件hash值(SHA256)的兩種方式,SHA256是一種哈希算法,它是不可逆的,也就是說無法解密,需要的朋友可以參考下
    2023-09-09
  • Mybatis實現(xiàn)增刪改查(CRUD)實例代碼

    Mybatis實現(xiàn)增刪改查(CRUD)實例代碼

    MyBatis 是支持普通 SQL 查詢,存儲過程和高級映射的優(yōu)秀持久層框架。通過本文給大家介紹Mybatis實現(xiàn)增刪改查(CRUD)實例代碼 ,需要的朋友參考下
    2016-05-05
  • SpringBoot之使用Feign實現(xiàn)微服務間的交互

    SpringBoot之使用Feign實現(xiàn)微服務間的交互

    這篇文章主要介紹了SpringBoot中使用Feign實現(xiàn)微服務間的交互,對微服務這方面感興趣的小伙伴可以參考閱讀本文
    2023-03-03
  • OpenFeign在傳遞參數(shù)為對象類型是為空的問題

    OpenFeign在傳遞參數(shù)為對象類型是為空的問題

    這篇文章主要介紹了OpenFeign在傳遞參數(shù)為對象類型是為空的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Java通過反射查看類的信息示例

    Java通過反射查看類的信息示例

    這篇文章主要介紹了Java通過反射查看類的信息,結合實例形式詳細分析了java基于反射獲取類信息的相關原理與實現(xiàn)技巧,需要的朋友可以參考下
    2019-07-07
  • SpringBoot集成Caffeine緩存的實現(xiàn)步驟

    SpringBoot集成Caffeine緩存的實現(xiàn)步驟

    Caffeine cache是一個針對Java的高性能緩存庫。在本文中,我們將介紹它與Spring Boot如何一起使用。
    2021-05-05
  • Java中使用內(nèi)存映射實現(xiàn)大文件上傳實例

    Java中使用內(nèi)存映射實現(xiàn)大文件上傳實例

    這篇文章主要介紹了Java中使用內(nèi)存映射實現(xiàn)大文件上傳實例,本文對比測試了FileInputStream 或者FileOutputStream 抑或RandomAccessFile的頻繁讀寫操作,最后總結出映射到內(nèi)存后進行讀寫以提高速度,需要的朋友可以參考下
    2015-01-01
  • Java——對象初始化順序使用詳解

    Java——對象初始化順序使用詳解

    本篇文章介紹了,Java對象初始化順序的使用。需要的朋友參考下
    2017-04-04

最新評論

从江县| 博客| 肥西县| 即墨市| 宁乡县| 夏河县| 绵竹市| 油尖旺区| 新河县| 衡阳市| 六安市| 修武县| 根河市| 当雄县| 鹤山市| 临汾市| 曲阜市| 闸北区| 洞口县| 手游| 威宁| 临朐县| 久治县| 双鸭山市| 马龙县| 太仆寺旗| 泗洪县| 邓州市| 平武县| 汝城县| 汉源县| 紫金县| 台东市| 陇西县| 中卫市| 建水县| 炎陵县| 开化县| 承德县| 琼海市| 咸宁市|