国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

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

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

全面解析SpringBoot自動配置的實現(xiàn)原理

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

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

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

一,配置屬性類

其實就是值對象注入的方式去配置一些Spring常用的配置,我們編寫一個最簡單的配置對象。

?
1
2
3
4
5
6
7
8
9
10
11
@ConfigurationProperties(prefix = "hello")
//@Component //如果這里添加了注解那么在自動配置類的時候就不用添加@enableConfigurationProperties(HelloProperties.class)注解.
public class HelloProperties {
  private String msg="default";//現(xiàn)在我們在配置文件寫hello.msg=world,因為簡單就不再展示;如果那么默認為default.
  public String getMsg() {
    return msg;
  }
  public void setMsg(String msg) {
    this.msg = msg;
  }
}

這是一個簡單的屬性值對象,那么相當于寫死的字段就是SpringBoot為我們自動配置的配置,那么我們很多時候可以自己在application.properties中修改某些配置就是這樣的道理,我們不設置就是默認的,設置了就是我們設置的屬性。

二,自動配置類

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

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

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

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

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

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

三,測試自動配置

?
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 ,運行后訪問你會看到:

hello world

代表我們的自動配置功能成功。

四,SpringBoot管理自動配置

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

最主要的注解就是@enableAutoConfiguration,而這個注解會導入一個EnableAutoConfigurationImportSelector的類,而這個類會去讀取一個spring.factories下key為EnableAutoConfiguration全限定名對應值.

?
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自動配置的實現(xiàn)原理,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復大家的!

 

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲精品成人 | 欧洲成人午夜免费大片 | 国产精品亲子伦av一区二区三区 | 国产精品国产三级国产aⅴ9色 | 一本大道综合伊人精品热热 | 日韩欧美国产精品 | 国产精品久久久久久久久 | 激情总合网 | 最近日本韩国高清免费观看 | 久久精品久久综合 | 一区二区三区四区在线 | 亚洲一区二区国产 | 亚洲综合精品 | 久久久精品呻吟 | 可以免费看黄的网站 | 毛片免费观看 | 日韩免费在线视频 | 午夜三区| 九色网址 | 色天天综合 | 噜噜噜在线观看免费视频日本 | 免费观看一级视频 | 亚洲精品久久久一区二区三区 | 成人国产精品久久久 | 中文字幕成人 | 久久成人免费视频 | 日韩在线一区二区三区 | 免费观看欧美一级大片 | 色综合天天天天做夜夜夜夜做 | 国产精品成人3p一区二区三区 | 色偷偷噜噜噜亚洲男人的天堂 | 99久色| 黄色片网站在线看 | 福利视频在线播放 | 国产精品jizz在线观看麻豆 | 久久综合九色综合网站 | 81精品国产乱码久久久久久 | 午夜亚洲一区 | 欧美精品成人一区二区三区四区 | 久久综合伊人 | 欧美日韩三级在线 |