手?jǐn)]一個(gè)SpringBoot的Starter,簡(jiǎn)單易上手-創(chuàng)新互聯(lián)

前言:今天介紹一SpringBoot的Starter,并手寫(xiě)一個(gè)自己的Starter,在SpringBoot項(xiàng)目中,有各種的Starter提供給開(kāi)發(fā)者使用,Starter則提供各種API,這樣使開(kāi)發(fā)SpringBoot項(xiàng)目變得簡(jiǎn)單。實(shí)際上Starter簡(jiǎn)單來(lái)說(shuō)就是Spring+SpringMVC開(kāi)發(fā)的。話(huà)不多說(shuō)開(kāi)始擼代碼

創(chuàng)新互聯(lián)建站成立以來(lái)不斷整合自身及行業(yè)資源、不斷突破觀念以使企業(yè)策略得到完善和成熟,建立了一套“以技術(shù)為基點(diǎn),以客戶(hù)需求中心、市場(chǎng)為導(dǎo)向”的快速反應(yīng)體系。對(duì)公司的主營(yíng)項(xiàng)目,如中高端企業(yè)網(wǎng)站企劃 / 設(shè)計(jì)、行業(yè) / 企業(yè)門(mén)戶(hù)設(shè)計(jì)推廣、行業(yè)門(mén)戶(hù)平臺(tái)運(yùn)營(yíng)、手機(jī)APP定制開(kāi)發(fā)、移動(dòng)網(wǎng)站建設(shè)、微信網(wǎng)站制作、軟件開(kāi)發(fā)、成都服務(wù)器托管等實(shí)行標(biāo)準(zhǔn)化操作,讓客戶(hù)可以直觀的預(yù)知到從創(chuàng)新互聯(lián)建站可以獲得的服務(wù)效果。

1.創(chuàng)建項(xiàng)目

首先在idea中創(chuàng)建SpringBoot項(xiàng)目,并首先創(chuàng)建一個(gè)BeautyProperties類(lèi),代碼代碼如下:

package com.mystarter;

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

@ConfigurationProperties(prefix = "beauty")
public class BeautyProperties {
    private String name;
    private Integer age;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
}
  • @ConfigurationProperties(prefix = "beauty")注解表示,在resource目錄下的application.properties文件中定義的變量的以beauty前綴的變量值映射到這個(gè)類(lèi)中,給這個(gè)對(duì)象賦值
  • 其中這個(gè)XXProperties類(lèi),若是由閱讀過(guò)SpringBoot源碼的程序員都知道,在SpringBooot的源碼中Starter有各種的XXProperties類(lèi)與.properties文件相對(duì)應(yīng)
  • 如圖所示所有的自動(dòng)配置相關(guān)的都在spring-boot-autoconfigure這個(gè)jar包下。其中就列舉了兩個(gè)RabbitProperties、BatchProperties等的配置類(lèi)
    手?jǐn)]一個(gè)SpringBoot的Starter,簡(jiǎn)單易上手
  • 點(diǎn)進(jìn)RabbitProperties這個(gè)類(lèi)中去看,代碼如下,只粘貼一部分,這個(gè)類(lèi)中也是使用@ConfigurationProperties注解將配置文件中的值與此類(lèi)中的屬性值相映射,目的就是為了給這些屬性賦值
  • 這里留一個(gè)問(wèn)題,大佬們就自己去尋找答案吧?就是那些與這些類(lèi)相映射的文件在哪里呢?自行百度哈
    @ConfigurationProperties(
    prefix = "spring.rabbitmq"
    )
    public class RabbitProperties {
    private String host = "localhost";
    private int port = 5672;
    private String username = "guest";
    private String password = "guest";
    private final RabbitProperties.Ssl ssl = new RabbitProperties.Ssl();
    private String virtualHost;
    private String addresses;
  • 然后再創(chuàng)建一個(gè)ActionService類(lèi),這個(gè)類(lèi)沒(méi)什么好說(shuō)的了,代碼如下:
    
    package com.mystarter;

public class ActionService {

private String name;

private Integer age;

public String sayHello() {
    return "my name is "+ name +",I am "+ age +" years old";
}

public Integer getAge() {
    return age;
}

public void setAge(Integer age) {
    this.age = age;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

}


 - 最后再創(chuàng)建一個(gè)類(lèi)ActionServiceAutoConfiguration,這個(gè)類(lèi)是重點(diǎn),代碼如下:
 - @Configuration注解表明這是一個(gè)配置類(lèi)
 - @EnableConfigurationProperties(BeautyProperties.class)表明開(kāi)啟@ConfigurationProperties這個(gè)注解,使這個(gè)注解生效
 - @ConditionalOnClass(ActionService.class)條件判斷注解,表明有這個(gè)類(lèi)ActionService,條件才生效,即配置才生效。
 - 通過(guò)@Autowired將BeautyProperties 類(lèi)自動(dòng)注入IOC容器中
 - @Bean將返回的值注入到容器中

package com.mystarter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration@EnableConfigurationProperties(BeautyProperties.class)
br/>@EnableConfigurationProperties(BeautyProperties.class)
public class ActionServiceAutoConfiguration {

@Autowired
BeautyProperties beautyProperties;

@Bean
ActionService helloService() {
    ActionService helloService = new ActionService();
    helloService.setName(beautyProperties.getName());
    helloService.setAge(beautyProperties.getAge());
    return helloService;
}

}


 - 然后再resources文件夾下的application.properties文件中,加入如下配置,作為使用這個(gè)Starter時(shí)候,沒(méi)有設(shè)置相關(guān)值的時(shí)候作為默認(rèn)值注入到配置類(lèi)中

beauty.name=李依依默認(rèn)
beauty.age=18


 - 最后再resources中新建一個(gè)META-INF文件夾,然后在新建一個(gè)文件spring.factories,這個(gè)名字和文件夾的名字不能改,加入配置如下,這個(gè)表明指定自動(dòng)配置的類(lèi)的全路徑,自動(dòng)配置的時(shí)候就找到這個(gè)全路徑,實(shí)例化這個(gè)對(duì)象到容器中

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.mystarter.ActionServiceAutoConfiguration


 - 最后一步點(diǎn)擊install,出現(xiàn)Build Success說(shuō)明這個(gè)Starter已經(jīng)安裝到本地maven倉(cāng)庫(kù)中,可以被別人引用

![在這里插入圖片描述](https://img-blog.csdnimg.cn/20191218230620617.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzMjU1MDE3,size_16,color_FFFFFF,t_70)
## 2.測(cè)試Starter
新建一個(gè)SpringBoot工程,在application.properties的文件中加入如下配置:

beauty.name=李依依
beauty.age=24

在pom文件中引入依賴(lài),如下:

<dependency>
<groupId>com.org.ldc</groupId>
<artifactId>mystarter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>


然后測(cè)試,如下代碼

package com.org.ldc.mystarter;

import com.mystarter.HelloService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest
class TestmystarterApplicationTests {

@Autowired
HelloService helloService;

@Test
public void contextLoads() {
    System.out.println(helloService.sayHello());
}

}


執(zhí)行測(cè)試,出現(xiàn)如下,說(shuō)明創(chuàng)建成功
![在這里插入圖片描述](https://img-blog.csdnimg.cn/2019121823094633.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzMjU1MDE3,size_16,color_FFFFFF,t_70)
>更多的教程請(qǐng)關(guān)注:非科班的科班,路過(guò)有空的大佬們點(diǎn)個(gè)贊,謝謝大家

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線(xiàn),公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性?xún)r(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿(mǎn)足用戶(hù)豐富、多元化的應(yīng)用場(chǎng)景需求。

本文名稱(chēng):手?jǐn)]一個(gè)SpringBoot的Starter,簡(jiǎn)單易上手-創(chuàng)新互聯(lián)
本文鏈接:http://bm7419.com/article40/ggsho.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、建站公司搜索引擎優(yōu)化、網(wǎng)站制作、App設(shè)計(jì)、外貿(mào)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名