一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - 全面解析SpringBoot自動(dòng)配置的實(shí)現(xiàn)原理

全面解析SpringBoot自動(dòng)配置的實(shí)現(xiàn)原理

2020-10-22 17:24mrr Java教程

這篇文章主要介紹了全面解析SpringBoot自動(dòng)配置的實(shí)現(xiàn)原理的相關(guān)資料,需要的朋友可以參考下

之前一直在用SpringBoot框架,一直感覺SpringBoot框架自動(dòng)配置的功能很強(qiáng)大,但是并沒有明白它是怎么實(shí)現(xiàn)自動(dòng)配置的,現(xiàn)在有空研究了一下,大概明白了SpringBoot框架是怎么實(shí)現(xiàn)自動(dòng)配置的功能,我們編寫一個(gè)最簡(jiǎn)單的自動(dòng)配置功能,大概的總結(jié)一下.

一,配置屬性類

其實(shí)就是值對(duì)象注入的方式去配置一些Spring常用的配置,我們編寫一個(gè)最簡(jiǎn)單的配置對(duì)象。

?
1
2
3
4
5
6
7
8
9
10
11
@ConfigurationProperties(prefix = "hello")
//@Component //如果這里添加了注解那么在自動(dòng)配置類的時(shí)候就不用添加@enableConfigurationProperties(HelloProperties.class)注解.
public class HelloProperties {
  private String msg="default";//現(xiàn)在我們?cè)谂渲梦募慼ello.msg=world,因?yàn)楹?jiǎn)單就不再展示;如果那么默認(rèn)為default.
  public String getMsg() {
    return msg;
  }
  public void setMsg(String msg) {
    this.msg = msg;
  }
}

這是一個(gè)簡(jiǎn)單的屬性值對(duì)象,那么相當(dāng)于寫死的字段就是SpringBoot為我們自動(dòng)配置的配置,那么我們很多時(shí)候可以自己在application.properties中修改某些配置就是這樣的道理,我們不設(shè)置就是默認(rèn)的,設(shè)置了就是我們?cè)O(shè)置的屬性。

二,自動(dòng)配置類

上面已經(jīng)構(gòu)建了我們簡(jiǎn)單的屬性對(duì)象,那么現(xiàn)在我們要通過屬性對(duì)象得到相應(yīng)的屬性值將其注入到我們的Bean中,這些Bean也就是一些SpringBoot啟動(dòng)后為我們自動(dòng)配置生成的Bean,當(dāng)然SpringBoot優(yōu)先使用我們配置的Bean這個(gè)功能是如何實(shí)現(xiàn)的,我們往下看一下就明白了。

首先我們需要一個(gè)功能Bean,可以把這個(gè)Bean看做是SpringBoot框架啟動(dòng)后在容器里面生成的為我們服務(wù)的內(nèi)置Bean,簡(jiǎn)單的寫一個(gè)。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
//@Component  這里很重要,如果我們添加了這個(gè)注解那么,按照我們下面的設(shè)置SpringBoot會(huì)優(yōu)先使用我們配置的這個(gè)Bean,這是符合SpringBoot框架優(yōu)先使用自定義Bean的原則的。
public class HelloService {
  private String msg = "service";//如果自動(dòng)配置沒有讀入成功,那么為默認(rèn)值
  public String say() {
    return "hello " + msg;
  }//為我們服務(wù)的方法
  public String getMsg() {
    return msg;
  }
  public void setMsg(String msg) {
    this.msg = msg;
  }
}

現(xiàn)在編寫我們的自動(dòng)配置類。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Configuration //配置類
@EnableConfigurationProperties(HelloProperties.class)//這里就是前面說的,這個(gè)注解讀入我們的配置對(duì)象類
@ConditionalOnClass(HelloService.class)//當(dāng)類路徑存在這個(gè)類時(shí)才會(huì)加載這個(gè)配置類,否則跳過,這個(gè)很有用比如不同jar包間類依賴,依賴的類不存在直接跳過,不會(huì)報(bào)錯(cuò)
public class HelloAutoConfiguration {
  @Autowired
  private HelloProperties helloProperties;
  @Bean
  @ConditionalOnMissingBean(HelloService.class)//這個(gè)配置就是SpringBoot可以優(yōu)先使用自定義Bean的核心所在,如果沒有我們的自定義Bean那么才會(huì)自動(dòng)配置一個(gè)新的Bean
  public HelloService auto(){
    HelloService helloService =new HelloService();
    helloService.setMsg(helloProperties.getMsg());
    return helloService;
  }
}

好了現(xiàn)在自動(dòng)配置的類也寫好了,我們可以啟動(dòng)一下SpringBoot應(yīng)用,測(cè)試一下。

三,測(cè)試自動(dòng)配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
@SpringBootApplication
@RestController
public class MyRun {
  @Autowired
  private HelloService helloService;
  @RequestMapping("/auto/home")
  public String home(){
    return helloService.say();
  }
  public static void main(String[] args) {
    SpringApplication.run(MyRun.class,args);
  }
}

ok ,運(yùn)行后訪問你會(huì)看到:

hello world

代表我們的自動(dòng)配置功能成功。

四,SpringBoot管理自動(dòng)配置

其實(shí)在很多時(shí)候我們的配置是在很多jar包里的,那么我們新的應(yīng)用該怎么讀入這些jar包里的配置文件呢,SpringBoot是這樣管理的。

最主要的注解就是@enableAutoConfiguration,而這個(gè)注解會(huì)導(dǎo)入一個(gè)EnableAutoConfigurationImportSelector的類,而這個(gè)類會(huì)去讀取一個(gè)spring.factories下key為EnableAutoConfiguration全限定名對(duì)應(yīng)值.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# 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.MessageSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration,\
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.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.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.jms.hornetq.HornetQAutoConfiguration,\
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.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.velocity.VelocityAutoConfiguration,\
org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration,\
org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration,\
org.springframework.boot.autoconfigure.transaction.jta.JtaAutoConfiguration,\
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

所以如果需要我們可以在我們的resources目錄下創(chuàng)建spring.factories下添加類似的配置即可。。

以上所述是小編給大家介紹的SpringBoot自動(dòng)配置的實(shí)現(xiàn)原理,希望對(duì)大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!

 

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 好大好硬抽搐好爽想要 | 91午夜视频 | 91你懂的| 日韩在线视频免费不卡一区 | 嫩草在线视频www免费观看 | 国产高清ujzzujzz | hd在线观看免费高清视频 | 美妇在男人胯下哀求 | 日本高清在线播放 | 青草青草久热精品视频在线网站 | 亚洲国产天堂 | 97久久精品午夜一区二区 | sao虎在线精品永久在线 | 成人精品网 | 成全动漫视频在线观看 | 国产福利不卡 | 国产9191精品免费观看 | 国产成人青草视频 | 2020中文字幕 | babes性欧美30 | 特黄未满14周岁毛片 | 蜜桃破解版免费看nba | 海绵宝宝第二季全集免费观看 | 91精品国产亚洲爽啪在线影院 | 国产青草视频在线观看免费影院 | 国产麻豆传媒在线观看 | 我和么公的秘密小说免费 | 91亚洲精品丁香在线观看 | 国内精品自产拍在线观看91 | 免费看黄色片的网站 | 久久这里只精品热在线18 | 国产精品一级片 | 日本特级a禁片在线播放 | 免费一级特黄特色大片在线 | 色菇凉天天综合网 | 国产亚洲精品自在线亚洲情侣 | 成人啪精品视频免费网站 | 欧美特级午夜一区二区三区 | 久久久久久久电影 | 大杳蕉在线影院在线播放 | 国产1区二区 |