當(dāng)前位置:
首頁(yè) >
前端技术
> javascript
>内容正文
javascript
SpringBoot_入门-HelloWorld细节-自动配置
生活随笔
收集整理的這篇文章主要介紹了
SpringBoot_入门-HelloWorld细节-自动配置
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
前面分析了pom文件的父依賴,以及starter場(chǎng)景啟動(dòng)器,下面說(shuō)一下helloworld主程序,這個(gè)主程序我們只要運(yùn)行main方法就行了,但是運(yùn)行main方法要注意,我們spring應(yīng)用run的時(shí)候,要傳入一個(gè)類,一定是SpringBootApplication來(lái)標(biāo)注的類,如果我把這個(gè)注解注掉,然后我來(lái)運(yùn)行一下,這個(gè)就報(bào)錯(cuò)了,這個(gè)注解是非常重要的,SpringBootApplication,我們來(lái)說(shuō)一下這個(gè)主程序類,主程序類,也是我們的主入口類,他的這段代碼呢,里面有一個(gè)核心注解,叫SpringBootApplication,我們翻譯過(guò)來(lái)就是SpringBoot應(yīng)用,那么這個(gè)類我們標(biāo)注在哪一個(gè)類上,這個(gè)注解我們標(biāo)注在哪個(gè)類上,說(shuō)明這個(gè)類,是SpringBoot的主配置類,Springboot就應(yīng)該運(yùn)行,這個(gè)類的main方法,來(lái)啟動(dòng)Springboot應(yīng)用,我們有一個(gè)非常重要的注解,SpringBootApplication,這個(gè)SpringBootApplication,到底是什么呢,打開(kāi)可以看一下,其實(shí)它是一個(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 {他有這么多的注解來(lái)組成的,我們來(lái)看一下,復(fù)合注解里面呢,第一個(gè)注解叫做SpringBootConfiguration,按照我們注解的名字,翻譯過(guò)來(lái),就叫SpringBoot配置,我們也叫配置類,那么這個(gè)注解,他標(biāo)注在某個(gè)類上,表示這是Springboot的一個(gè)配置類,這個(gè)注解點(diǎn)進(jìn)來(lái)看一下,我們會(huì)非常熟悉/*** 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 {}它上面有一個(gè)注解叫Configuration,Spring里面定義的一個(gè)注解,他標(biāo)在某一個(gè)類上,配置類上來(lái)標(biāo)注這個(gè)注解,我們一直在說(shuō)這個(gè)配置類,所謂的配置類呢,就是和我們的配置文件是一樣的,我們以前開(kāi)發(fā)Spring應(yīng)用,需要編寫非常多的配置文件,這文件一多太麻煩了,那接下來(lái)怎么辦呢,把一個(gè)個(gè)配置文件替換成一個(gè)個(gè)的配置類,當(dāng)然Springboot怎么知道這個(gè)類是做配置的,比如可以給容器中,注入組件等等,以前配置文件的功能她都能夠做,那么我們要讓Springboot知道,這是一個(gè)配置類,標(biāo)注@Configuration注解,Spring就知道了,包括你標(biāo)注@SpringBootConfiguration,效果都是一樣的,只不過(guò)這個(gè)注解是Spring定義的注解,這個(gè)是Springboot定義的注解,而我們這個(gè)配置類呢,其實(shí)他就是一個(gè)組件@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 "";}我們記住配置類也是容器中的一個(gè)組件,這是我們說(shuō)的第一個(gè)注解,叫做SpringBootApplication
然后他的第二個(gè)注解,叫做@EnableAutoConfiguration,開(kāi)啟啟用的意思,開(kāi)啟自動(dòng)配置功能,我們整個(gè)SpringBoot里面沒(méi)有做任何配置,我們SpringMVC也啟動(dòng)起來(lái)了,我們整個(gè)應(yīng)用也能用了,包掃描也掃進(jìn)去了,這些功能都是怎么做的呢,就是我們這個(gè)注解,叫做@EnableAutoConfiguration,開(kāi)啟自動(dòng)配置,以前我們需要配置的東西,我們現(xiàn)在都不需要配置了,然后是SpringBoot幫我們配置,要幫我們自動(dòng)配置,就得需要加上這個(gè)注解,這個(gè)注解告訴SpringBoot,開(kāi)啟自動(dòng)配置功能,這樣我們整個(gè)自動(dòng)配置,這樣自動(dòng)配置功能才能夠生效,而自動(dòng)配置是一個(gè)什么樣的原理,我們還是打開(kāi)來(lái)看一下,EnableAutoConfiguration點(diǎn)進(jìn)來(lái)@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 {};}它里面首先有一個(gè)@AutoConfigurationPackage,他也是一個(gè)組合注解,EnableAutoConfiguration它里面首先有一個(gè)注解,叫AutoConfigurationPackage,那么按照這個(gè)注解呢,翻譯過(guò)來(lái)叫做自動(dòng)配置包,自動(dòng)配置包是什么意思呢,我們點(diǎn)擊這個(gè)注解,/*** 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的底層注解,他的作用就是給容器中,導(dǎo)入一個(gè)組件,導(dǎo)入那個(gè)組件呢,后面的這個(gè)類,在后面寫一些邏輯判斷,然后給他返回,由導(dǎo)入的組件這個(gè)類來(lái)指定,導(dǎo)入哪些組件,由import里面的class類,或者Spring注解的底層原理,我們就說(shuō)一下AutoConfigurationPackages,他利用import,指定這個(gè)類給容器中導(dǎo)入組件,導(dǎo)入了什么組件呢,他這里有一個(gè)方法/*** {@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,注冊(cè)一些bean定義信息,怎么導(dǎo)呢,new PackageImport(metadata).getPackageName(),metadata就是我們注解的原信息,所有的源數(shù)據(jù),getPackageName拿到一個(gè)包名,首先metadata是注解的元信息,是Springboot注解,這是Springboot注解里面的東西,然后它是標(biāo)注在HelloWorldMainApplication上的,元信息都能夠拿得到,主要有一個(gè)new PackageImport(metadata).getPackageName(),可以來(lái)看一下包名,來(lái)計(jì)算一下,這個(gè)包名就叫做com.learn所以這個(gè)注解,AutoConfigurationPackages它本身的含義,將我們主配置類,主配置類就在這,所在的包下邊,所有的組件,都掃描進(jìn)去,這包名是主配置類的,將主配置類,也就是,就是我們SpringBootApplication注解,標(biāo)注的這個(gè)類,所在包及下邊所有子包,里面的所有組件,掃描到Spring容器中,這是非常重要的一句話,所以我們能夠掃描我們的Controller,因?yàn)樗窃谖覀冏∨渲妙惖淖影?那么按照這種想法,我一旦說(shuō)換包了,放到com下的一個(gè)HelloController,這里面才是我們的主類,看能不能掃描進(jìn)來(lái)
我再來(lái)啟動(dòng)看行不行,我們看能不能把外面的Controller掃進(jìn)來(lái)呢,這個(gè)應(yīng)用啟動(dòng)起來(lái)了,它是把error路徑映射到了,但是hello沒(méi)有,如果我們?cè)L問(wèn)hello請(qǐng)求那肯定就是404了localhost:8080/hello報(bào)的是404的錯(cuò)誤,就是掃不進(jìn)來(lái),它是將主配置類下面的,所有配置掃進(jìn)來(lái),這個(gè)就是@AutoConfigurationPackage這個(gè)注解的作用,@EnableAutoConfiguration注解里面還有一個(gè)注解,還標(biāo)了一個(gè)注解是@Import(EnableAutoConfigurationImportSelector.class),我們說(shuō)了import的左右就是,給容器中導(dǎo)入一些組件,導(dǎo)入什么組件呢,就是后面這個(gè)類告訴你,后面這個(gè)類翻譯過(guò)來(lái),開(kāi)啟自動(dòng)配置導(dǎo)包的選擇器,導(dǎo)入哪些組件的選擇器,點(diǎn)進(jìn)來(lái)看要導(dǎo)入哪些組件,@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;}}這里面只有一個(gè)方法叫做isEnabled,我們看到他的父類點(diǎn)進(jìn)來(lái)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);}
}里面有一個(gè)方法叫做selectImports,這個(gè)方法就是用來(lái)告訴Spring容器,到底要導(dǎo)哪些組件,將所有需要導(dǎo)入的組件,以全類名的方式返回,這些組件就會(huì)被添加到容器中,我們到底Selector給容器添加了哪些組件呢,我們分析一下剛才的代碼,我以debug的方式來(lái)進(jìn)行運(yùn)行,運(yùn)行來(lái)到這一步,這是我們獲取注解的原信息,注解是SprintBootApplication,標(biāo)在哪個(gè)類上都有,接下來(lái)放行,這一步返回了listConfigurations,下面都在用,最后還把configurations返回了,這個(gè)configurations數(shù)組,就是我們?nèi)萜髦行枰獙?dǎo)入的組件,有96個(gè),AutoConfiguration,聽(tīng)名字呢,什么的自動(dòng)配置,所以說(shuō)他的作用,最終會(huì)給容器中,會(huì)導(dǎo)入非常多的自動(dòng)配置類,我們叫做AutoConfiguration,這些自動(dòng)配置類的作用,就是給容器中導(dǎo)入這些場(chǎng)景,需要的所有組件,并配置好這些組件,這就是這些配置類,如果我們要做AOP的功能,那AOP的自動(dòng)配置類就配置好,我們要做批處理功能,就有批處理的自動(dòng)配置類,比如我們要做mongodb的功能,那么mongodb的配置就要配置好,這些自動(dòng)配置呢,都是通過(guò)這些自動(dòng)配置類完成的,我們給容器中導(dǎo)入了大量的自動(dòng)配置類,自動(dòng)配置類的細(xì)節(jié),我們后來(lái)在講自動(dòng)配置類的時(shí)候再說(shuō),有了這些自動(dòng)配置類,免去了我們手動(dòng)編寫配置,和注入功能組件等工作,這些工作都不用我們做了,由配置類幫我們做好,那自動(dòng)配置類他怎么就找到的呢,其實(shí)我們有據(jù)可查,他在調(diào)用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;
}調(diào)了這么一個(gè)方法,SpringFactoriesLoader.loadFactoryNames,這個(gè)方法里面?zhèn)鲀蓚€(gè)參數(shù),第一個(gè)參數(shù)的值點(diǎn)進(jìn)來(lái)/*** Return the class used by {@link SpringFactoriesLoader} to load configuration* candidates.* @return the factory class*/
protected Class<?> getSpringFactoriesLoaderFactoryClass() {return EnableAutoConfiguration.class;
}叫EnableAutoConfiguration.class,第二個(gè)參數(shù)就是getBeanClassLoader類加載器機(jī)制了,然后他的作用是什么,他用classLoader類加載器,獲取這個(gè)資源,獲取完這個(gè)資源以后呢,它會(huì)把這個(gè)資源當(dāng)做property配置文件,從Properties中拿出factoryPropertyName,我們工廠的名字,從哪里得呢,叫做"META-INF/spring.factories",他的作用就是從類路徑下,META-INF/spring.factories中,獲取EnableAutoConfiguration指定的值,那么我們可以來(lái)看一下,我們導(dǎo)入的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
就是我們導(dǎo)入的自動(dòng)配置類,所以我們一句話總結(jié),Spring在啟動(dòng)的時(shí)候,從類路徑下,這個(gè)文件夾中獲取值,將這些值作為自動(dòng)配置類,導(dǎo)入到容器中,然后我們這個(gè)自動(dòng)配置類就生效了,他一生效以后呢,就能幫我們進(jìn)行自動(dòng)配置工作了,他只要一進(jìn)行自動(dòng)配置工作,我們就不用寫那么多的代碼了,其實(shí)為什么會(huì)有這么多神奇的效果,就是這個(gè)自動(dòng)配置類,我們沒(méi)寫的配置,其實(shí)人家都幫我們來(lái)寫了,比如我們來(lái)看一個(gè)自動(dòng)配置類,我們現(xiàn)在是WEB應(yīng)用,跟WEB有關(guān)的看一下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 {這個(gè)注解以后我們?cè)敿?xì)再說(shuō),我們先來(lái)說(shuō)一個(gè)@Bean@Bean
@ConditionalOnMissingBean(HiddenHttpMethodFilter.class)
public OrderedHiddenHttpMethodFilter hiddenHttpMethodFilter() {return new OrderedHiddenHttpMethodFilter();
}給容器中添加一個(gè)組件,這個(gè)后來(lái)也會(huì)說(shuō)的,比如給容器中添加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;
}我們以前的配置都給配置類給替換了,我們需要自己指定的配置,自動(dòng)配置類都幫我們做了,其實(shí)在底層用Spring的時(shí)候,一個(gè)都不能少,這個(gè)被Spring官方替我們做了,那么我們主要來(lái)看,這些自動(dòng)配置類,其實(shí)都是在springboot.autoconfigure包下,這個(gè)就是對(duì)整個(gè)J2EE的大整合,比如有跟高級(jí)消息隊(duì)列的,aop的,有做緩存的,有左DAO的整合,一籃子解決方案全都在這,自動(dòng)配置,所以我們這個(gè)SpringBoot就會(huì)這么強(qiáng)大,J2EE整體的解決方案,和自動(dòng)配置,都在我們這個(gè)包里邊,spring-boot-autoconfigure-1.5.12.RELEASE-sources.jar他里面來(lái)幫我們做了這個(gè)事,所以我們用了SpringBoot就不用這個(gè)東西了,一切都有人來(lái)幫我們配,在自動(dòng)配置包里邊,看每一種功能,都是怎么配的,如果不滿意,我們通過(guò)不斷地學(xué)習(xí),也可以自己來(lái)改善一些配置
?
總結(jié)
以上是生活随笔為你收集整理的SpringBoot_入门-HelloWorld细节-自动配置的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: SpringBoot_入门-HelloW
- 下一篇: SpringBoot_配置-proper