前言:今天介紹一SpringBoot的Starter,并手寫一個自己的Starter,在SpringBoot項目中,有各種的Starter提供給開發(fā)者使用,Starter則提供各種API,這樣使開發(fā)SpringBoot項目變得簡單。實際上Starter簡單來說就是Spring+SpringMVC開發(fā)的。話不多說開始擼代碼
成都創(chuàng)新互聯(lián)公司是一家專注于成都網(wǎng)站設(shè)計、網(wǎng)站制作與策劃設(shè)計,五蓮網(wǎng)站建設(shè)哪家好?成都創(chuàng)新互聯(lián)公司做網(wǎng)站,專注于網(wǎng)站建設(shè)十余年,網(wǎng)設(shè)計領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:五蓮等地區(qū)。五蓮做網(wǎng)站價格咨詢:18982081108
首先在idea中創(chuàng)建SpringBoot項目,并首先創(chuàng)建一個BeautyProperties類,代碼代碼如下:
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 = "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;
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)建一個類ActionServiceAutoConfiguration,這個類是重點,代碼如下:
- @Configuration注解表明這是一個配置類
- @EnableConfigurationProperties(BeautyProperties.class)表明開啟@ConfigurationProperties這個注解,使這個注解生效
- @ConditionalOnClass(ActionService.class)條件判斷注解,表明有這個類ActionService,條件才生效,即配置才生效。
- 通過@Autowired將BeautyProperties 類自動注入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文件中,加入如下配置,作為使用這個Starter時候,沒有設(shè)置相關(guān)值的時候作為默認值注入到配置類中
beauty.name=李依依默認
beauty.age=18
- 最后再resources中新建一個META-INF文件夾,然后在新建一個文件spring.factories,這個名字和文件夾的名字不能改,加入配置如下,這個表明指定自動配置的類的全路徑,自動配置的時候就找到這個全路徑,實例化這個對象到容器中
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.mystarter.ActionServiceAutoConfiguration
- 最后一步點擊install,出現(xiàn)Build Success說明這個Starter已經(jīng)安裝到本地maven倉庫中,可以被別人引用

## 2.測試Starter
新建一個SpringBoot工程,在application.properties的文件中加入如下配置:
beauty.name=李依依
beauty.age=24
在pom文件中引入依賴,如下:
<dependency>
<groupId>com.org.ldc</groupId>
<artifactId>mystarter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
然后測試,如下代碼
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í)行測試,出現(xiàn)如下,說明創(chuàng)建成功

>更多的教程請關(guān)注:非科班的科班,路過有空的大佬們點個贊,謝謝大家
分享標(biāo)題:手擼一個SpringBoot的Starter,簡單易上手
網(wǎng)站鏈接:http://www.chinadenli.net/article34/piihpe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、自適應(yīng)網(wǎng)站、標(biāo)簽優(yōu)化、電子商務(wù)、微信小程序、微信公眾號
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)