【移動應(yīng)用開發(fā)技術(shù)】SpringBoot自動裝配原理的知識點有哪些_第1頁
【移動應(yīng)用開發(fā)技術(shù)】SpringBoot自動裝配原理的知識點有哪些_第2頁
【移動應(yīng)用開發(fā)技術(shù)】SpringBoot自動裝配原理的知識點有哪些_第3頁
【移動應(yīng)用開發(fā)技術(shù)】SpringBoot自動裝配原理的知識點有哪些_第4頁
【移動應(yīng)用開發(fā)技術(shù)】SpringBoot自動裝配原理的知識點有哪些_第5頁
已閱讀5頁,還剩10頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

【移動應(yīng)用開發(fā)技術(shù)】SpringBoot自動裝配原理的知識點有哪些

這篇文章主要介紹“SpringBoot自動裝配原理的知識點有哪些”,在日常操作中,相信很多人在SpringBoot自動裝配原理的知識點有哪些問題上存在疑惑,在下查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”SpringBoot自動裝配原理的知識點有哪些”的疑惑有所幫助!接下來,請跟著在下一起來學習吧!SpringBoot自動裝配原理自動裝配原理pom.xmlspring-boot-dependencies:核心依賴在父工程中!我們在寫或者寫入一些springboot依賴的時候,不需要指定版本,就因為有這些版本倉庫啟動器

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter</artifactId>

</dependency>啟動器:說白了就是SpringBoot的啟動場景比如:spring-boot-starter-web,他就會幫我們自動導(dǎo)入web環(huán)境所有的依賴!SpringBoot會將所有的功能場景,都變成一個個的啟動器我們要使用什么功能,就只需要找到對用的啟動器就可以了starter主程序@SpringBootApplication

//

標注這個類是一個Springboot的應(yīng)用

public

class

SpringbootHelloworldApplication

{

public

static

void

main(String[]

args)

{

//

將SpringBoot應(yīng)用啟動

SpringApplication.run(SpringbootHelloworldApplication.class,

args);

}

}注解原理:SpringBoot啟動的時候加載主配置類,開啟了自動配置功能@EnableAutoConfiguration@SpringBootApplication

//

加載主配置類

@EnableAutoConfiguration//

開啟了自動配置功能

@Import({AutoConfigurationImportSelector.class})

AutoConfigurationImportSelector

//

這個新版本的,舊版本是

EnableAutoConfigurationImportSelector

-

利用AutoConfigurationImportSelector給容器中導(dǎo)入一些組件

-

可以查看selectImports()方法的內(nèi)容;

-

List<String>

configurations

=

this.getCandidateConfigurations(annotationMetadata,

attributes);//

獲取候選的配置

protected

List<String>

getCandidateConfigurations(AnnotationMetadata

metadata,

AnnotationAttributes

attributes)

{

List<String>

configurations

=

SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(),

this.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()

掃描所有jar包類路徑下

META-INF/spring.factories

把掃描到的這些文件的內(nèi)容包裝成List對象

從List中獲取到EnableAutoConfiguration.class類(類名)對應(yīng)的值,然后把他們添加在容器中將類路徑下META-INF/spring.factories里面配置的所有EnableAutoConfiguration的值加入到了容器中#

Initializers

org.springframework.context.ApplicationContextInitializer=\

org.springframework.boot.autoconfigure.SharedMetadataReaderFactoryContextInitializer,\

org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener

#

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.OnBeanCondition,\

org.springframework.boot.autoconfigure.condition.OnClassCondition,\

org.springframework.boot.autoconfigure.condition.OnWebApplicationCondition

#

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.context.ConfigurationPropertiesAutoConfiguration,\

org.springframework.boot.autoconfigure.context.LifecycleAutoConfiguration,\

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.CassandraReactiveDataAutoConfiguration,\

org.springframework.boot.autoconfigure.data.cassandra.CassandraReactiveRepositoriesAutoConfiguration,\

org.springframework.boot.autoconfigure.data.cassandra.CassandraRepositoriesAutoConfiguration,\

org.springframework.boot.autoconfigure.data.couchbase.CouchbaseDataAutoConfiguration,\

org.springframework.boot.autoconfigure.data.couchbase.CouchbaseReactiveDataAutoConfiguration,\

org.springframework.boot.autoconfigure.data.couchbase.CouchbaseReactiveRepositoriesAutoConfiguration,\

org.springframework.boot.autoconfigure.data.couchbase.CouchbaseRepositoriesAutoConfiguration,\

org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration,\

org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchRepositoriesAutoConfiguration,\

org.springframework.boot.autoconfigure.data.elasticsearch.ReactiveElasticsearchRepositoriesAutoConfiguration,\

org.springframework.boot.autoconfigure.data.elasticsearch.ReactiveElasticsearchRestClientAutoConfiguration,\

org.springframework.boot.autoconfigure.data.jdbc.JdbcRepositoriesAutoConfiguration,\

org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration,\

org.springframework.boot.autoconfigure.data.ldap.LdapRepositoriesAutoConfiguration,\

org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration,\

org.springframework.boot.autoconfigure.data.mongo.MongoReactiveDataAutoConfiguration,\

org.springframework.boot.autoconfigure.data.mongo.MongoReactiveRepositoriesAutoConfiguration,\

org.springframework.boot.autoconfigure.data.mongo.MongoRepositoriesAutoConfiguration,\

org.springframework.boot.autoconfigure.data.neo4j.Neo4jDataAutoConfiguration,\

org.springframework.boot.autoconfigure.data.neo4j.Neo4jReactiveDataAutoConfiguration,\

org.springframework.boot.autoconfigure.data.neo4j.Neo4jReactiveRepositoriesAutoConfiguration,\

org.springframework.boot.autoconfigure.data.neo4j.Neo4jRepositoriesAutoConfiguration,\

org.springframework.boot.autoconfigure.data.solr.SolrRepositoriesAutoConfiguration,\

org.springframework.boot.autoconfigure.data.r2dbc.R2dbcDataAutoConfiguration,\

org.springframework.boot.autoconfigure.data.r2dbc.R2dbcRepositoriesAutoConfiguration,\

org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration,\

org.springframework.boot.autoconfigure.data.redis.RedisReactiveAutoConfiguration,\

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.ElasticsearchRestClientAutoConfiguration,\

org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration,\

org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration,\

org.springframework.boot.autoconfigure.groovy.template.GroovyTemplateAutoConfiguration,\

org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration,\

org.springframework.boot.autoconfigure.h3.H2ConsoleAutoConfiguration,\

org.springframework.boot.autoconfigure.hateoas.HypermediaAutoConfiguration,\

org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration,\

org.springframework.boot.autoconfigure.hazelcast.HazelcastJpaDependencyAutoConfiguration,\

org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration,\

org.springframework.boot.autoconfigure.http.codec.CodecsAutoConfiguration,\

org.springframework.boot.autoconfigure.influx.InfluxDbAutoConfiguration,\

.ProjectInfoAutoConfiguration,\

egration.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.jersey.JerseyAutoConfiguration,\

org.springframework.boot.autoconfigure.jooq.JooqAutoConfiguration,\

org.springframework.boot.autoconfigure.jsonb.JsonbAutoConfiguration,\

org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration,\

org.springframework.boot.autoconfigure.availability.ApplicationAvailabilityAutoConfiguration,\

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.mongo.embedded.EmbeddedMongoAutoConfiguration,\

org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration,\

org.springframework.boot.autoconfigure.mongo.MongoReactiveAutoConfiguration,\

org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration,\

org.springframework.boot.autoconfigure.neo4j.Neo4jAutoConfiguration,\

org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,\

org.springframework.boot.autoconfigure.quartz.QuartzAutoConfiguration,\

org.springframework.boot.autoconfigure.r2dbc.R2dbcAutoConfiguration,\

org.springframework.boot.autoconfigure.r2dbc.R2dbcTransactionManagerAutoConfiguration,\

org.springframework.boot.autoconfigure.rsocket.RSocketMessagingAutoConfiguration,\

org.springframework.boot.autoconfigure.rsocket.RSocketRequesterAutoConfiguration,\

org.springframework.boot.autoconfigure.rsocket.RSocketServerAutoConfiguration,\

org.springframework.boot.autoconfigure.rsocket.RSocketStrategiesAutoConfiguration,\

org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration,\

org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration,\

org.springframework.boot.autoconfigure.security.servlet.SecurityFilterAutoConfiguration,\

org.springframework.boot.autoconfigure.security.reactive.ReactiveSecurityAutoConfiguration,\

org.springframework.boot.autoconfigure.security.reactive.ReactiveUserDetailsServiceAutoConfiguration,\

org.springframework.boot.autoconfigure.security.rsocket.RSocketSecurityAutoConfiguration,\

org.springframework.boot.autoconfigure.security.saml2.Saml2RelyingPartyAutoConfiguration,\

org.springframework.boot.autoconfigure.sendgrid.SendGridAutoConfiguration,\

org.springframework.boot.autoconfigure.session.SessionAutoConfiguration,\

org.springframework.boot.autoconfigure.security.oauth3.client.servlet.OAuth3ClientAutoConfiguration,\

org.springframework.boot.autoconfigure.security.oauth3.client.reactive.ReactiveOAuth3ClientAutoConfiguration,\

org.springframework.boot.autoconfigure.security.oauth3.resource.servlet.OAuth3ResourceServerAutoConfiguration,\

org.springframework.boot.autoconfigure.security.oauth3.resource.reactive.ReactiveOAuth3ResourceServerAutoConfiguration,\

org.springframework.boot.autoconfigure.solr.SolrAutoConfiguration,\

org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration,\

org.springframework.boot.autoconfigure.task.TaskSchedulingAutoConfiguration,\

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.client.RestTemplateAutoConfiguration,\

org.springframework.boot.autoconfigure.web.embedded.EmbeddedWebServerFactoryCustomizerAutoConfiguration,\

org.springframework.boot.autoconfigure.web.reactive.HttpHandlerAutoConfiguration,\

org.springframework.boot.autoconfigure.web.reactive.ReactiveWebServerFactoryAutoConfiguration,\

org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration,\

org.springframework.boot.autoconfigure.web.reactive.error.ErrorWebFluxAutoConfiguration,\

org.springframework.boot.autoconfigure.web.reactive.function.client.ClientHttpConnectorAutoConfiguration,\

org.springframework.boot.autoconfigure.web.reactive.function.client.WebClientAutoConfiguration,\

org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration,\

org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration,\

org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration,\

org.springframework.boot.autoconfigure.web.servlet.HttpEncodingAutoConfiguration,\

org.springframework.boot.autoconfigure.web.servlet.MultipartAutoConfiguration,\

org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration,\

org.springframework.boot.autoconfigure.websocket.reactive.WebSocketReactiveAutoConfiguration,\

org.springframework.boot.autoconfigure.websocket.servlet.WebSocketServletAutoConfiguration,\

org.springframework.boot.autoconfigure.websocket.servlet.WebSocketMessagingAutoConfiguration,\

org.springframework.boot.autoconfigure.webservices.WebServicesAutoConfiguration,\

org.springframework.boot.autoconfigure.webservices.client.WebServiceTemplateAutoConfiguration

#

Failure

analyzers

org.springframework.boot.diagnostics.FailureAnalyzer=\

org.springframework.boot.autoconfigure.data.redis.RedisUrlSyntaxFailureAnalyzer,\

org.springframework.boot.autoconfigure.diagnostics.analyzer.NoSuchBeanDefinitionFailureAnalyzer,\

org.springframework.boot.autoconfigure.flyway.FlywayMigrationScriptMissingFailureAnalyzer,\

org.springframework.boot.autoconfigure.jdbc.DataSourceBeanCreationFailureAnalyzer,\

org.springframework.boot.autoconfigure.jdbc.HikariDriverConfigurationFailureAnalyzer,\

org.springframework.boot.autoconfigure.r2dbc.ConnectionFactoryBeanCreationFailureAnalyzer,\

org.springframework.boot.autoconfigure.session.NonUniqueSessionRepositoryFailureAnalyzer

#

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.servlet.JspTemplateAvailabilityProvider每一個這樣的xxxAutoConfiguration類都是容器中的一個組件,都加入到容器中;用他們來做自動配置;每一個自動配置類進行自動配置功能;以HttpEncodingAutoConfiguration(Http編碼自動配置)為例解釋自動配置原理;@Configuration

//表示這是一個配置類,以前編寫的配置文件一樣,也可以給容器中添加組件

@EnableConfigurationProperties(ServerProperties.class)

//啟動指定類的ConfigurationProperties功能;將配置文件中對應(yīng)的值和

ServerProperties

綁定起來;并把

ServerProperties

加入到ioc容器中

@ConditionalOnWebApplication

//Spring底層@Conditional注解(Spring注解版),根據(jù)不同的條件,如果滿足指定的條件,整個配置類里面的配置就會生效;

判斷當前應(yīng)用是否是web應(yīng)用,如果是,當前配置類生效

@ConditionalOnClass(CharacterEncodingFilter.class)

//判斷當前項目有沒有這個類CharacterEncodingFilter;SpringMVC中進行亂碼解決的過濾器;

@ConditionalOnProperty(prefix

=

"spring.http.encoding",

value

=

"enabled",

matchIfMissing

=

true)

//判斷配置文件中是否存在某個配置

spring.http.encoding.enabled;如果不存在,判斷也是成立的

//即使我們配置文件中不配置pring.http.encoding.enabled=true,也是默認生效的;

public

class

HttpEncodingAutoConfiguration

{

//他已經(jīng)和SpringBoot的配置文件映射了

private

final

HttpEncodingProperties

properties;

//只有一個有參構(gòu)造器的情況下,參數(shù)的值就會從容器中拿

public

HttpEncodingAutoConfiguration(HttpEncodingProperties

properties)

{

perties

=

properties;

}

@Bean

//給容器中添加一個組件,這個組件的某些值需要從properties中獲取

@ConditionalOnMissingBean(CharacterEncodingFilter.class)

//判斷容器沒有這個組件?

public

CharacterEncodingFilter

characterEncodingFilter()

{

CharacterEncodingFilter

filter

=

new

OrderedCharacterEncodingFilter();

filter.setEncoding(perties.getCharset().name());

filter.setForceRequestEncoding(perties.shouldForce(Type.REQUEST));

filter.setForceResponseEncoding(perties.shouldForce(Type.RESPONSE));

return

filter;

}根據(jù)當前不同的條件判斷,決定這個配置類是否生效?一但這個配置類生效;這個配置類就會給容器中添加各種組件;這些組件的屬性是從對應(yīng)的properties類中獲取的,這些類里面的每一個屬性又是和配置文件綁定的;5)、所有在配置文件中能配置的屬性都是在xxxxProperties類中封裝者‘;配置文件能配置什么就可以參照某個功能對應(yīng)的這個屬性類@ConfigurationProperties(prefix

=

"spring.http.encoding")

//從配置文件中獲取指定的值和bean的屬性進行綁定

public

class

HttpEncodingProperties

{

public

static

final

Charset

DEFAULT_CHARSET

=

Charset.forName("UTF-8");精髓:1)、SpringBoot啟動會加載大量的自動配置類

2)、我們看我們需要的功能有沒有SpringBoot默認寫好的自動配置類;

3)、我們再來看這個自動配置類中到底配置了哪些組件;(只要我們要用的組件有,我們就不需要再來配置了)

4)、給容器中自動配置類添加組件的時候,會從properties類中獲取某些屬性。我們就可以在配置文件中指定這些屬性的值;xxxxAutoConfigurartion:自動配置類;給容器中添加組件xxxxProperties:封裝配置文件中相關(guān)屬性;細節(jié)@Conditional派生注解(Spring注解版原生的@Conditional作用)作用:必須是@Conditional指定的條件成立,才給容器中添加組件,配置配里面的所有內(nèi)容才生效;@Conditional擴展注解作

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論