SpringBoot原理浅析
starter-* POM依赖
使用SpringBoot开发时,在pom.xml文件中引入的依赖一般都是形如spring-boot-starter-*
。starter依赖是居于某个场景或者功能的,我们引入一个starter依赖之后,它会间接引入实现这个场景或功能所需的其他依赖。我们可以把这些starters称为场景启动器,只需要在项目里面引入这些starter相关场景的所有依赖都会导入进来。要用什么功能就导入什么场景的启动器。这里以spring-boot-starter-web
为例分析。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId></dependency>
spring-boot-starter-web的间接依赖
我们来看下spring-boot-starter-web
的pom文件,它定义了一个父类spring-boot-starters
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starters</artifactId> <version>1.5.4.RELEASE</version> </parent> <artifactId>spring-boot-starter-web</artifactId> <name>Spring Boot Web Starter</name> <url>http://projects.spring.io/spring-boot/</url> <organization> <name>Pivotal Software, Inc.</name> <url>http://www.spring.io</url> </organization> <properties> <main.basedir>${basedir}/../..</main.basedir> </properties>
spring-boot-starters
的打包类型为pom,它定义好了SpringBoot中所有的starter,同时它的父类为spring-boot-parent
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-parent</artifactId> <version>1.5.4.RELEASE</version> <relativePath>../spring-boot-parent</relativePath> </parent> <artifactId>spring-boot-starters</artifactId> <packaging>pom</packaging> <name>Spring Boot Starters</name> <description>Spring Boot Starters</description> <url>http://projects.spring.io/spring-boot/</url> <organization> <name>Pivotal Software, Inc.</name> <url>http://www.spring.io</url> </organization> <properties> <main.basedir>${basedir}/..</main.basedir> </properties> <modules> <module>spring-boot-starter</module> <module>spring-boot-starter-activemq</module> <module>spring-boot-starter-amqp</module> <module>spring-boot-starter-aop</module> <module>spring-boot-starter-artemis</module> <module>spring-boot-starter-batch</module> <module>spring-boot-starter-cache</module> <module>spring-boot-starter-cloud-connectors</module> <module>spring-boot-starter-data-cassandra</module> <module>spring-boot-starter-data-couchbase</module> <module>spring-boot-starter-data-elasticsearch</module> <module>spring-boot-starter-data-gemfire</module> <module>spring-boot-starter-data-jpa</module> <module>spring-boot-starter-data-ldap</module> <module>spring-boot-starter-data-mongodb</module> <module>spring-boot-starter-data-neo4j</module> <module>spring-boot-starter-data-redis</module> <module>spring-boot-starter-data-rest</module> <module>spring-boot-starter-data-solr</module> <module>spring-boot-starter-freemarker</module> <module>spring-boot-starter-groovy-templates</module> <module>spring-boot-starter-hateoas</module> <module>spring-boot-starter-integration</module> <module>spring-boot-starter-jdbc</module> <module>spring-boot-starter-jersey</module> <module>spring-boot-starter-jetty</module> <module>spring-boot-starter-jooq</module> <module>spring-boot-starter-jta-atomikos</module> <module>spring-boot-starter-jta-bitronix</module> <module>spring-boot-starter-jta-narayana</module> <module>spring-boot-starter-logging</module> <module>spring-boot-starter-log4j2</module> <module>spring-boot-starter-mail</module> <module>spring-boot-starter-mobile</module> <module>spring-boot-starter-mustache</module> <module>spring-boot-starter-actuator</module> <module>spring-boot-starter-parent</module> <module>spring-boot-starter-security</module> <module>spring-boot-starter-social-facebook</module> <module>spring-boot-starter-social-twitter</module> <module>spring-boot-starter-social-linkedin</module> <module>spring-boot-starter-remote-shell</module> <module>spring-boot-starter-test</module> <module>spring-boot-starter-thymeleaf</module> <module>spring-boot-starter-tomcat</module> <module>spring-boot-starter-undertow</module> <module>spring-boot-starter-validation</module> <module>spring-boot-starter-web</module> <module>spring-boot-starter-websocket</module> <module>spring-boot-starter-web-services</module> </modules>
spring-boot-parent
的父pom为spring-boot-dependencies
,该pom文件中定义了我们所需要具体jar包的version。
<properties> <!-- Dependency versions --> <activemq.version>5.14.5</activemq.version> <antlr2.version>2.7.7</antlr2.version> <appengine-sdk.version>1.9.53</appengine-sdk.version> <artemis.version>1.5.5</artemis.version> <aspectj.version>1.8.10</aspectj.version> <assertj.version>2.6.0</assertj.version> <atomikos.version>3.9.3</atomikos.version> <bitronix.version>2.1.4</bitronix.version> <caffeine.version>2.3.5</caffeine.version> <cassandra-driver.version>3.1.4</cassandra-driver.version> <classmate.version>1.3.3</classmate.version> <commons-beanutils.version>1.9.3</commons-beanutils.version> <commons-collections.version>3.2.2</commons-collections.version> <commons-codec.version>1.10</commons-codec.version> <commons-dbcp.version>1.4</commons-dbcp.version> <commons-dbcp2.version>2.1.1</commons-dbcp2.version> <commons-digester.version>2.1</commons-digester.version> <commons-pool.version>1.6</commons-pool.version> <commons-pool2.version>2.4.2</commons-pool2.version> <couchbase-client.version>2.3.7</couchbase-client.version> <couchbase-cache-client.version>2.1.0</couchbase-cache-client.version> <crashub.version>1.3.2</crashub.version> <derby.version>10.13.1.1</derby.version> <dom4j.version>1.6.1</dom4j.version> <dropwizard-metrics.version>3.1.4</dropwizard-metrics.version> <ehcache.version>2.10.4</ehcache.version> <ehcache3.version>3.2.2</ehcache3.version> <embedded-mongo.version>1.50.5</embedded-mongo.version> <flyway.version>3.2.1</flyway.version> <freemarker.version>2.3.26-incubating</freemarker.version> <elasticsearch.version>2.4.5</elasticsearch.version> <gemfire.version>8.2.4</gemfire.version> <glassfish-el.version>3.0.0</glassfish-el.version> <gradle.version>2.9</gradle.version> <groovy.version>2.4.11</groovy.version> <gson.version>2.8.0</gson.version> <h2.version>1.4.195</h2.version> <hamcrest.version>1.3</hamcrest.version> <hazelcast.version>3.7.7</hazelcast.version> <hazelcast-hibernate4.version>3.7.1</hazelcast-hibernate4.version> <hazelcast-hibernate5.version>1.1.3</hazelcast-hibernate5.version> <hibernate.version>5.0.12.Final</hibernate.version> <hibernate-validator.version>5.3.5.Final</hibernate-validator.version> <hikaricp.version>2.5.1</hikaricp.version> <hikaricp-java6.version>2.3.13</hikaricp-java6.version> <hikaricp-java7.version>2.4.11</hikaricp-java7.version> <hsqldb.version>2.3.5</hsqldb.version> <htmlunit.version>2.21</htmlunit.version> <httpasyncclient.version>4.1.3</httpasyncclient.version> <httpclient.version>4.5.3</httpclient.version> <httpcore.version>4.4.6</httpcore.version> <infinispan.version>8.2.6.Final</infinispan.version> <jackson.version>2.8.8</jackson.version> <janino.version>2.7.8</janino.version> <javassist.version>3.21.0-GA</javassist.version> <!-- Same as Hibernate --> <javax-cache.version>1.0.0</javax-cache.version> <javax-mail.version>1.5.6</javax-mail.version> <javax-transaction.version>1.2</javax-transaction.version> <javax-validation.version>1.1.0.Final</javax-validation.version> <jaxen.version>1.1.6</jaxen.version> <jaybird.version>2.2.13</jaybird.version> <jboss-logging.version>3.3.1.Final</jboss-logging.version> <jboss-transaction-spi.version>7.6.0.Final</jboss-transaction-spi.version> <jdom2.version>2.0.6</jdom2.version> <jedis.version>2.9.0</jedis.version> <jersey.version>2.25.1</jersey.version> <jest.version>2.0.4</jest.version> <jetty.version>9.4.5.v20170502</jetty.version> <jetty-jsp.version>2.2.0.v201112011158</jetty-jsp.version> <jetty-el.version>8.0.33</jetty-el.version> <jms-api.version>1.1-rev-1</jms-api.version> <jmustache.version>1.13</jmustache.version> <jna.version>4.2.2</jna.version> <joda-time.version>2.9.9</joda-time.version> <jolokia.version>1.3.6</jolokia.version> <jooq.version>3.9.2</jooq.version> <json.version>20140107</json.version> <jsonassert.version>1.4.0</jsonassert.version> <json-path.version>2.2.0</json-path.version> <jstl.version>1.2</jstl.version> <jtds.version>1.3.1</jtds.version> <junit.version>4.12</junit.version> <liquibase.version>3.5.3</liquibase.version> <log4j2.version>2.7</log4j2.version> <logback.version>1.1.11</logback.version> <lombok.version>1.16.16</lombok.version> <mariadb.version>1.5.9</mariadb.version> <mssql-jdbc.version>6.1.0.jre7</mssql-jdbc.version> <mockito.version>1.10.19</mockito.version> <mongodb.version>3.4.2</mongodb.version> <mysql.version>5.1.42</mysql.version> <narayana.version>5.5.24.Final</narayana.version> <nekohtml.version>1.9.22</nekohtml.version> <neo4j-ogm.version>2.1.3</neo4j-ogm.version> <postgresql.version>9.4.1212.jre7</postgresql.version> <querydsl.version>4.1.4</querydsl.version> <reactor.version>2.0.8.RELEASE</reactor.version> <reactor-spring.version>2.0.7.RELEASE</reactor-spring.version> <selenium.version>2.53.1</selenium.version> <selenium-htmlunit.version>2.21</selenium-htmlunit.version> <sendgrid.version>2.2.2</sendgrid.version> <servlet-api.version>3.1.0</servlet-api.version> <simple-json.version>1.1.1</simple-json.version> <slf4j.version>1.7.25</slf4j.version> <snakeyaml.version>1.17</snakeyaml.version> <solr.version>5.5.4</solr.version> <spock.version>1.0-groovy-2.4</spock.version> <spring.version>4.3.9.RELEASE</spring.version> <spring-amqp.version>1.7.3.RELEASE</spring-amqp.version> <spring-cloud-connectors.version>1.2.4.RELEASE</spring-cloud-connectors.version> <spring-batch.version>3.0.7.RELEASE</spring-batch.version> <spring-data-releasetrain.version>Ingalls-SR4</spring-data-releasetrain.version> <spring-hateoas.version>0.23.0.RELEASE</spring-hateoas.version> <spring-integration.version>4.3.10.RELEASE</spring-integration.version> <spring-integration-java-dsl.version>1.2.2.RELEASE</spring-integration-java-dsl.version> <spring-kafka.version>1.1.6.RELEASE</spring-kafka.version> <spring-ldap.version>2.3.1.RELEASE</spring-ldap.version> <spring-loaded.version>1.2.7.RELEASE</spring-loaded.version> <spring-mobile.version>1.1.5.RELEASE</spring-mobile.version> <spring-plugin.version>1.2.0.RELEASE</spring-plugin.version> <spring-restdocs.version>1.1.3.RELEASE</spring-restdocs.version> <spring-retry.version>1.2.0.RELEASE</spring-retry.version> <spring-security.version>4.2.3.RELEASE</spring-security.version> <spring-security-jwt.version>1.0.8.RELEASE</spring-security-jwt.version> <spring-security-oauth.version>2.0.14.RELEASE</spring-security-oauth.version> <spring-session.version>1.3.1.RELEASE</spring-session.version> <spring-social.version>1.1.4.RELEASE</spring-social.version> <spring-social-facebook.version>2.0.3.RELEASE</spring-social-facebook.version> <spring-social-linkedin.version>1.0.2.RELEASE</spring-social-linkedin.version> <spring-social-twitter.version>1.1.2.RELEASE</spring-social-twitter.version> <spring-ws.version>2.4.0.RELEASE</spring-ws.version> <sqlite-jdbc.version>3.15.1</sqlite-jdbc.version> <statsd-client.version>3.1.0</statsd-client.version> <sun-mail.version>${javax-mail.version}</sun-mail.version> <thymeleaf.version>2.1.5.RELEASE</thymeleaf.version> <thymeleaf-extras-springsecurity4.version>2.1.3.RELEASE</thymeleaf-extras-springsecurity4.version> <thymeleaf-extras-conditionalcomments.version>2.1.2.RELEASE</thymeleaf-extras-conditionalcomments.version> <thymeleaf-layout-dialect.version>1.4.0</thymeleaf-layout-dialect.version> <thymeleaf-extras-data-attribute.version>1.3</thymeleaf-extras-data-attribute.version> <thymeleaf-extras-java8time.version>2.1.0.RELEASE</thymeleaf-extras-java8time.version> <tomcat.version>8.5.15</tomcat.version> <undertow.version>1.4.15.Final</undertow.version> <unboundid-ldapsdk.version>3.2.1</unboundid-ldapsdk.version> <webjars-hal-browser.version>9f96c74</webjars-hal-browser.version> <webjars-locator.version>0.32-1</webjars-locator.version> <wsdl4j.version>1.6.3</wsdl4j.version> <xml-apis.version>1.4.01</xml-apis.version> <!-- Plugin versions --> <build-helper-maven-plugin.version>1.10</build-helper-maven-plugin.version> <exec-maven-plugin.version>1.5.0</exec-maven-plugin.version> <git-commit-id-plugin.version>2.2.2</git-commit-id-plugin.version> <maven-antrun-plugin.version>1.8</maven-antrun-plugin.version> <maven-assembly-plugin.version>2.6</maven-assembly-plugin.version> <maven-clean-plugin.version>2.6.1</maven-clean-plugin.version> <maven-compiler-plugin.version>3.1</maven-compiler-plugin.version> <maven-dependency-plugin.version>2.10</maven-dependency-plugin.version> <maven-deploy-plugin.version>2.8.2</maven-deploy-plugin.version> <maven-eclipse-plugin.version>2.10</maven-eclipse-plugin.version> <maven-enforcer-plugin.version>1.4</maven-enforcer-plugin.version> <maven-failsafe-plugin.version>2.18.1</maven-failsafe-plugin.version> <maven-install-plugin.version>2.5.2</maven-install-plugin.version> <maven-invoker-plugin.version>1.10</maven-invoker-plugin.version> <maven-help-plugin.version>2.2</maven-help-plugin.version> <maven-jar-plugin.version>2.6</maven-jar-plugin.version> <maven-javadoc-plugin.version>2.10.4</maven-javadoc-plugin.version> <maven-resources-plugin.version>2.7</maven-resources-plugin.version> <maven-shade-plugin.version>2.4.3</maven-shade-plugin.version> <maven-site-plugin.version>3.5.1</maven-site-plugin.version> <maven-source-plugin.version>2.4</maven-source-plugin.version> <maven-surefire-plugin.version>2.18.1</maven-surefire-plugin.version> <maven-war-plugin.version>2.6</maven-war-plugin.version> <versions-maven-plugin.version>2.2</versions-maven-plugin.version> </properties>
这就解释了springboot为我们定义好了依赖包的版本,在开发过程中jar包冲突是个常见且让人头痛的问题,而使用springboot的话由于其帮我们选择好了依赖包的版本,所以很好的解决了包冲突等繁琐问题。如果spring-boot-parent
没有定义的jar包,而此时项目中需要使用的话,需要我们自己定义好版本。
程序入口
@SpringBootApplicationpublic class StartSpringBootMain { public static void main(String[] args) throws Exception { SpringApplication.run(StartSpringBootMain.class, args); } }
我们在主程序类中加入@SpringBootApplication
注解就能启用SpringBoot,下面就来看看这个注解类
@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 @interface SpringBootApplication { ...... }
其中@ComponentScan是我们熟悉的组件扫描,其作用是使@Controller、@Service、@Componet等组件生效。这里它排除了两个Filter类TypeExcludeFilter
和AutoConfigurationExcludeFilter
。
@SpringBootConfiguration
注解相当于@Configuration
,表示应用该注解的类为配置类,相当于applicationContext.xml文件。
@Configurationpublic @interface SpringBootConfiguration { }
@EnableAutoConfiguration
注解表示开启自动配置的功能,之前需要开发人员手动进行的配置,SpringBoot帮我们自动配置。它的源码如下
@AutoConfigurationPackage@Import(EnableAutoConfigurationImportSelector.class)public @interface EnableAutoConfiguration { ...... }
@AutoConfigurationPackage
自动配置包
@Import(AutoConfigurationPackages.Registrar.class) public @interface AutoConfigurationPackage { }
AutoConfigurationPackages.Registrar
的源码如下
static class Registrar implements ImportBeanDefinitionRegistrar, DeterminableImports { @Override public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) { register(registry, new PackageImport(metadata).getPackageName()); }
new PackageImport(metadata).getPackageName()
会获取主配置类(@SpringBootApplication标注的类)所在的包名,然后会将该包及下面所有子包里面的所有组件扫描到Spring容器中。@Import
注解的含义为给Spring容器中注入一个对象,@Import(EnableAutoConfigurationImportSelector.class)
表示给Spring容器中注入EnableAutoConfigurationImportSelector
对象,该对象的字面意思为“开启自动配置导入选择器”。同时它还继承自AutoConfigurationImportSelector
类
@Deprecatedpublic class EnableAutoConfigurationImportSelector extends AutoConfigurationImportSelector {
AutoConfigurationImportSelector
类的作用是将所有需要导入的组件以全类名的方式添加到容器中;其通过selectImports方法筛选出选哟导入的组件的全类名。
public String[] selectImports(AnnotationMetadata annotationMetadata) { try { AutoConfigurationMetadata autoConfigurationMetadata = AutoConfigurationMetadataLoader .loadMetadata(this.beanClassLoader); AnnotationAttributes attributes = getAttributes(annotationMetadata); //这句获取自动配置类 List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes); configurations = removeDuplicates(configurations); configurations = sort(configurations, autoConfigurationMetadata); Set<String> exclusions = getExclusions(annotationMetadata, attributes); checkExcludedClasses(configurations, exclusions); configurations.removeAll(exclusions); configurations = filter(configurations, autoConfigurationMetadata); fireAutoConfigurationImportEvents(configurations, exclusions); return configurations.toArray(new String[configurations.size()]); } catch (IOException ex) { throw new IllegalStateException(ex); } }
getCandidateConfigurations
方法用来获取自动配置类,该方法内部的实现最终会委托给SpringFactoriesLoader.loadFactoryNames(EnableAutoConfiguration.class,classLoader);
方法,loadFactoryNames方法的源码如下:
public static List<String> loadFactoryNames(Class<?> factoryClass, ClassLoader classLoader) { String factoryClassName = factoryClass.getName(); try { Enumeration<URL> urls = classLoader != null ? classLoader.getResources("META-INF/spring.factories") : ClassLoader.getSystemResources("META-INF/spring.factories"); ArrayList result = new ArrayList(); while(urls.hasMoreElements()) { URL url = (URL)urls.nextElement(); Properties properties = PropertiesLoaderUtils.loadProperties(new UrlResource(url)); String factoryClassNames = properties.getProperty(factoryClassName); result.addAll(Arrays.asList(StringUtils.commaDelimitedListToStringArray(factoryClassNames))); } return result; } catch (IOException var8) { throw new IllegalArgumentException("Unable to load [" + factoryClass.getName() + "] factories from location [" + "META-INF/spring.factories" + "]", var8); } }
可以看到其在Spring Boot启动的时候从类路径下的META-INF/spring.factories中获取EnableAutoConfiguration指定的值,将这些值作为自动配置类导入到容器中,自动配置类就生效,帮我们进行自动配置工作;
我们调式源码的时候可以看到加载的自动配置类96个之多
而这些类是定义在spring.factories文件中有定义的。
今天先分析到这里,还存在没有理清楚的地方,后续有时间的话再更新吧。
作者:Coding小聪
链接:https://www.jianshu.com/p/a6d0103b563a
共同学习,写下你的评论
评论加载中...
作者其他优质文章