当前位置: 移动技术网 > IT编程>开发语言>Java > 全面解析SpringBoot自动配置的实现原理

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

2019年07月22日  | 移动技术网IT编程  | 我要评论

之前一直在用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自动配置的实现原理,希望对大家有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网