日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

SpringBoot_入门-HelloWorld细节-自动配置

發布時間:2024/4/13 javascript 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringBoot_入门-HelloWorld细节-自动配置 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
前面分析了pom文件的父依賴,以及starter場景啟動器,下面說一下helloworld主程序,這個主程序我們只要運行main方法就行了,但是運行main方法要注意,我們spring應用run的時候,要傳入一個類,一定是SpringBootApplication來標注的類,如果我把這個注解注掉,然后我來運行一下,這個就報錯了,這個注解是非常重要的,SpringBootApplication,我們來說一下這個主程序類,主程序類,也是我們的主入口類,他的這段代碼呢,里面有一個核心注解,叫SpringBootApplication,我們翻譯過來就是SpringBoot應用,那么這個類我們標注在哪一個類上,這個注解我們標注在哪個類上,說明這個類,是SpringBoot的主配置類,Springboot就應該運行,這個類的main方法,來啟動Springboot應用,我們有一個非常重要的注解,SpringBootApplication,這個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 {他有這么多的注解來組成的,我們來看一下,復合注解里面呢,第一個注解叫做SpringBootConfiguration,按照我們注解的名字,翻譯過來,就叫SpringBoot配置,我們也叫配置類,那么這個注解,他標注在某個類上,表示這是Springboot的一個配置類,這個注解點進來看一下,我們會非常熟悉/*** 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).* <p>* Application should only ever include <em>one</em> {@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 {}它上面有一個注解叫Configuration,Spring里面定義的一個注解,他標在某一個類上,配置類上來標注這個注解,我們一直在說這個配置類,所謂的配置類呢,就是和我們的配置文件是一樣的,我們以前開發Spring應用,需要編寫非常多的配置文件,這文件一多太麻煩了,那接下來怎么辦呢,把一個個配置文件替換成一個個的配置類,當然Springboot怎么知道這個類是做配置的,比如可以給容器中,注入組件等等,以前配置文件的功能她都能夠做,那么我們要讓Springboot知道,這是一個配置類,標注@Configuration注解,Spring就知道了,包括你標注@SpringBootConfiguration,效果都是一樣的,只不過這個注解是Spring定義的注解,這個是Springboot定義的注解,而我們這個配置類呢,其實他就是一個組件@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Component public @interface Configuration {/*** Explicitly specify the name of the Spring bean definition associated* with this Configuration class. If left unspecified (the common case),* a bean name will be automatically generated.* <p>The custom name applies only if the Configuration class is picked up via* component scanning or supplied directly to a {@link AnnotationConfigApplicationContext}.* If the Configuration class is registered as a traditional XML bean definition,* the name/id of the bean element will take precedence.* @return the suggested component name, if any (or empty String otherwise)* @see org.springframework.beans.factory.support.DefaultBeanNameGenerator*/String value() default "";}我們記住配置類也是容器中的一個組件,這是我們說的第一個注解,叫做SpringBootApplication 然后他的第二個注解,叫做@EnableAutoConfiguration,開啟啟用的意思,開啟自動配置功能,我們整個SpringBoot里面沒有做任何配置,我們SpringMVC也啟動起來了,我們整個應用也能用了,包掃描也掃進去了,這些功能都是怎么做的呢,就是我們這個注解,叫做@EnableAutoConfiguration,開啟自動配置,以前我們需要配置的東西,我們現在都不需要配置了,然后是SpringBoot幫我們配置,要幫我們自動配置,就得需要加上這個注解,這個注解告訴SpringBoot,開啟自動配置功能,這樣我們整個自動配置,這樣自動配置功能才能夠生效,而自動配置是一個什么樣的原理,我們還是打開來看一下,EnableAutoConfiguration點進來@SuppressWarnings("deprecation") @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @AutoConfigurationPackage @Import(EnableAutoConfigurationImportSelector.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 {};}它里面首先有一個@AutoConfigurationPackage,他也是一個組合注解,EnableAutoConfiguration它里面首先有一個注解,叫AutoConfigurationPackage,那么按照這個注解呢,翻譯過來叫做自動配置包,自動配置包是什么意思呢,我們點擊這個注解,/*** Indicates that the package containing the annotated class should be registered with* {@link AutoConfigurationPackages}.** @author Phillip Webb* @since 1.3.0* @see AutoConfigurationPackages*/ @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @Import(AutoConfigurationPackages.Registrar.class) public @interface AutoConfigurationPackage {}它是用@Import(AutoConfigurationPackages.Registrar.class)注解完成的功能,而Import就是Spring的底層注解,他的作用就是給容器中,導入一個組件,導入那個組件呢,后面的這個類,在后面寫一些邏輯判斷,然后給他返回,由導入的組件這個類來指定,導入哪些組件,由import里面的class類,或者Spring注解的底層原理,我們就說一下AutoConfigurationPackages,他利用import,指定這個類給容器中導入組件,導入了什么組件呢,他這里有一個方法/*** {@link ImportBeanDefinitionRegistrar} to store the base package from the importing* configuration.*/ @Order(Ordered.HIGHEST_PRECEDENCE) static class Registrar implements ImportBeanDefinitionRegistrar, DeterminableImports {@Overridepublic void registerBeanDefinitions(AnnotationMetadata metadata,BeanDefinitionRegistry registry) {register(registry, new PackageImport(metadata).getPackageName());}@Overridepublic Set<Object> determineImports(AnnotationMetadata metadata) {return Collections.<Object>singleton(new PackageImport(metadata));}}叫做registerBeanDefinitions,注冊一些bean定義信息,怎么導呢,new PackageImport(metadata).getPackageName(),metadata就是我們注解的原信息,所有的源數據,getPackageName拿到一個包名,首先metadata是注解的元信息,是Springboot注解,這是Springboot注解里面的東西,然后它是標注在HelloWorldMainApplication上的,元信息都能夠拿得到,主要有一個new PackageImport(metadata).getPackageName(),可以來看一下包名,來計算一下,這個包名就叫做com.learn所以這個注解,AutoConfigurationPackages它本身的含義,將我們主配置類,主配置類就在這,所在的包下邊,所有的組件,都掃描進去,這包名是主配置類的,將主配置類,也就是,就是我們SpringBootApplication注解,標注的這個類,所在包及下邊所有子包,里面的所有組件,掃描到Spring容器中,這是非常重要的一句話,所以我們能夠掃描我們的Controller,因為它是在我們住配置類的子包下,那么按照這種想法,我一旦說換包了,放到com下的一個HelloController,這里面才是我們的主類,看能不能掃描進來 我再來啟動看行不行,我們看能不能把外面的Controller掃進來呢,這個應用啟動起來了,它是把error路徑映射到了,但是hello沒有,如果我們訪問hello請求那肯定就是404了localhost:8080/hello報的是404的錯誤,就是掃不進來,它是將主配置類下面的,所有配置掃進來,這個就是@AutoConfigurationPackage這個注解的作用,@EnableAutoConfiguration注解里面還有一個注解,還標了一個注解是@Import(EnableAutoConfigurationImportSelector.class),我們說了import的左右就是,給容器中導入一些組件,導入什么組件呢,就是后面這個類告訴你,后面這個類翻譯過來,開啟自動配置導包的選擇器,導入哪些組件的選擇器,點進來看要導入哪些組件,@Deprecated public class EnableAutoConfigurationImportSelectorextends AutoConfigurationImportSelector {@Overrideprotected boolean isEnabled(AnnotationMetadata metadata) {if (getClass().equals(EnableAutoConfigurationImportSelector.class)) {return getEnvironment().getProperty(EnableAutoConfiguration.ENABLED_OVERRIDE_PROPERTY, Boolean.class,true);}return true;}}這里面只有一個方法叫做isEnabled,我們看到他的父類點進來AutoConfigurationImportSelector@Override public String[] selectImports(AnnotationMetadata annotationMetadata) {if (!isEnabled(annotationMetadata)) {return NO_IMPORTS;}try {AutoConfigurationMetadata autoConfigurationMetadata = AutoConfigurationMetadataLoader.loadMetadata(this.beanClassLoader);AnnotationAttributes attributes = getAttributes(annotationMetadata);List<String> configurations = getCandidateConfigurations(annotationMetadata,attributes);configurations = removeDuplicates(configurations);configurations = sort(configurations, autoConfigurationMetadata);Set<String> exclusions = getExclusions(annotationMetadata, attributes);checkExcludedClasses(configurations, exclusions);configurations.removeAll(exclusions);configurations = filter(configurations, autoConfigurationMetadata);fireAutoConfigurationImportEvents(configurations, exclusions);return configurations.toArray(new String[configurations.size()]);}catch (IOException ex) {throw new IllegalStateException(ex);} }里面有一個方法叫做selectImports,這個方法就是用來告訴Spring容器,到底要導哪些組件,將所有需要導入的組件,以全類名的方式返回,這些組件就會被添加到容器中,我們到底Selector給容器添加了哪些組件呢,我們分析一下剛才的代碼,我以debug的方式來進行運行,運行來到這一步,這是我們獲取注解的原信息,注解是SprintBootApplication,標在哪個類上都有,接下來放行,這一步返回了listConfigurations,下面都在用,最后還把configurations返回了,這個configurations數組,就是我們容器中需要導入的組件,有96個,AutoConfiguration,聽名字呢,什么的自動配置,所以說他的作用,最終會給容器中,會導入非常多的自動配置類,我們叫做AutoConfiguration,這些自動配置類的作用,就是給容器中導入這些場景,需要的所有組件,并配置好這些組件,這就是這些配置類,如果我們要做AOP的功能,那AOP的自動配置類就配置好,我們要做批處理功能,就有批處理的自動配置類,比如我們要做mongodb的功能,那么mongodb的配置就要配置好,這些自動配置呢,都是通過這些自動配置類完成的,我們給容器中導入了大量的自動配置類,自動配置類的細節,我們后來在講自動配置類的時候再說,有了這些自動配置類,免去了我們手動編寫配置,和注入功能組件等工作,這些工作都不用我們做了,由配置類幫我們做好,那自動配置類他怎么就找到的呢,其實我們有據可查,他在調用getCandidateConfigurations,獲取配置文件,/*** Return the auto-configuration class names that should be considered. By default* this method will load candidates using {@link SpringFactoriesLoader} with* {@link #getSpringFactoriesLoaderFactoryClass()}.* @param metadata the source metadata* @param attributes the {@link #getAttributes(AnnotationMetadata) annotation* attributes}* @return a list of candidate configurations*/ protected List<String> getCandidateConfigurations(AnnotationMetadata metadata,AnnotationAttributes attributes) {List<String> configurations = SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(), getBeanClassLoader());Assert.notEmpty(configurations,"No auto configuration classes found in META-INF/spring.factories. If you "+ "are using a custom packaging, make sure that file is correct.");return configurations; }調了這么一個方法,SpringFactoriesLoader.loadFactoryNames,這個方法里面傳兩個參數,第一個參數的值點進來/*** Return the class used by {@link SpringFactoriesLoader} to load configuration* candidates.* @return the factory class*/ protected Class<?> getSpringFactoriesLoaderFactoryClass() {return EnableAutoConfiguration.class; }叫EnableAutoConfiguration.class,第二個參數就是getBeanClassLoader類加載器機制了,然后他的作用是什么,他用classLoader類加載器,獲取這個資源,獲取完這個資源以后呢,它會把這個資源當做property配置文件,從Properties中拿出factoryPropertyName,我們工廠的名字,從哪里得呢,叫做"META-INF/spring.factories",他的作用就是從類路徑下,META-INF/spring.factories中,獲取EnableAutoConfiguration指定的值,那么我們可以來看一下,我們導入的jar包類路徑里邊

# Initializers org.springframework.context.ApplicationContextInitializer=\ org.springframework.boot.autoconfigure.SharedMetadataReaderFactoryContextInitializer,\ org.springframework.boot.autoconfigure.logging.AutoConfigurationReportLoggingInitializer# Application Listeners org.springframework.context.ApplicationListener=\ org.springframework.boot.autoconfigure.BackgroundPreinitializer# Auto Configuration Import Listeners org.springframework.boot.autoconfigure.AutoConfigurationImportListener=\ org.springframework.boot.autoconfigure.condition.ConditionEvaluationReportAutoConfigurationImportListener# Auto Configuration Import Filters org.springframework.boot.autoconfigure.AutoConfigurationImportFilter=\ org.springframework.boot.autoconfigure.condition.OnClassCondition# Auto Configure org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\ org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\ org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\ org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,\ org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration,\ org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration,\ org.springframework.boot.autoconfigure.cloud.CloudAutoConfiguration,\ org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration,\ org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration,\ org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration,\ org.springframework.boot.autoconfigure.couchbase.CouchbaseAutoConfiguration,\ org.springframework.boot.autoconfigure.dao.PersistenceExceptionTranslationAutoConfiguration,\ org.springframework.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration,\ org.springframework.boot.autoconfigure.data.cassandra.CassandraRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.couchbase.CouchbaseDataAutoConfiguration,\ org.springframework.boot.autoconfigure.data.couchbase.CouchbaseRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchAutoConfiguration,\ org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration,\ org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.ldap.LdapDataAutoConfiguration,\ org.springframework.boot.autoconfigure.data.ldap.LdapRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration,\ org.springframework.boot.autoconfigure.data.mongo.MongoRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.neo4j.Neo4jDataAutoConfiguration,\ org.springframework.boot.autoconfigure.data.neo4j.Neo4jRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.solr.SolrRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration,\ org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.rest.RepositoryRestMvcAutoConfiguration,\ org.springframework.boot.autoconfigure.data.web.SpringDataWebAutoConfiguration,\ org.springframework.boot.autoconfigure.elasticsearch.jest.JestAutoConfiguration,\ org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration,\ org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration,\ org.springframework.boot.autoconfigure.h2.H2ConsoleAutoConfiguration,\ org.springframework.boot.autoconfigure.hateoas.HypermediaAutoConfiguration,\ org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration,\ org.springframework.boot.autoconfigure.hazelcast.HazelcastJpaDependencyAutoConfiguration,\ org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration,\ org.springframework.boot.autoconfigure.integration.IntegrationAutoConfiguration,\ org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration,\ org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,\ org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration,\ org.springframework.boot.autoconfigure.jdbc.JndiDataSourceAutoConfiguration,\ org.springframework.boot.autoconfigure.jdbc.XADataSourceAutoConfiguration,\ org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,\ org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration,\ org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration,\ org.springframework.boot.autoconfigure.jms.JndiConnectionFactoryAutoConfiguration,\ org.springframework.boot.autoconfigure.jms.activemq.ActiveMQAutoConfiguration,\ org.springframework.boot.autoconfigure.jms.artemis.ArtemisAutoConfiguration,\ org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration,\ org.springframework.boot.autoconfigure.groovy.template.GroovyTemplateAutoConfiguration,\ org.springframework.boot.autoconfigure.jersey.JerseyAutoConfiguration,\ org.springframework.boot.autoconfigure.jooq.JooqAutoConfiguration,\ org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration,\ org.springframework.boot.autoconfigure.ldap.embedded.EmbeddedLdapAutoConfiguration,\ org.springframework.boot.autoconfigure.ldap.LdapAutoConfiguration,\ org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration,\ org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration,\ org.springframework.boot.autoconfigure.mail.MailSenderValidatorAutoConfiguration,\ org.springframework.boot.autoconfigure.mobile.DeviceResolverAutoConfiguration,\ org.springframework.boot.autoconfigure.mobile.DeviceDelegatingViewResolverAutoConfiguration,\ org.springframework.boot.autoconfigure.mobile.SitePreferenceAutoConfiguration,\ org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration,\ org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration,\ org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration,\ org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,\ org.springframework.boot.autoconfigure.reactor.ReactorAutoConfiguration,\ org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration,\ org.springframework.boot.autoconfigure.security.SecurityFilterAutoConfiguration,\ org.springframework.boot.autoconfigure.security.FallbackWebSecurityAutoConfiguration,\ org.springframework.boot.autoconfigure.security.oauth2.OAuth2AutoConfiguration,\ org.springframework.boot.autoconfigure.sendgrid.SendGridAutoConfiguration,\ org.springframework.boot.autoconfigure.session.SessionAutoConfiguration,\ org.springframework.boot.autoconfigure.social.SocialWebAutoConfiguration,\ org.springframework.boot.autoconfigure.social.FacebookAutoConfiguration,\ org.springframework.boot.autoconfigure.social.LinkedInAutoConfiguration,\ org.springframework.boot.autoconfigure.social.TwitterAutoConfiguration,\ org.springframework.boot.autoconfigure.solr.SolrAutoConfiguration,\ org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration,\ org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration,\ org.springframework.boot.autoconfigure.transaction.jta.JtaAutoConfiguration,\ org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration,\ org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration,\ org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration,\ org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration,\ org.springframework.boot.autoconfigure.web.HttpEncodingAutoConfiguration,\ org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration,\ org.springframework.boot.autoconfigure.web.MultipartAutoConfiguration,\ org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration,\ org.springframework.boot.autoconfigure.web.WebClientAutoConfiguration,\ org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration,\ org.springframework.boot.autoconfigure.websocket.WebSocketAutoConfiguration,\ org.springframework.boot.autoconfigure.websocket.WebSocketMessagingAutoConfiguration,\ org.springframework.boot.autoconfigure.webservices.WebServicesAutoConfiguration# Failure analyzers org.springframework.boot.diagnostics.FailureAnalyzer=\ org.springframework.boot.autoconfigure.diagnostics.analyzer.NoSuchBeanDefinitionFailureAnalyzer,\ org.springframework.boot.autoconfigure.jdbc.DataSourceBeanCreationFailureAnalyzer,\ org.springframework.boot.autoconfigure.jdbc.HikariDriverConfigurationFailureAnalyzer# Template availability providers org.springframework.boot.autoconfigure.template.TemplateAvailabilityProvider=\ org.springframework.boot.autoconfigure.freemarker.FreeMarkerTemplateAvailabilityProvider,\ org.springframework.boot.autoconfigure.mustache.MustacheTemplateAvailabilityProvider,\ org.springframework.boot.autoconfigure.groovy.template.GroovyTemplateAvailabilityProvider,\ org.springframework.boot.autoconfigure.thymeleaf.ThymeleafTemplateAvailabilityProvider,\ org.springframework.boot.autoconfigure.web.JspTemplateAvailabilityProvider 就是我們導入的自動配置類,所以我們一句話總結,Spring在啟動的時候,從類路徑下,這個文件夾中獲取值,將這些值作為自動配置類,導入到容器中,然后我們這個自動配置類就生效了,他一生效以后呢,就能幫我們進行自動配置工作了,他只要一進行自動配置工作,我們就不用寫那么多的代碼了,其實為什么會有這么多神奇的效果,就是這個自動配置類,我們沒寫的配置,其實人家都幫我們來寫了,比如我們來看一個自動配置類,我們現在是WEB應用,跟WEB有關的看一下org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration,\這里有WEB MVC,@Configuration @ConditionalOnWebApplication @ConditionalOnClass({ Servlet.class, DispatcherServlet.class,WebMvcConfigurerAdapter.class }) @ConditionalOnMissingBean(WebMvcConfigurationSupport.class) @AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10) @AutoConfigureAfter({ DispatcherServletAutoConfiguration.class,ValidationAutoConfiguration.class }) public class WebMvcAutoConfiguration {這個注解以后我們詳細再說,我們先來說一個@Bean@Bean @ConditionalOnMissingBean(HiddenHttpMethodFilter.class) public OrderedHiddenHttpMethodFilter hiddenHttpMethodFilter() {return new OrderedHiddenHttpMethodFilter(); }給容器中添加一個組件,這個后來也會說的,比如給容器中添加filter組件,包括給容器中做一些配置,給容器中添加視圖解析器@Bean @ConditionalOnBean(View.class) @ConditionalOnMissingBean public BeanNameViewResolver beanNameViewResolver() {BeanNameViewResolver resolver = new BeanNameViewResolver();resolver.setOrder(Ordered.LOWEST_PRECEDENCE - 10);return resolver; }@Bean @ConditionalOnMissingBean public InternalResourceViewResolver defaultViewResolver() {InternalResourceViewResolver resolver = new InternalResourceViewResolver();resolver.setPrefix(this.mvcProperties.getView().getPrefix());resolver.setSuffix(this.mvcProperties.getView().getSuffix());return resolver; }我們以前的配置都給配置類給替換了,我們需要自己指定的配置,自動配置類都幫我們做了,其實在底層用Spring的時候,一個都不能少,這個被Spring官方替我們做了,那么我們主要來看,這些自動配置類,其實都是在springboot.autoconfigure包下,這個就是對整個J2EE的大整合,比如有跟高級消息隊列的,aop的,有做緩存的,有左DAO的整合,一籃子解決方案全都在這,自動配置,所以我們這個SpringBoot就會這么強大,J2EE整體的解決方案,和自動配置,都在我們這個包里邊,spring-boot-autoconfigure-1.5.12.RELEASE-sources.jar他里面來幫我們做了這個事,所以我們用了SpringBoot就不用這個東西了,一切都有人來幫我們配,在自動配置包里邊,看每一種功能,都是怎么配的,如果不滿意,我們通過不斷地學習,也可以自己來改善一些配置

?

總結

以上是生活随笔為你收集整理的SpringBoot_入门-HelloWorld细节-自动配置的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。