AVt天堂网 手机版,亚洲va久久久噜噜噜久久4399,天天综合亚洲色在线精品,亚洲一级Av无码毛片久久精品

當前位置:首頁 > 科技  > 軟件

Spring Boot 3-啟動類詳解,你學會了嗎?

來源: 責編: 時間:2023-11-06 08:53:57 320觀看
導讀Spring Boot是一個功能強大、靈活且易于使用的框架,它極大地簡化了Spring應用程序的開發和部署流程,使得開發人員能夠更專注于業務邏輯的實現。本文將詳細解釋這個啟動類的作用和功能。Spring Boot啟動類在Spring Boot

Spring Boot是一個功能強大、靈活且易于使用的框架,它極大地簡化了Spring應用程序的開發和部署流程,使得開發人員能夠更專注于業務邏輯的實現。本文將詳細解釋這個啟動類的作用和功能。PZl28資訊網——每日最新資訊28at.com

Spring Boot啟動類

在Spring Boot中,啟動類是整個應用程序的入口點。一般是放在項目的根路徑下的(推薦放在項目的根路徑下)。它是一個標注了 @SpringBootApplication 注解的 Java 類,必須包含一個標準的 main 方法,在main方法中添加SpringApplication.run()方法,用于啟動 Spring Boot 應用程序。PZl28資訊網——每日最新資訊28at.com

import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class XjdocApplication { public static void main(String[] args) {  SpringApplication.run(XjdocApplication.class, args); }}

啟動類上邊的@SpringBootApplication是 注解應用啟動的入口類,它自動開啟了許多有用的特性,如自動配置、組件掃描、籌劃配置類等,從而減少了開發人員的配置工作量。@SpringBootApplication是Spring Boot啟動類上的核心注解,是一個組合注解,源碼如下:PZl28資訊網——每日最新資訊28at.com

@Target({ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Documented@Inherited@SpringBootConfiguration@EnableAutoConfiguration@ComponentScan(    excludeFilters = {@Filter(    type = FilterType.CUSTOM,    classes = {TypeExcludeFilter.class}), @Filter(    type = FilterType.CUSTOM,    classes = {AutoConfigurationExcludeFilter.class})})public @interface SpringBootApplication {  ...}

他主要組合了以下3個注解:PZl28資訊網——每日最新資訊28at.com

  • @SpringBootConfiguration:表明被注解的類是一個配置類,用于定義應用程序的配置信息。
  • @EnableAutoConfiguration:開啟自動配置功能。
  • @ComponentScan:啟用組件掃描,使得Spring能夠自動發現和裝配一些組件

關系圖如下:PZl28資訊網——每日最新資訊28at.com

springbootApplication.pngspringbootApplication.pngPZl28資訊網——每日最新資訊28at.com

注解@SpringBootConfiguration

@SpringBootConfiguration 是Spring Boot提供的特定注解之一,它用于指示一個類是Spring Boot應用程序的配置類。該注解組合了 @Configuration 注解,它表示被標注的類可以作為配置類用于配置應用程序的上下文。與 @Configuration 注解類似,它通常與其他注解一起使用,用于定義和管理應用程序的配置信息。源碼如下:PZl28資訊網——每日最新資訊28at.com

import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;import org.springframework.context.annotation.Configuration;import org.springframework.core.annotation.AliasFor;import org.springframework.stereotype.Indexed;@Target({ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Documented@Configuration@Indexedpublic @interface SpringBootConfiguration {    @AliasFor(        annotation = Configuration.class    )    boolean proxyBeanMethods() default true;}

@Configuration 注解源碼如下:PZl28資訊網——每日最新資訊28at.com

import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;import org.springframework.core.annotation.AliasFor;import org.springframework.stereotype.Component;@Target({ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Documented@Componentpublic @interface Configuration {    @AliasFor(        annotation = Component.class    )    String value() default "";    boolean proxyBeanMethods() default true;    boolean enforceUniqueMethods() default true;}

注解@EnableAutoConfiguration

@EnableAutoConfiguration 是Spring Boot框架中的一個重要注解,它允許應用程序根據類路徑中的依賴自動配置其組件。通過這個注解,Spring Boot可以根據應用程序的依賴關系自動配置各種組件。源碼如下:PZl28資訊網——每日最新資訊28at.com

import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Inherited;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;import org.springframework.context.annotation.Import;@Target({ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Documented@Inherited@AutoConfigurationPackage@Import({AutoConfigurationImportSelector.class})public @interface EnableAutoConfiguration {    String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";    Class<?>[] exclude() default {};    String[] excludeName() default {};}

注解@ComponentScan

@ComponentScan 是Spring框架中的一個注解,用于指定Spring在哪些包中尋找組件。它會自動掃描并注冊指定包中的所有帶有 @Component 及其派生注解的類作為Spring的Bean。這樣可以方便地將自定義的類納入Spring的上下文中,使得它們可以被自動裝配和使用。下面是對 @ComponentScan 注解的詳細解釋:PZl28資訊網——每日最新資訊28at.com

  • 指定掃描包:@ComponentScan 注解允許開發人員指定要掃描的包路徑。在指定的包及其子包(默認當前目錄及所有子目錄)中,所有帶有 @Component 及其派生注解的類都將被自動注冊為Spring的Bean,這也是推薦把它放在項目根路徑下的原因。
  • 自動注冊組件:通過 @ComponentScan 注解,開發人員可以方便地將自定義的類納入Spring的上下文中,使得它們可以被自動裝配和使用。這樣可以提高開發效率,同時減少手動配置的工作量。
  • 定制掃描規則:@ComponentScan 注解還支持一些參數配置,允許開發人員定制掃描規則,如指定特定的注解、過濾規則等,以滿足特定的需求。

源碼如下:PZl28資訊網——每日最新資訊28at.com

import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Repeatable;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;import org.springframework.beans.factory.support.BeanNameGenerator;import org.springframework.core.annotation.AliasFor;@Retention(RetentionPolicy.RUNTIME)@Target({ElementType.TYPE})@Documented@Repeatable(ComponentScans.class)public @interface ComponentScan {  ...}

總結

Spring Boot啟動類是構建Spring Boot應用程序的關鍵組成部分。它允許開發人員配置和管理應用程序的行為,同時簡化了應用程序的配置和部署過程。通過深入了解Spring Boot啟動類的功能和用法,開發人員可以更好地構建和管理復雜的Spring Boot應用程序。希望本文能夠幫助您更好地理解和使用Spring Boot啟動類。PZl28資訊網——每日最新資訊28at.com

本文鏈接:http://www.tebozhan.com/showinfo-26-17173-0.htmlSpring Boot 3-啟動類詳解,你學會了嗎?

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: AutoCAD 產品設計:文字樣式的字高為 0 的邏輯

下一篇: 什么是API管理?你了解嗎?

標簽:
  • 熱門焦點
  • 小米官宣:2023年上半年出貨量中國第一!

    今日早間,小米電視官方微博帶來消息,稱2023年小米電視上半年出貨量達到了中國第一,同時還表示小米電視的巨屏風暴即將開始。“公布一個好消息2023年#小米電視上半年出貨量中國
  • 女孩租房開2小時空調用完100元電費引熱議:5級能耗惹不起 月薪過萬電費也交不起

    近日,江蘇蘇州一女孩租房當天充值了100元電費,開著空調不到2小時發現電費已用完。對于為什么這個快,房東表示,電表壞了這種情況很多,之前也遇到過,給租客換
  • Golang 中的 io 包詳解:組合接口

    io.ReadWriter// ReadWriter is the interface that groups the basic Read and Write methods.type ReadWriter interface { Reader Writer}是對Reader和Writer接口的組合,
  • 十個簡單但很有用的Python裝飾器

    裝飾器(Decorators)是Python中一種強大而靈活的功能,用于修改或增強函數或類的行為。裝飾器本質上是一個函數,它接受另一個函數或類作為參數,并返回一個新的函數或類。它們通常用
  • Python異步IO編程的進程/線程通信實現

    這篇文章再講3種方式,同時講4中進程間通信的方式一、 Python 中線程間通信的實現方式共享變量共享變量是多個線程可以共同訪問的變量。在Python中,可以使用threading模塊中的L
  • 一文搞定Java NIO,以及各種奇葩流

    大家好,我是哪吒。很多朋友問我,如何才能學好IO流,對各種流的概念,云里霧里的,不求甚解。用到的時候,現百度,功能雖然實現了,但是為什么用這個?不知道。更別說效率問題了~下次再遇到,
  • 認真聊聊東方甄選:如何告別低垂的果實

    來源:山核桃作者:財經無忌爆火一年后,俞敏洪和他的東方甄選依舊是頗受外界關心的&ldquo;網紅&rdquo;。7月5日至9日,為期5天的東方甄選&ldquo;甘肅行&rdquo;首次在自有App內直播,
  • 超級標準版旗艦!iQOO 11S全球首發iQOO超算獨顯芯片

    上半年已接近尾聲,截至目前各大品牌旗下的頂級旗艦都已悉數亮相,而下半年即將推出的頂級旗艦已經成為了數碼圈爆料的主流,其中就包括全新的iQOO 11S系
  • 電博會與軟博會實現"線下+云端"的雙線融合

    在本次“電博會”與“軟博會”雙展會利好條件的加持下,既可以發揮展會拉動人流、信息流、資金流實現快速交互流動的作用,繼而推動區域經濟良性發展;又可以聚
Top