Spring Boot自动注入的原理分析
前言
我们经常会被问到这么一个问题:SpringBoot相对于spring有哪些优势呢?其中有一条答案就是SpringBoot自动注入。那么自动注入的原理是什么呢?
我们进行如下分析。
1:首先我们分析项目的启动类时,发现都会加上@SpringBootApplication这个注解,我们分析这个继续进入这个注解会发现,它是由多个注解组成的,如下
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @SpringBootConfiguration @EnableAutoConfiguration @ComponentScan(excludeFilters={ @Filter(type=FilterType.CUSTOM,classes=TypeExcludeFilter.class), @Filter(type=FilterType.CUSTOM,classes=AutoConfigurationExcludeFilter.class)}) public@interfaceSpringBootApplication{
2:服务启动会扫描org.springframework.boot.autoconfigure下的META-INF/spring.factories这个文件,这个文件中保存着springboot启动时默认会自动注入的类,部分如下
#AutoConfigure 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.cloud.CloudAutoConfiguration,\ org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration,\ 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.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.ldap.LdapDataAutoConfiguration,\ org.springframework.boot.autoconfigure.data.ldap.LdapRepositoriesAutoConfiguration,\ 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.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.kafka.KafkaAutoConfiguration,\ 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.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.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.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
3:你是不是在其中发现了自己常用的redis,mysql等相关的类?没错,springboot会尝试加载这些类,我们以org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration这个类为例,进去看一下它的源码,部分示例如下
@Configuration @ConditionalOnClass({JedisConnection.class,RedisOperations.class,Jedis.class}) @EnableConfigurationProperties(RedisProperties.class) publicclassRedisAutoConfiguration{ /** *Redisconnectionconfiguration. */ @Configuration @ConditionalOnClass(GenericObjectPool.class) protectedstaticclassRedisConnectionConfiguration{ privatefinalRedisPropertiesproperties; privatefinalRedisSentinelConfigurationsentinelConfiguration; privatefinalRedisClusterConfigurationclusterConfiguration; publicRedisConnectionConfiguration(RedisPropertiesproperties, ObjectProvidersentinelConfiguration, ObjectProvider clusterConfiguration){ this.properties=properties; this.sentinelConfiguration=sentinelConfiguration.getIfAvailable(); this.clusterConfiguration=clusterConfiguration.getIfAvailable(); } @Bean @ConditionalOnMissingBean(RedisConnectionFactory.class) publicJedisConnectionFactoryredisConnectionFactory() throwsUnknownHostException{ returnapplyProperties(createJedisConnectionFactory()); }
我们能看到这个类上加了这个注解@ConditionalOnClass({JedisConnection.class,RedisOperations.class,Jedis.class})意思就是如果你的classpath中没有这些类的话,那么这个类就不能被加载,那么这些被依赖的类在哪出现呢?没错,就在我们在pom.xml中引入的依赖所对应的包里。
看到这里你因该就明白了,META-INF/spring.factories文件中被列出来的那些类都会被springboot去尝试加载,但是有些模块我们没引入相关的依赖,那么这个类就会加载失败。即这个模块没有被成功加载。
4:我们通过上面的redis的自动加载类时,看到上面还有个@EnableConfigurationProperties(RedisProperties.class)注解,这个注解来注入关于redis的配置信息,这个信息都在RedisProperties.class中保存,我们看下RedisProperties的源码
@ConfigurationProperties(prefix="spring.redis") publicclassRedisProperties{ /** *Databaseindexusedbytheconnectionfactory. */ privateintdatabase=0; /** *Redisurl,whichwilloverrulehost,portandpasswordifset. */ privateStringurl; /** *Redisserverhost. */ privateStringhost="localhost"; /** *Loginpasswordoftheredisserver. */ privateStringpassword; /** *Redisserverport. */ privateintport=6379; /** *EnableSSL. */ privatebooleanssl; /** *Connectiontimeoutinmilliseconds. */ privateinttimeout; privatePoolpool; privateSentinelsentinel; privateClustercluster; publicintgetDatabase(){ returnthis.database; } publicvoidsetDatabase(intdatabase){ this.database=database; } publicStringgetUrl(){ returnthis.url; } publicvoidsetUrl(Stringurl){ this.url=url; } publicStringgetHost(){ returnthis.host; } publicvoidsetHost(Stringhost){ this.host=host; } publicStringgetPassword(){ returnthis.password; } publicvoidsetPassword(Stringpassword){ this.password=password; } publicintgetPort(){ returnthis.port; } publicvoidsetPort(intport){ this.port=port; } publicbooleanisSsl(){ returnthis.ssl; } publicvoidsetSsl(booleanssl){ this.ssl=ssl; } publicvoidsetTimeout(inttimeout){ this.timeout=timeout; } publicintgetTimeout(){ returnthis.timeout; } publicSentinelgetSentinel(){ returnthis.sentinel; } publicvoidsetSentinel(Sentinelsentinel){ this.sentinel=sentinel; } publicPoolgetPool(){ returnthis.pool; } publicvoidsetPool(Poolpool){ this.pool=pool; } publicClustergetCluster(){ returnthis.cluster; } publicvoidsetCluster(Clustercluster){ this.cluster=cluster; } /** *Poolproperties. */ publicstaticclassPool{ /** *Maxnumberof"idle"connectionsinthepool.Useanegativevaluetoindicate *anunlimitednumberofidleconnections. */ privateintmaxIdle=8; /** *Targetfortheminimumnumberofidleconnectionstomaintaininthepool.This *settingonlyhasaneffectifitispositive. */ privateintminIdle=0; /** *Maxnumberofconnectionsthatcanbeallocatedbythepoolatagiventime. *Useanegativevaluefornolimit. */ privateintmaxActive=8; /** *Maximumamountoftime(inmilliseconds)aconnectionallocationshouldblock *beforethrowinganexceptionwhenthepoolisexhausted.Useanegativevalue *toblockindefinitely. */ privateintmaxWait=-1; publicintgetMaxIdle(){ returnthis.maxIdle; } publicvoidsetMaxIdle(intmaxIdle){ this.maxIdle=maxIdle; } publicintgetMinIdle(){ returnthis.minIdle; } publicvoidsetMinIdle(intminIdle){ this.minIdle=minIdle; } publicintgetMaxActive(){ returnthis.maxActive; } publicvoidsetMaxActive(intmaxActive){ this.maxActive=maxActive; } publicintgetMaxWait(){ returnthis.maxWait; } publicvoidsetMaxWait(intmaxWait){ this.maxWait=maxWait; } } /** *Clusterproperties. */ publicstaticclassCluster{ /** *Comma-separatedlistof"host:port"pairstobootstrapfrom.Thisrepresentsan *"initial"listofclusternodesandisrequiredtohaveatleastoneentry. */ privateListnodes; /** *Maximumnumberofredirectstofollowwhenexecutingcommandsacrossthe *cluster. */ privateIntegermaxRedirects; publicList getNodes(){ returnthis.nodes; } publicvoidsetNodes(List nodes){ this.nodes=nodes; } publicIntegergetMaxRedirects(){ returnthis.maxRedirects; } publicvoidsetMaxRedirects(IntegermaxRedirects){ this.maxRedirects=maxRedirects; } } /** *Redissentinelproperties. */ publicstaticclassSentinel{ /** *NameofRedisserver. */ privateStringmaster; /** *Comma-separatedlistofhost:portpairs. */ privateStringnodes; publicStringgetMaster(){ returnthis.master; } publicvoidsetMaster(Stringmaster){ this.master=master; } publicStringgetNodes(){ returnthis.nodes; } publicvoidsetNodes(Stringnodes){ this.nodes=nodes; } } }
发现里面的配置项基本都是有默认值的,通过上面的注解可以明白,如果配置文件中存在spring.redis开头的配置项,则使用配置文件中的,如果没有的话则使用文件中默认写死的配置。你是不是想到了springboot的另外一个优势:约定大于配置。
这里我们大概了解了SpringBoot自动配置的原理和流程,里面的那些细节我们不在分析。同理,对于那些第三方或者我们自己写的starter,springboot启动的时候会扫描starter的jar包下META-INF/spring.factories这个文件,解析内容并加载里面对用的类。
由此,我们总结出以下几点
1:SpringBoot的自动配置是如何实现的
2:有关的那些注解,如@EnableAutoConfiguration,@ConditionalOnClass,@Configuration等也是SpringBoot的核心注解,也值得我们了解其用法和原理。
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对毛票票的支持。