SpringBoot自定義Starter實現(xiàn)流程詳解
starter起步依賴
starter起步依賴是springboot一種非常重要的機(jī)制,
它打包了某些場景下需要用到依賴,將其統(tǒng)一集成到starter,
比如,
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
這就是一個starter,你可以把它看做一個外部外部項目,注意:是外部項目。
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項目



添加依賴
<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;
/**
* 這兩個都是非@Component注解,
* 否則springboot啟動時會直接被加載進(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 + '\'' +
'}';
}
}自動配置類
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();
}
}啟動訪問

到此這篇關(guān)于SpringBoot自定義Starter實現(xiàn)流程詳解的文章就介紹到這了,更多相關(guān)SpringBoot自定義Starter內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解springboot通過Async注解實現(xiàn)異步任務(wù)及回調(diào)的方法
這篇文章主要介紹了springboot通過Async注解實現(xiàn)異步任務(wù)及回調(diào),文中通過一個簡單示例來直觀的理解什么是同步調(diào)用,在單元測試用例中,注入?SyncTask?對象,并在測試用例中執(zhí)行?doTaskOne(),doTaskTwo(),doTaskThree()?三個方法,具體實現(xiàn)方式跟隨小編一起看看吧2022-05-05
linux中nohup?java?-jar啟動java項目的步驟
nohup是一個Unix和Linux命令,用于運(yùn)行關(guān)閉時不會被終止的進(jìn)程,這篇文章主要給大家介紹了關(guān)于linux中nohup?java?-jar啟動java項目的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-08-08
Java使用PreparedStatement接口及ResultSet結(jié)果集的方法示例
這篇文章主要介紹了Java使用PreparedStatement接口及ResultSet結(jié)果集的方法,結(jié)合實例形式分析了PreparedStatement接口及ResultSet結(jié)果集的相關(guān)使用方法與操作注意事項,需要的朋友可以參考下2018-07-07
springboot使用redisRepository和redistemplate操作redis的過程解析
本文給大家介紹springboot整合redis/分別用redisRepository和redistemplate操作redis,本文結(jié)合實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2022-05-05
Java靜態(tài)內(nèi)部類實現(xiàn)單例過程
這篇文章主要介紹了Java靜態(tài)內(nèi)部類實現(xiàn)單例過程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-10-10

