Spring Boot是一個(gè)功能強(qiáng)大、靈活且易于使用的框架,它極大地簡化了Spring應(yīng)用程序的開發(fā)和部署流程,使得開發(fā)人員能夠更專注于業(yè)務(wù)邏輯的實(shí)現(xiàn)。本文將詳細(xì)解釋這個(gè)啟動(dòng)類的作用和功能。
在Spring Boot中,啟動(dòng)類是整個(gè)應(yīng)用程序的入口點(diǎn)。一般是放在項(xiàng)目的根路徑下的(推薦放在項(xiàng)目的根路徑下)。它是一個(gè)標(biāo)注了 @SpringBootApplication 注解的 Java 類,必須包含一個(gè)標(biāo)準(zhǔn)的 main 方法,在main方法中添加SpringApplication.run()方法,用于啟動(dòng) Spring Boot 應(yīng)用程序。
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); }}
啟動(dòng)類上邊的@SpringBootApplication是 注解應(yīng)用啟動(dòng)的入口類,它自動(dòng)開啟了許多有用的特性,如自動(dòng)配置、組件掃描、籌劃配置類等,從而減少了開發(fā)人員的配置工作量。@SpringBootApplication是Spring Boot啟動(dòng)類上的核心注解,是一個(gè)組合注解,源碼如下:
@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個(gè)注解:
關(guān)系圖如下:
springbootApplication.png
@SpringBootConfiguration 是Spring Boot提供的特定注解之一,它用于指示一個(gè)類是Spring Boot應(yīng)用程序的配置類。該注解組合了 @Configuration 注解,它表示被標(biāo)注的類可以作為配置類用于配置應(yīng)用程序的上下文。與 @Configuration 注解類似,它通常與其他注解一起使用,用于定義和管理應(yīng)用程序的配置信息。源碼如下:
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 注解源碼如下:
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 是Spring Boot框架中的一個(gè)重要注解,它允許應(yīng)用程序根據(jù)類路徑中的依賴自動(dòng)配置其組件。通過這個(gè)注解,Spring Boot可以根據(jù)應(yīng)用程序的依賴關(guān)系自動(dòng)配置各種組件。源碼如下:
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 是Spring框架中的一個(gè)注解,用于指定Spring在哪些包中尋找組件。它會自動(dòng)掃描并注冊指定包中的所有帶有 @Component 及其派生注解的類作為Spring的Bean。這樣可以方便地將自定義的類納入Spring的上下文中,使得它們可以被自動(dòng)裝配和使用。下面是對 @ComponentScan 注解的詳細(xì)解釋:
源碼如下:
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啟動(dòng)類是構(gòu)建Spring Boot應(yīng)用程序的關(guān)鍵組成部分。它允許開發(fā)人員配置和管理應(yīng)用程序的行為,同時(shí)簡化了應(yīng)用程序的配置和部署過程。通過深入了解Spring Boot啟動(dòng)類的功能和用法,開發(fā)人員可以更好地構(gòu)建和管理復(fù)雜的Spring Boot應(yīng)用程序。希望本文能夠幫助您更好地理解和使用Spring Boot啟動(dòng)類。
本文鏈接:http://www.www897cc.com/showinfo-26-17173-0.htmlSpring Boot 3-啟動(dòng)類詳解,你學(xué)會了嗎?
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。郵件:2376512515@qq.com
上一篇: AutoCAD 產(chǎn)品設(shè)計(jì):文字樣式的字高為 0 的邏輯
下一篇: 什么是API管理?你了解嗎?