摘要:用于主類(lèi)上最最最核心的注解,表示這是一個(gè)項(xiàng)目,用于開(kāi)啟的各項(xiàng)能力。下面我們來(lái)分析一下這個(gè)注解的組成以及作用通過(guò)上面的代碼我們可以看出來(lái)是一個(gè)組合注解,主要由和這三個(gè)注解組成的。通過(guò)源碼可以看出也是一個(gè)組合注解。
??SpringBoot項(xiàng)目一般都會(huì)有Application的入口類(lèi),入口類(lèi)中會(huì)有main方法,這是一個(gè)標(biāo)準(zhǔn)的java應(yīng)用程序的入口方法。@SpringBootApplication用于Spring主類(lèi)上最最最核心的注解,表示這是一個(gè)SpringBoot項(xiàng)目,用于開(kāi)啟SpringBoot的各項(xiàng)能力。
??下面我們來(lái)分析一下@SpringBootApplication這個(gè)注解的組成以及作用
/** * Indicates a {@link Configuration configuration} class that declares one or more * {@link Bean @Bean} methods and also triggers {@link EnableAutoConfiguration * auto-configuration} and {@link ComponentScan component scanning}. This is a convenience * annotation that is equivalent to declaring {@code @Configuration}, * {@code @EnableAutoConfiguration} and {@code @ComponentScan}. * * @author Phillip Webb * @author Stephane Nicoll * @since 1.2.0 */ @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
??通過(guò)上面的代碼我們可以看出來(lái)@SpringBootApplication是一個(gè)組合注解,主要由@SpringBootConfiguration、@EnableAutoConfiguration和@ComponentScan這三個(gè)注解組成的。
@SpringBootConfiguration/** * Indicates that a class provides Spring Boot application * {@link Configuration @Configuration}. Can be used as an alternative to the Spring"s * standard {@code @Configuration} annotation so that configuration can be found * automatically (for example in tests). ** Application should only ever include one {@code @SpringBootConfiguration} and * most idiomatic Spring Boot applications will inherit it from * {@code @SpringBootApplication}. * * @author Phillip Webb * @since 1.4.0 */ @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Configuration public @interface SpringBootConfiguration { }
??通過(guò)源碼可以看出@SpringBootConfiguration也是一個(gè)組合注解。@SpringBootConfiguration是SpringBoot項(xiàng)目的配置注解,標(biāo)識(shí)這是一個(gè)被裝載的Bean,在SpringBoot項(xiàng)目中推薦使用@SpringBootConfiguration替代@Configuration
@EnableAutoConfiguration??@EnableAutoConfiguration的作用是開(kāi)啟自動(dòng)配置功能。以前我們需要配置的東西,SpringBoot幫我們自動(dòng)配置;@EnableAutoConfiguration告訴SpringBoot開(kāi)啟自動(dòng)配置功能;這樣自動(dòng)配置才能生效
/** * Enable auto-configuration of the Spring Application Context, attempting to guess and * configure beans that you are likely to need. Auto-configuration classes are usually * applied based on your classpath and what beans you have defined. For example, if you * have {@code tomcat-embedded.jar} on your classpath you are likely to want a * {@link TomcatServletWebServerFactory} (unless you have defined your own * {@link ServletWebServerFactory} bean). ** When using {@link SpringBootApplication}, the auto-configuration of the context is * automatically enabled and adding this annotation has therefore no additional effect. *
* Auto-configuration tries to be as intelligent as possible and will back-away as you * define more of your own configuration. You can always manually {@link #exclude()} any * configuration that you never want to apply (use {@link #excludeName()} if you don"t * have access to them). You can also exclude them via the * {@code spring.autoconfigure.exclude} property. Auto-configuration is always applied * after user-defined beans have been registered. *
* The package of the class that is annotated with {@code @EnableAutoConfiguration}, * usually via {@code @SpringBootApplication}, has specific significance and is often used * as a "default". For example, it will be used when scanning for {@code @Entity} classes. * It is generally recommended that you place {@code @EnableAutoConfiguration} (if you"re * not using {@code @SpringBootApplication}) in a root package so that all sub-packages * and classes can be searched. *
* Auto-configuration classes are regular Spring {@link Configuration} beans. They are * located using the {@link SpringFactoriesLoader} mechanism (keyed against this class). * Generally auto-configuration beans are {@link Conditional @Conditional} beans (most * often using {@link ConditionalOnClass @ConditionalOnClass} and * {@link ConditionalOnMissingBean @ConditionalOnMissingBean} annotations). * * @author Phillip Webb * @author Stephane Nicoll * @see ConditionalOnBean * @see ConditionalOnMissingBean * @see ConditionalOnClass * @see AutoConfigureAfter * @see SpringBootApplication */ @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @AutoConfigurationPackage @Import(AutoConfigurationImportSelector.class) public @interface EnableAutoConfiguration { String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration"; /** * Exclude specific auto-configuration classes such that they will never be applied. * @return the classes to exclude */ Class>[] exclude() default {}; /** * Exclude specific auto-configuration class names such that they will never be * applied. * @return the class names to exclude * @since 1.3.0 */ String[] excludeName() default {}; }
??當(dāng)我們啟用了@EnableAutoConfiguration注解,啟動(dòng)自動(dòng)配置后,該注解會(huì)使SpringBoot根據(jù)項(xiàng)目中依賴(lài)的jar包自動(dòng)配置項(xiàng)目的配置項(xiàng)。例如:引入了Spring-boot-starter-web的依賴(lài),項(xiàng)目中也就會(huì)引入SpringMVC的依賴(lài),SpringBoot就會(huì)自動(dòng)配置tomcat和SpringMVC
@ComponentScan??@ComponentScan注解會(huì)自動(dòng)掃描包路徑下的所有@Controller、@Service、@Repository、@Component的類(lèi),不配置包路徑的話(huà),SpringBoot中默認(rèn)掃描@SpringBootApplication注解所在類(lèi)的同級(jí)目錄以及子目錄下的相關(guān)注解。
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/75688.html
摘要:常規(guī)的配置讓開(kāi)發(fā)人員將更多的經(jīng)歷耗費(fèi)在了配置文件上。其中有三個(gè)注解,,。以前我們需要配置的東西,幫我們自動(dòng)配置,告訴開(kāi)啟自動(dòng)配置功能,這樣自動(dòng)配置才能生效。 為什么需要自動(dòng)化配置 ??在常規(guī)的spring應(yīng)用程序中,充斥著大量的配置文件,我們需要手動(dòng)去配置這些文件,如配置組件掃描、視圖解析器、http編碼等等。常規(guī)的配置讓開(kāi)發(fā)人員將更多的經(jīng)歷耗費(fèi)在了配置文件上。而這些配置都是一些固定模...
摘要:常規(guī)的配置讓開(kāi)發(fā)人員將更多的經(jīng)歷耗費(fèi)在了配置文件上。其中有三個(gè)注解,,。以前我們需要配置的東西,幫我們自動(dòng)配置,告訴開(kāi)啟自動(dòng)配置功能,這樣自動(dòng)配置才能生效。 為什么需要自動(dòng)化配置 ??在常規(guī)的spring應(yīng)用程序中,充斥著大量的配置文件,我們需要手動(dòng)去配置這些文件,如配置組件掃描、視圖解析器、http編碼等等。常規(guī)的配置讓開(kāi)發(fā)人員將更多的經(jīng)歷耗費(fèi)在了配置文件上。而這些配置都是一些固定模...
摘要:多線(xiàn)程編程這篇文章分析了多線(xiàn)程的優(yōu)缺點(diǎn),如何創(chuàng)建多線(xiàn)程,分享了線(xiàn)程安全和線(xiàn)程通信線(xiàn)程池等等一些知識(shí)。 中間件技術(shù)入門(mén)教程 中間件技術(shù)入門(mén)教程,本博客介紹了 ESB、MQ、JMS 的一些知識(shí)... SpringBoot 多數(shù)據(jù)源 SpringBoot 使用主從數(shù)據(jù)源 簡(jiǎn)易的后臺(tái)管理權(quán)限設(shè)計(jì) 從零開(kāi)始搭建自己權(quán)限管理框架 Docker 多步構(gòu)建更小的 Java 鏡像 Docker Jav...
摘要:?jiǎn)栴}來(lái)了,我們到底還在用嗎答案是,不全用。后者是初始化的配置,主要是的配置。啟動(dòng)類(lèi)測(cè)試啟動(dòng)項(xiàng)目后,在瀏覽器里面輸入。通過(guò)查詢(xún)已裝載的,并且支持該而獲取的。按照前面對(duì)的描述,對(duì)于而言,這個(gè)必定是。的核心在的方法中。 之前已經(jīng)分析過(guò)了Spring的IOC(《零基礎(chǔ)帶你看Spring源碼——IOC控制反轉(zhuǎn)》)與AOP(《從源碼入手,一文帶你讀懂Spring AOP面向切面編程》)的源碼,本次...
閱讀 666·2019-08-30 15:44
閱讀 1386·2019-08-30 11:02
閱讀 2994·2019-08-29 18:42
閱讀 3516·2019-08-29 16:16
閱讀 1724·2019-08-26 13:55
閱讀 1778·2019-08-26 13:45
閱讀 2391·2019-08-26 11:43
閱讀 3256·2019-08-26 10:32