最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot自定義Starter實(shí)現(xiàn)流程詳解

 更新時間:2022年09月27日 08:58:46   作者:CaptainCats  
SpringBoot中的starter是一種非常重要的機(jī)制,能夠拋棄以前繁雜的配置,將其統(tǒng)一集成進(jìn)starter,應(yīng)用者只需要在maven中引入starter依賴,SpringBoot就能自動掃描到要加載的信息并啟動相應(yīng)的默認(rèn)配置。starter讓我們擺脫了各種依賴庫的處理,需要配置各種信息的困擾

starter起步依賴

starter起步依賴是springboot一種非常重要的機(jī)制,

它打包了某些場景下需要用到依賴,將其統(tǒng)一集成到starter,

比如,

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>

這就是一個starter,你可以把它看做一個外部外部項(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;
/**
 * 這兩個都是非@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實(shí)現(xiàn)流程詳解的文章就介紹到這了,更多相關(guān)SpringBoot自定義Starter內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

古蔺县| 寻乌县| 漳州市| 英德市| 玛纳斯县| 东台市| 道孚县| 乳山市| 兴化市| 鄢陵县| 揭东县| 秦安县| 新竹市| 沂水县| 横峰县| 遂宁市| 出国| 三门峡市| 喀喇沁旗| 安化县| 班戈县| 邯郸市| 望江县| 常宁市| 佛山市| 五河县| 鞍山市| 罗定市| 密山市| 吴江市| 突泉县| 铁岭市| 建平县| 廉江市| 镇沅| 邻水| 樟树市| 崇明县| 苏尼特右旗| 彭阳县| 淮阳县|