摘要:用于主類上最最最核心的注解,表示這是一個項目,用于開啟的各項能力。下面我們來分析一下這個注解的組成以及作用通過上面的代碼我們可以看出來是一個組合注解,主要由和這三個注解組成的。通過源碼可以看出也是一個組合注解。
??SpringBoot項目一般都會有Application的入口類,入口類中會有main方法,這是一個標準的java應用程序的入口方法。@SpringBootApplication用于Spring主類上最最最核心的注解,表示這是一個SpringBoot項目,用于開啟SpringBoot的各項能力。
??下面我們來分析一下@SpringBootApplication這個注解的組成以及作用
/** * 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
??通過上面的代碼我們可以看出來@SpringBootApplication是一個組合注解,主要由@SpringBootConfiguration、@EnableAutoConfiguration和@ComponentScan這三個注解組成的。
@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 { }
??通過源碼可以看出@SpringBootConfiguration也是一個組合注解。@SpringBootConfiguration是SpringBoot項目的配置注解,標識這是一個被裝載的Bean,在SpringBoot項目中推薦使用@SpringBootConfiguration替代@Configuration
@EnableAutoConfiguration??@EnableAutoConfiguration的作用是開啟自動配置功能。以前我們需要配置的東西,SpringBoot幫我們自動配置;@EnableAutoConfiguration告訴SpringBoot開啟自動配置功能;這樣自動配置才能生效
/** * 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 {}; }
??當我們啟用了@EnableAutoConfiguration注解,啟動自動配置后,該注解會使SpringBoot根據項目中依賴的jar包自動配置項目的配置項。例如:引入了Spring-boot-starter-web的依賴,項目中也就會引入SpringMVC的依賴,SpringBoot就會自動配置tomcat和SpringMVC
@ComponentScan??@ComponentScan注解會自動掃描包路徑下的所有@Controller、@Service、@Repository、@Component的類,不配置包路徑的話,SpringBoot中默認掃描@SpringBootApplication注解所在類的同級目錄以及子目錄下的相關注解。
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/75688.html
摘要:常規的配置讓開發人員將更多的經歷耗費在了配置文件上。其中有三個注解,,。以前我們需要配置的東西,幫我們自動配置,告訴開啟自動配置功能,這樣自動配置才能生效。 為什么需要自動化配置 ??在常規的spring應用程序中,充斥著大量的配置文件,我們需要手動去配置這些文件,如配置組件掃描、視圖解析器、http編碼等等。常規的配置讓開發人員將更多的經歷耗費在了配置文件上。而這些配置都是一些固定模...
摘要:常規的配置讓開發人員將更多的經歷耗費在了配置文件上。其中有三個注解,,。以前我們需要配置的東西,幫我們自動配置,告訴開啟自動配置功能,這樣自動配置才能生效。 為什么需要自動化配置 ??在常規的spring應用程序中,充斥著大量的配置文件,我們需要手動去配置這些文件,如配置組件掃描、視圖解析器、http編碼等等。常規的配置讓開發人員將更多的經歷耗費在了配置文件上。而這些配置都是一些固定模...
摘要:問題來了,我們到底還在用嗎答案是,不全用。后者是初始化的配置,主要是的配置。啟動類測試啟動項目后,在瀏覽器里面輸入。通過查詢已裝載的,并且支持該而獲取的。按照前面對的描述,對于而言,這個必定是。的核心在的方法中。 之前已經分析過了Spring的IOC(《零基礎帶你看Spring源碼——IOC控制反轉》)與AOP(《從源碼入手,一文帶你讀懂Spring AOP面向切面編程》)的源碼,本次...
閱讀 654·2019-08-30 15:44
閱讀 1379·2019-08-30 11:02
閱讀 2980·2019-08-29 18:42
閱讀 3506·2019-08-29 16:16
閱讀 1719·2019-08-26 13:55
閱讀 1768·2019-08-26 13:45
閱讀 2384·2019-08-26 11:43
閱讀 3246·2019-08-26 10:32