您好,登錄后才能下訂單哦!
前言:今天介紹一SpringBoot的Starter,并手寫一個自己的Starter,在SpringBoot項(xiàng)目中,有各種的Starter提供給開發(fā)者使用,Starter則提供各種API,這樣使開發(fā)SpringBoot項(xiàng)目變得簡單。實(shí)際上Starter簡單來說就是Spring+SpringMVC開發(fā)的。話不多說開始擼代碼
首先在idea中創(chuàng)建SpringBoot項(xiàng)目,并首先創(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,這個類是重點(diǎn),代碼如下:
- @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)值的時候作為默認(rèn)值注入到配置類中
beauty.name=李依依默認(rèn)
beauty.age=18
- 最后再resources中新建一個META-INF文件夾,然后在新建一個文件spring.factories,這個名字和文件夾的名字不能改,加入配置如下,這個表明指定自動配置的類的全路徑,自動配置的時候就找到這個全路徑,實(shí)例化這個對象到容器中
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.mystarter.ActionServiceAutoConfiguration
- 最后一步點(diǎn)擊install,出現(xiàn)Build Success說明這個Starter已經(jīng)安裝到本地maven倉庫中,可以被別人引用
![在這里插入圖片描述](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.測試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)建成功
![在這里插入圖片描述](https://img-blog.csdnimg.cn/2019121823094633.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzMjU1MDE3,size_16,color_FFFFFF,t_70)
>更多的教程請關(guān)注:非科班的科班,路過有空的大佬們點(diǎn)個贊,謝謝大家
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。