• SpringBoot使用手册


    SpringBoot使用手册

    1、自动装配

    1.1、创建spring Boot项目

    在之前的文章中已经专门写过,这里不做赘述。

    1.2、pom.xml

    1.2.1、版本管理

    在学习完maven项目后,我们学习框架时首先阅读的就是pom.xml文件,这里是管理自己该项目中所用到的jar包。

    学习SpringBoot时也是一样,在创建好一个项目后,我们首先先浏览一下pom.xml。

    <parent>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-parentartifactId>
            <version>2.7.3version>
            <relativePath/> 
        parent>
    

    打开这个文件后,我们首先就能看到上面这串代码,这个和我们之前学习的不太一样,仔细阅读后可以发现,这时导入Springboot父依赖的代码。

    什么是父依赖?

    当我们按住ctrl,鼠标点击这个依赖,可以发现我们又进入了一个pom文件,在这里,我们又能看到一个父依赖。

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-dependenciesartifactId>
        <version>2.7.3version>
      parent>
    

    再次点击进入,便能发现这里管理着大量的版本信息:

    <properties>
        <activemq.version>5.16.5activemq.version>
        <antlr2.version>2.7.7antlr2.version>
        <appengine-sdk.version>1.9.98appengine-sdk.version>
        <artemis.version>2.19.1artemis.version>
        <aspectj.version>1.9.7aspectj.version>
        <assertj.version>3.22.0assertj.version>
        <atomikos.version>4.0.6atomikos.version>
        <awaitility.version>4.2.0awaitility.version>
        <build-helper-maven-plugin.version>3.3.0build-helper-maven-plugin.version>
        <byte-buddy.version>1.12.13byte-buddy.version>
        <cache2k.version>2.6.1.Finalcache2k.version>
        <caffeine.version>2.9.3caffeine.version>
        <cassandra-driver.version>4.14.1cassandra-driver.version>
        <classmate.version>1.5.1classmate.version>
        <commons-codec.version>1.15commons-codec.version>
        <commons-dbcp2.version>2.9.0commons-dbcp2.version>
        <commons-lang3.version>3.12.0commons-lang3.version>
        <commons-pool.version>1.6commons-pool.version>
        <commons-pool2.version>2.11.1commons-pool2.version>
        <couchbase-client.version>3.3.3couchbase-client.version>
        <db2-jdbc.version>11.5.7.0db2-jdbc.version>
        <dependency-management-plugin.version>1.0.13.RELEASEdependency-management-plugin.version>
        <derby.version>10.14.2.0derby.version>
        <dropwizard-metrics.version>4.2.11dropwizard-metrics.version>
        <ehcache.version>2.10.9.2ehcache.version>
        <ehcache3.version>3.10.0ehcache3.version>
        <elasticsearch.version>7.17.4elasticsearch.version>
        <embedded-mongo.version>3.4.8embedded-mongo.version>
        <flyway.version>8.5.13flyway.version>
        <freemarker.version>2.3.31freemarker.version>
        <git-commit-id-plugin.version>4.9.10git-commit-id-plugin.version>
        <glassfish-el.version>3.0.4glassfish-el.version>
        <glassfish-jaxb.version>2.3.6glassfish-jaxb.version>
        <glassfish-jstl.version>1.2.6glassfish-jstl.version>
        <graphql-java.version>18.3graphql-java.version>
        <groovy.version>3.0.12groovy.version>
        <gson.version>2.9.1gson.version>
        <h2.version>2.1.214h2.version>
        <hamcrest.version>2.2hamcrest.version>
        <hazelcast.version>5.1.3hazelcast.version>
        <hazelcast-hibernate5.version>2.2.1hazelcast-hibernate5.version>
        <hibernate.version>5.6.10.Finalhibernate.version>
        <hibernate-validator.version>6.2.4.Finalhibernate-validator.version>
        <hikaricp.version>4.0.3hikaricp.version>
        <hsqldb.version>2.5.2hsqldb.version>
        <htmlunit.version>2.60.0htmlunit.version>
        <httpasyncclient.version>4.1.5httpasyncclient.version>
        <httpclient.version>4.5.13httpclient.version>
        <httpclient5.version>5.1.3httpclient5.version>
        <httpcore.version>4.4.15httpcore.version>
        <httpcore5.version>5.1.4httpcore5.version>
        <infinispan.version>13.0.10.Finalinfinispan.version>
        <influxdb-java.version>2.22influxdb-java.version>
        <jackson-bom.version>2.13.3jackson-bom.version>
        <jakarta-activation.version>1.2.2jakarta-activation.version>
        <jakarta-annotation.version>1.3.5jakarta-annotation.version>
        <jakarta-jms.version>2.0.3jakarta-jms.version>
        <jakarta-json.version>1.1.6jakarta-json.version>
        <jakarta-json-bind.version>1.0.2jakarta-json-bind.version>
        <jakarta-mail.version>1.6.7jakarta-mail.version>
        <jakarta-management.version>1.1.4jakarta-management.version>
        <jakarta-persistence.version>2.2.3jakarta-persistence.version>
        <jakarta-servlet.version>4.0.4jakarta-servlet.version>
        <jakarta-servlet-jsp-jstl.version>1.2.7jakarta-servlet-jsp-jstl.version>
        <jakarta-transaction.version>1.3.3jakarta-transaction.version>
        <jakarta-validation.version>2.0.2jakarta-validation.version>
        <jakarta-websocket.version>1.1.2jakarta-websocket.version>
        <jakarta-ws-rs.version>2.1.6jakarta-ws-rs.version>
        <jakarta-xml-bind.version>2.3.3jakarta-xml-bind.version>
        <jakarta-xml-soap.version>1.4.2jakarta-xml-soap.version>
        <jakarta-xml-ws.version>2.3.3jakarta-xml-ws.version>
        <janino.version>3.1.7janino.version>
        <javax-activation.version>1.2.0javax-activation.version>
        <javax-annotation.version>1.3.2javax-annotation.version>
        <javax-cache.version>1.1.1javax-cache.version>
        <javax-jaxb.version>2.3.1javax-jaxb.version>
        <javax-jaxws.version>2.3.1javax-jaxws.version>
        <javax-jms.version>2.0.1javax-jms.version>
        <javax-json.version>1.1.4javax-json.version>
        <javax-jsonb.version>1.0javax-jsonb.version>
        <javax-mail.version>1.6.2javax-mail.version>
        <javax-money.version>1.1javax-money.version>
        <javax-persistence.version>2.2javax-persistence.version>
        <javax-transaction.version>1.3javax-transaction.version>
        <javax-validation.version>2.0.1.Finaljavax-validation.version>
        <javax-websocket.version>1.1javax-websocket.version>
        <jaxen.version>1.2.0jaxen.version>
        <jaybird.version>4.0.6.java8jaybird.version>
        <jboss-logging.version>3.4.3.Finaljboss-logging.version>
        <jdom2.version>2.0.6.1jdom2.version>
        <jedis.version>3.8.0jedis.version>
        <jersey.version>2.35jersey.version>
        <jetty-el.version>9.0.52jetty-el.version>
        <jetty-jsp.version>2.2.0.v201112011158jetty-jsp.version>
        <jetty-reactive-httpclient.version>1.1.12jetty-reactive-httpclient.version>
        <jetty.version>9.4.48.v20220622jetty.version>
        <jmustache.version>1.15jmustache.version>
        <johnzon.version>1.2.18johnzon.version>
        <jolokia.version>1.7.1jolokia.version>
        <jooq.version>3.14.16jooq.version>
        <json-path.version>2.7.0json-path.version>
        <json-smart.version>2.4.8json-smart.version>
        <jsonassert.version>1.5.1jsonassert.version>
        <jstl.version>1.2jstl.version>
        <jtds.version>1.3.1jtds.version>
        <junit.version>4.13.2junit.version>
        <junit-jupiter.version>5.8.2junit-jupiter.version>
        <kafka.version>3.1.1kafka.version>
        <kotlin.version>1.6.21kotlin.version>
        <kotlin-coroutines.version>1.6.4kotlin-coroutines.version>
        <lettuce.version>6.1.9.RELEASElettuce.version>
        <liquibase.version>4.9.1liquibase.version>
        <log4j2.version>2.17.2log4j2.version>
        <logback.version>1.2.11logback.version>
        <lombok.version>1.18.24lombok.version>
        <mariadb.version>3.0.7mariadb.version>
        <maven-antrun-plugin.version>3.0.0maven-antrun-plugin.version>
        <maven-assembly-plugin.version>3.3.0maven-assembly-plugin.version>
        <maven-clean-plugin.version>3.2.0maven-clean-plugin.version>
        <maven-compiler-plugin.version>3.10.1maven-compiler-plugin.version>
        <maven-dependency-plugin.version>3.3.0maven-dependency-plugin.version>
        <maven-deploy-plugin.version>2.8.2maven-deploy-plugin.version>
        <maven-enforcer-plugin.version>3.0.0maven-enforcer-plugin.version>
        <maven-failsafe-plugin.version>2.22.2maven-failsafe-plugin.version>
        <maven-help-plugin.version>3.2.0maven-help-plugin.version>
        <maven-install-plugin.version>2.5.2maven-install-plugin.version>
        <maven-invoker-plugin.version>3.2.2maven-invoker-plugin.version>
        <maven-jar-plugin.version>3.2.2maven-jar-plugin.version>
        <maven-javadoc-plugin.version>3.4.1maven-javadoc-plugin.version>
        <maven-resources-plugin.version>3.2.0maven-resources-plugin.version>
        <maven-shade-plugin.version>3.3.0maven-shade-plugin.version>
        <maven-source-plugin.version>3.2.1maven-source-plugin.version>
        <maven-surefire-plugin.version>2.22.2maven-surefire-plugin.version>
        <maven-war-plugin.version>3.3.2maven-war-plugin.version>
        <micrometer.version>1.9.3micrometer.version>
        <mockito.version>4.5.1mockito.version>
        <mongodb.version>4.6.1mongodb.version>
        <mssql-jdbc.version>10.2.1.jre8mssql-jdbc.version>
        <mysql.version>8.0.30mysql.version>
        <nekohtml.version>1.9.22nekohtml.version>
        <neo4j-java-driver.version>4.4.9neo4j-java-driver.version>
        <netty.version>4.1.79.Finalnetty.version>
        <okhttp.version>4.9.3okhttp.version>
        <oracle-database.version>21.5.0.0oracle-database.version>
        <pooled-jms.version>1.2.4pooled-jms.version>
        <postgresql.version>42.3.6postgresql.version>
        <prometheus-client.version>0.15.0prometheus-client.version>
        <quartz.version>2.3.2quartz.version>
        <querydsl.version>5.0.0querydsl.version>
        <r2dbc-bom.version>Borca-SR1r2dbc-bom.version>
        <rabbit-amqp-client.version>5.14.2rabbit-amqp-client.version>
        <rabbit-stream-client.version>0.5.0rabbit-stream-client.version>
        <reactive-streams.version>1.0.4reactive-streams.version>
        <reactor-bom.version>2020.0.22reactor-bom.version>
        <rest-assured.version>4.5.1rest-assured.version>
        <rsocket.version>1.1.2rsocket.version>
        <rxjava.version>1.3.8rxjava.version>
        <rxjava-adapter.version>1.2.1rxjava-adapter.version>
        <rxjava2.version>2.2.21rxjava2.version>
        <saaj-impl.version>1.5.3saaj-impl.version>
        <selenium.version>4.1.4selenium.version>
        <selenium-htmlunit.version>3.61.0selenium-htmlunit.version>
        <sendgrid.version>4.9.3sendgrid.version>
        <servlet-api.version>4.0.1servlet-api.version>
        <slf4j.version>1.7.36slf4j.version>
        <snakeyaml.version>1.30snakeyaml.version>
        <solr.version>8.11.2solr.version>
        <spring-amqp.version>2.4.6spring-amqp.version>
        <spring-batch.version>4.3.6spring-batch.version>
        <spring-data-bom.version>2021.2.2spring-data-bom.version>
        <spring-framework.version>5.3.22spring-framework.version>
        <spring-graphql.version>1.0.1spring-graphql.version>
        <spring-hateoas.version>1.5.1spring-hateoas.version>
        <spring-integration.version>5.5.14spring-integration.version>
        <spring-kafka.version>2.8.8spring-kafka.version>
        <spring-ldap.version>2.4.1spring-ldap.version>
        <spring-restdocs.version>2.0.6.RELEASEspring-restdocs.version>
        <spring-retry.version>1.3.3spring-retry.version>
        <spring-security.version>5.7.3spring-security.version>
        <spring-session-bom.version>2021.2.0spring-session-bom.version>
        <spring-ws.version>3.1.3spring-ws.version>
        <sqlite-jdbc.version>3.36.0.3sqlite-jdbc.version>
        <sun-mail.version>1.6.7sun-mail.version>
        <thymeleaf.version>3.0.15.RELEASEthymeleaf.version>
        <thymeleaf-extras-data-attribute.version>2.0.1thymeleaf-extras-data-attribute.version>
        <thymeleaf-extras-java8time.version>3.0.4.RELEASEthymeleaf-extras-java8time.version>
        <thymeleaf-extras-springsecurity.version>3.0.4.RELEASEthymeleaf-extras-springsecurity.version>
        <thymeleaf-layout-dialect.version>3.0.0thymeleaf-layout-dialect.version>
        <tomcat.version>9.0.65tomcat.version>
        <unboundid-ldapsdk.version>6.0.5unboundid-ldapsdk.version>
        <undertow.version>2.2.19.Finalundertow.version>
        <versions-maven-plugin.version>2.10.0versions-maven-plugin.version>
        <webjars-locator-core.version>0.50webjars-locator-core.version>
        <wsdl4j.version>1.6.3wsdl4j.version>
        <xml-maven-plugin.version>1.0.2xml-maven-plugin.version>
        <xmlunit2.version>2.9.0xmlunit2.version>
      properties>
    

    这便是SpringBoot不需要书写版本的原因!

    配置依赖
    <resource>
            <directory>${basedir}/src/main/resourcesdirectory>
            <filtering>truefiltering>
            <includes>
              <include>**/application*.ymlinclude>
              <include>**/application*.yamlinclude>
              <include>**/application*.propertiesinclude>
            includes>
          resource>
          <resource>
            <directory>${basedir}/src/main/resourcesdirectory>
            <excludes>
              <exclude>**/application*.ymlexclude>
              <exclude>**/application*.yamlexclude>
              <exclude>**/application*.propertiesexclude>
            excludes>
          resource>
    
    1.2.2、启动器

    springBoot的启动器:spring Boot的启动场景。

    <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starterartifactId>
            dependency>
    

    比如:spring-boot-starter-web,它会自动导入web项目所需要的所有依赖。

    <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
            dependency>
    

    spring boot会将所有的功能场景,都编程一个个的启动器,如果我们要使用什么功能,就只需要找到对应的启动器就行了。

    spring官方文档:
    https://docs.spring.io
    
    1.2.3、主程序
    @SpringBootApplication
    public class BlogFztApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(BlogFztApplication.class, args);
        }
    
    }
    

    @SpringBootApplication:标注这个类是一个Springboot启动类,启动类下的所有资源被导入

    点进去,就能看到核心注解:

    注解:

    @SpringBootConfiguration//springboot的配置
    	@Configuration//spring配置类
    	@Component//说明这也是一个spring的组件
    @EnableAutoConfiguration//自动配置
    //点进去
    	@AutoConfigurationPackage//自动配置包
    		@Import(AutoConfigurationPackages.Registrar.class)//自动配置‘包注册’
    	@Import(AutoConfigurationImportSelector.class)//自动配置导入选择
    //获取所有配置
    List<String> configurations = getCandidateConfigurations(annotationMetadata,attributes);
    
    

    核心方法:

    获取候选的配置

    protected List getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
    		List configurations = new ArrayList<>(
    				SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(), getBeanClassLoader()));
    		ImportCandidates.load(AutoConfiguration.class, getBeanClassLoader()).forEach(configurations::add);
    		Assert.notEmpty(configurations,//断言非空,如果配置文件不为空,则去找spring.factories文件
    				"No auto configuration classes found in META-INF/spring.factories nor in META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports. If you "
    						+ "are using a custom packaging, make sure that file is correct.");
    		return configurations;
    	}
    
    protected Class<?> getSpringFactoriesLoaderFactoryClass() {
    		return EnableAutoConfiguration.class;
    	}
    
    META-INF/spring.factories:org.springframework.boot.autoconfigure
    # Initializers
    org.springframework.context.ApplicationContextInitializer=\
    org.springframework.boot.autoconfigure.SharedMetadataReaderFactoryContextInitializer,\
    org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener
    
    # Application Listeners
    org.springframework.context.ApplicationListener=\
    org.springframework.boot.autoconfigure.BackgroundPreinitializer
    
    # Environment Post Processors
    org.springframework.boot.env.EnvironmentPostProcessor=\
    org.springframework.boot.autoconfigure.integration.IntegrationPropertiesEnvironmentPostProcessor
    
    # Auto Configuration Import Listeners
    org.springframework.boot.autoconfigure.AutoConfigurationImportListener=\
    org.springframework.boot.autoconfigure.condition.ConditionEvaluationReportAutoConfigurationImportListener
    
    # Auto Configuration Import Filters
    org.springframework.boot.autoconfigure.AutoConfigurationImportFilter=\
    org.springframework.boot.autoconfigure.condition.OnBeanCondition,\
    org.springframework.boot.autoconfigure.condition.OnClassCondition,\
    org.springframework.boot.autoconfigure.condition.OnWebApplicationCondition
    
    # Failure analyzers
    org.springframework.boot.diagnostics.FailureAnalyzer=\
    org.springframework.boot.autoconfigure.data.redis.RedisUrlSyntaxFailureAnalyzer,\
    org.springframework.boot.autoconfigure.diagnostics.analyzer.NoSuchBeanDefinitionFailureAnalyzer,\
    org.springframework.boot.autoconfigure.flyway.FlywayMigrationScriptMissingFailureAnalyzer,\
    org.springframework.boot.autoconfigure.jdbc.DataSourceBeanCreationFailureAnalyzer,\
    org.springframework.boot.autoconfigure.jdbc.HikariDriverConfigurationFailureAnalyzer,\
    org.springframework.boot.autoconfigure.jooq.NoDslContextBeanFailureAnalyzer,\
    org.springframework.boot.autoconfigure.r2dbc.ConnectionFactoryBeanCreationFailureAnalyzer,\
    org.springframework.boot.autoconfigure.r2dbc.MissingR2dbcPoolDependencyFailureAnalyzer,\
    org.springframework.boot.autoconfigure.r2dbc.MultipleConnectionPoolConfigurationsFailureAnalzyer,\
    org.springframework.boot.autoconfigure.r2dbc.NoConnectionFactoryBeanFailureAnalyzer,\
    org.springframework.boot.autoconfigure.session.NonUniqueSessionRepositoryFailureAnalyzer
    
    # Template availability providers
    org.springframework.boot.autoconfigure.template.TemplateAvailabilityProvider=\
    org.springframework.boot.autoconfigure.freemarker.FreeMarkerTemplateAvailabilityProvider,\
    org.springframework.boot.autoconfigure.mustache.MustacheTemplateAvailabilityProvider,\
    org.springframework.boot.autoconfigure.groovy.template.GroovyTemplateAvailabilityProvider,\
    org.springframework.boot.autoconfigure.thymeleaf.ThymeleafTemplateAvailabilityProvider,\
    org.springframework.boot.autoconfigure.web.servlet.JspTemplateAvailabilityProvider
    
    # DataSource initializer detectors
    org.springframework.boot.sql.init.dependency.DatabaseInitializerDetector=\
    org.springframework.boot.autoconfigure.flyway.FlywayMigrationInitializerDatabaseInitializerDetector
    
    # Depends on database initialization detectors
    org.springframework.boot.sql.init.dependency.DependsOnDatabaseInitializationDetector=\
    org.springframework.boot.autoconfigure.batch.JobRepositoryDependsOnDatabaseInitializationDetector,\
    org.springframework.boot.autoconfigure.quartz.SchedulerDependsOnDatabaseInitializationDetector,\
    org.springframework.boot.autoconfigure.session.JdbcIndexedSessionRepositoryDependsOnDatabaseInitializationDetector
    
    
    Properties properties = PropertiesLoaderUtils.loadProperties(resource);
    
  • 相关阅读:
    学长教你学C-day8-C语言算数运算和逻辑运算
    Qt的QChartView实现缩放和放大功能
    JAVA也能用上Seq啦
    Dom--选项卡的实现
    vue | 样式隔离scoped的原理 样式穿透deep的原理
    【HCIA】交换机三层互通及链路可靠性
    【MindSpore易点通】如何使用溢出检测工具定位精度问题
    JAVA学习-java基础讲义02
    大模型存在“反转诅咒”现象,无法处理反向问题;Langchain课程资源
    JVM学习-- JVM调优
  • 原文地址:https://blog.csdn.net/fzt12138/article/details/140235159