全面解析SpringBoot自动配置的实现原理

 更新时间:2017年05月21日 19:39:52   投稿:mrr  
这篇文章主要介绍了全面解析SpringBoot自动配置的实现原理的相关资料,需要的朋友可以参考下

之前一直在用SpringBoot框架,一直感觉SpringBoot框架自动配置的功能很强大,但是并没有明白它是怎么实现自动配置的,现在有空研究了一下,大概明白了SpringBoot框架是怎么实现自动配置的功能,我们编写一个最简单的自动配置功能,大概的总结一下.

一,配置属性类

其实就是值对象注入的方式去配置一些Spring常用的配置,我们编写一个最简单的配置对象。

@ConfigurationProperties(prefix = "hello")
//@Component //如果这里添加了注解那么在自动配置类的时候就不用添加@enableConfigurationProperties(HelloProperties.class)注解.
public class HelloProperties {
  private String msg="default";//现在我们在配置文件写hello.msg=world,因为简单就不再展示;如果那么默认为default.
  public String getMsg() {
    return msg;
  }
  public void setMsg(String msg) {
    this.msg = msg;
  }
}

这是一个简单的属性值对象,那么相当于写死的字段就是SpringBoot为我们自动配置的配置,那么我们很多时候可以自己在application.properties中修改某些配置就是这样的道理,我们不设置就是默认的,设置了就是我们设置的属性。

二,自动配置类

上面已经构建了我们简单的属性对象,那么现在我们要通过属性对象得到相应的属性值将其注入到我们的Bean中,这些Bean也就是一些SpringBoot启动后为我们自动配置生成的Bean,当然SpringBoot优先使用我们配置的Bean这个功能是如何实现的,我们往下看一下就明白了。

首先我们需要一个功能Bean,可以把这个Bean看做是SpringBoot框架启动后在容器里面生成的为我们服务的内置Bean,简单的写一个。

//@Component  这里很重要,如果我们添加了这个注解那么,按照我们下面的设置SpringBoot会优先使用我们配置的这个Bean,这是符合SpringBoot框架优先使用自定义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;
  }
}

现在编写我们的自动配置类。

@Configuration //配置类
@EnableConfigurationProperties(HelloProperties.class)//这里就是前面说的,这个注解读入我们的配置对象类
@ConditionalOnClass(HelloService.class)//当类路径存在这个类时才会加载这个配置类,否则跳过,这个很有用比如不同jar包间类依赖,依赖的类不存在直接跳过,不会报错
public class HelloAutoConfiguration {
  @Autowired
  private HelloProperties helloProperties;
  @Bean
  @ConditionalOnMissingBean(HelloService.class)//这个配置就是SpringBoot可以优先使用自定义Bean的核心所在,如果没有我们的自定义Bean那么才会自动配置一个新的Bean
  public HelloService auto(){
    HelloService helloService =new HelloService();
    helloService.setMsg(helloProperties.getMsg());
    return helloService;
  }
}

好了现在自动配置的类也写好了,我们可以启动一下SpringBoot应用,测试一下。

三,测试自动配置

@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全限定名对应值.

# 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目录下创建spring.factories下添加类似的配置即可。。

以上所述是小编给大家介绍的SpringBoot自动配置的实现原理,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

相关文章

  • SpringBoot+MyBatis-Plus+Dynamic-Datasource读写分离完整指南

    SpringBoot+MyBatis-Plus+Dynamic-Datasource读写分离完整指南

    在SpringBoot项目中,使用MyBatis-Plus和Dynamic-Datasource实现读写分离是一种常见且高效的架构选择,下面就来详细介绍一下SpringBoot读写分离,感兴趣的可以了解一下
    2025-11-11
  • SpringBoot 过滤器与拦截器实例演示

    SpringBoot 过滤器与拦截器实例演示

    本文通过示例代码给大家讲解SpringBoot 过滤器与拦截器的相关知识,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
    2021-11-11
  • java编写的文件管理器代码分享

    java编写的文件管理器代码分享

    本文给大家分享的是一则使用java编写的文件管理器的代码,新人练手的作品,逻辑上还是有点小问题,大家帮忙看看吧。
    2015-04-04
  • 解决SecureRandom.getInstanceStrong()引发的线程阻塞问题

    解决SecureRandom.getInstanceStrong()引发的线程阻塞问题

    这篇文章主要介绍了解决SecureRandom.getInstanceStrong()引发的线程阻塞问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-12-12
  • springboot2学习世界著名程序springboot开发体验

    springboot2学习世界著名程序springboot开发体验

    这篇文章主要为大家介绍了世界著名程序springboot开发体验,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-05-05
  • Dubbo配置http协议全过程

    Dubbo配置http协议全过程

    文章介绍了在服务提供者和服务消费者中,分别通过添加依赖包和配置文件,实现基于HTTP协议的服务接口注册及调用,无需修改消费者启动类,服务调用方式与Dubbo协议类似
    2025-10-10
  • SpringBoot中Bean注入冲突的四种解决方案

    SpringBoot中Bean注入冲突的四种解决方案

    在Spring Boot应用开发中,依赖注入是最常用的功能之一,它极大地简化了对象之间的依赖关系管理,然而,当Spring容器中存在多个类型相同的Bean时,就会产生注入冲突问题,本文将介绍Spring Boot中的四种Bean注入冲突解决方案,需要的朋友可以参考下
    2025-06-06
  • Spring Boot 整合 Druid 并开启监控的操作方法

    Spring Boot 整合 Druid 并开启监控的操作方法

    本文介绍了如何在SpringBoot项目中引入和配置Druid数据库连接池,并开启其监控功能,通过添加依赖、配置数据源、开启监控、自定义配置以及访问监控页面,开发者可以有效提高数据库访问效率并监控连接池状态,感兴趣的朋友跟随小编一起看看吧
    2025-01-01
  • Java 如何获取某年的第一天和最后一天

    Java 如何获取某年的第一天和最后一天

    在统计的数据是时候,要统计某年的数据,开始时间是某年的第一天,结束时间是某年的最后一天,该如何获取某年的第一天和最后一天,今天通过本文介绍下Java获取某年的第一天和最后一天,需要的朋友可以参考下
    2023-07-07
  • spring-boot-starter-thymeleaf加载外部html文件方式

    spring-boot-starter-thymeleaf加载外部html文件方式

    本文介绍了在SpringMVC中使用Thymeleaf模板引擎加载外部HTML文件的方法,以及在Spring Boot中使用Thymeleaf的基本步骤,包括引入依赖、创建Controller、创建HTML文件、参数化访问、热加载和热更新文件
    2025-02-02

最新评论