SpringBoot自定義Starter實(shí)現(xiàn)流程詳解
starter起步依賴
starter起步依賴是springboot一種非常重要的機(jī)制,
它打包了某些場(chǎng)景下需要用到依賴,將其統(tǒng)一集成到starter,
比如,
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
這就是一個(gè)starter,你可以把它看做一個(gè)外部外部項(xiàng)目,注意:是外部項(xiàng)目。
starter命名規(guī)則
springboot提供的starter以spring-boot-starter-x的方式命名,
自定義starter以x-spring-boot-starter的方式命名,
以區(qū)分springboot生態(tài)提供的starter。
自定義starter
new module
mystarter-spring-boot-starter

maven項(xiàng)目



添加依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> <version>2.2.9.RELEASE</version> </dependency>

load maven changes

simplebean
package com.duohoob.bean;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* 這兩個(gè)都是非@Component注解,
* 否則springboot啟動(dòng)時(shí)會(huì)直接被加載進(jìn)spring容器
*/
@EnableAutoConfiguration
@ConfigurationProperties(prefix = "simplebean")
public class SimpleBean {
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "SimpleBean{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
'}';
}
}自動(dòng)配置類
package com.duohoob.config;
import com.duohoob.bean.SimpleBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyAutoConfiguration {
@Bean
public SimpleBean simpleBean() {
return new SimpleBean();
}
}META-INF\spring.factories
在resources下創(chuàng)建META-INF\spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.duohoob.config.MyAutoConfiguration
在spring-boot-mytest中引入mystarter-spring-boot-starter
<dependency> <groupId>com.duohoob</groupId> <artifactId>mystarter-spring-boot-starter</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
load maven changes

添加配置
在spring-boot-mytest的src/main/resources/application.properties中添加配置

通過@Autowired引用
package com.duohoob.springbootmytest.controller;
import com.duohoob.bean.SimpleBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@Autowired
private SimpleBean simplebean;
@RequestMapping("/test")
public String test() {
return simplebean.toString();
}
}啟動(dòng)訪問

到此這篇關(guān)于SpringBoot自定義Starter實(shí)現(xiàn)流程詳解的文章就介紹到這了,更多相關(guān)SpringBoot自定義Starter內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
mybatis 解決將數(shù)值0識(shí)別成空字符串的問題
這篇文章主要介紹了mybatis 解決將數(shù)值0識(shí)別成空字符串的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
解決FontConfiguration.getVersion報(bào)空指針異常的問題
這篇文章主要介紹了解決FontConfiguration.getVersion報(bào)空指針異常的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
SpringBoot+OCR實(shí)現(xiàn)PDF內(nèi)容識(shí)別的示例代碼
在SpringBoot中,您可以結(jié)合OCR庫來實(shí)現(xiàn)對(duì)PDF文件內(nèi)容的識(shí)別和提取,本文就來介紹一下如何使用 Tesseract 和 pdf2image 對(duì) PDF 文件進(jìn)行OCR識(shí)別和提取,具有一定的參考價(jià)值,感興趣的可以了解一下2023-12-12
Java計(jì)時(shí)器StopWatch實(shí)現(xiàn)方法代碼實(shí)例
這篇文章主要介紹了Java計(jì)時(shí)器StopWatch實(shí)現(xiàn)方法代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
Spring?Boot?利用?XML?方式整合?MyBatis
這篇文章主要介紹了Spring?Boot?利用?XML?方式整合?MyBatis,文章圍繞主題的相關(guān)資料展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,組要的小伙伴可以參考一下2022-05-05
分享Spring?Cloud?OpenFeign?的五個(gè)優(yōu)化技巧
這篇文章主要分享的是Spring?Cloud?OpenFeign?的五個(gè)優(yōu)化技巧,OpenFeign?是?Spring?官方推出的一種聲明式服務(wù)調(diào)用和負(fù)載均衡組件,更多相關(guān)內(nèi)容需要的小伙伴可以參考一下2022-05-05
springboot jackson自定義序列化和反序列化實(shí)例
這篇文章主要介紹了spring boot jackson自定義序列化和反序列化實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
SpringBoot2.0整合Redis自定義注入bean組件配置的實(shí)戰(zhàn)教程
這篇文章主要介紹了SpringBoot2.0整合Redis自定義注入bean組件配置,我們將基于SpringBoot2.0整合搭建的微服務(wù)項(xiàng)目為奠基,開啟中間件Redis的實(shí)戰(zhàn)之路,需要的朋友可以參考下2023-06-06

