目录
1、pom.xml文件配置说明
2、SpringBoot项目结构说明
3、入门案例关键配置说明
🌈 前面我们学习了SpringBoot快速入门案例,本节我们通过POM文件和项目结构分析两部分内容了解下关于SpringBoot的一些配置说明,以便全面了解SpringBoot项目结构组成及依赖技术栈。
1、pom.xml文件配置说明
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--引入SpringBoot Parent依赖-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.7</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!--当前项目的GAV坐标-->
<groupId>com.xintu.demo</groupId>
<artifactId>springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!--maven项目名称,可以删除-->
<name>SpringBootCase</name>
<!--maven项目描述,可以删除-->
<description>Demo project for Spring Boot</description>
<!--maven属性配置,可以在其它地方通过${}方式进行引用-->
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<!--SpringBoot框架web项目起步依赖,通过该依赖自动关联其它依赖,不需要我们一个一个去添加了-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--SpringBoot框架的测试起步依赖,例如:junit测试,如果不需要的话可以删除-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!--SpringBoot提供的打包编译等插件-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2、SpringBoot项目结构说明
1) mvnw|mvnw.cmd文件
这两个脚本分别对应的是linux和windows环境,可执行maven相关的命令,通常使用比较少。
2) gitignore文件
使用版本控制工具git的时候,设置一些忽略提交的内容。这样可以避免很多无效文件提交到远程仓库。如:class文件、.idea文件内容等。
3)static|templates文件夹
涉及到页面或模板技术都会存放该目录下,比如html页面、JS脚本、CSS样式,以及Thymeleaf等。
4)application.properties(默认生成) | application.yml(手动添加)
SpringBoot的配置文件,很多集成的配置都可以在该文件中进行配置。例如:服务端口号、应用名称、数据库(Redis、ES、MySQL)连接信息、MyBatis配置、Rabbit MQ等。
5)SpringbootApplication.java
该类是SpringBoot程序执行的入口,执行该程序中的main方法,SpringBoot就启动了。
3、入门案例关键配置说明
1)Spring Boot的父级依赖spring-boot-starter-parent配置之后,当前的项目就是Spring Boot项目。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.7</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
2)spring-boot-starter-parent是一个Springboot的父级依赖,开发SpringBoot程序都需要继承该父级项目,它用来提供相关的Maven默认依赖,使用它之后,常用的jar包依赖可以省去version配置。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!--省略版本号-->
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<!--省略版本号-->
</dependency>
3)Spring Boot提供了哪些默认jar包的依赖,可查看该父级依赖的pom文件。大约有200多个依赖包,从中我们可以了解SpringBoot融合了哪些技术栈,便于我们全面了解SpringBoot这门技术。
<!--整体了解SpringBoot所使用的技术栈-->
<properties>
<activemq.version>5.16.5</activemq.version>
<antlr2.version>2.7.7</antlr2.version>
<appengine-sdk.version>1.9.98</appengine-sdk.version>
<artemis.version>2.19.1</artemis.version>
<aspectj.version>1.9.7</aspectj.version>
<assertj.version>3.22.0</assertj.version>
<atomikos.version>4.0.6</atomikos.version>
<awaitility.version>4.2.0</awaitility.version>
<build-helper-maven-plugin.version>3.3.0</build-helper-maven-plugin.version>
<byte-buddy.version>1.12.20</byte-buddy.version>
<cache2k.version>2.6.1.Final</cache2k.version>
<caffeine.version>2.9.3</caffeine.version>
<cassandra-driver.version>4.14.1</cassandra-driver.version>
<classmate.version>1.5.1</classmate.version>
<commons-codec.version>1.15</commons-codec.version>
<commons-dbcp2.version>2.9.0</commons-dbcp2.version>
<commons-lang3.version>3.12.0</commons-lang3.version>
<commons-pool.version>1.6</commons-pool.version>
<commons-pool2.version>2.11.1</commons-pool2.version>
<couchbase-client.version>3.3.4</couchbase-client.version>
<db2-jdbc.version>11.5.7.0</db2-jdbc.version>
<dependency-management-plugin.version>1.0.15.RELEASE</dependency-management-plugin.version>
<derby.version>10.14.2.0</derby.version>
<dropwizard-metrics.version>4.2.14</dropwizard-metrics.version>
<ehcache.version>2.10.9.2</ehcache.version>
<ehcache3.version>3.10.8</ehcache3.version>
<elasticsearch.version>7.17.8</elasticsearch.version>
<embedded-mongo.version>3.4.11</embedded-mongo.version>
<flyway.version>8.5.13</flyway.version>
<freemarker.version>2.3.31</freemarker.version>
<git-commit-id-plugin.version>4.9.10</git-commit-id-plugin.version>
<glassfish-el.version>3.0.4</glassfish-el.version>
<glassfish-jaxb.version>2.3.7</glassfish-jaxb.version>
<glassfish-jstl.version>1.2.6</glassfish-jstl.version>
<graphql-java.version>18.3</graphql-java.version>
<groovy.version>3.0.13</groovy.version>
<gson.version>2.9.1</gson.version>
<h2.version>2.1.214</h2.version>
<hamcrest.version>2.2</hamcrest.version>
<hazelcast.version>5.1.5</hazelcast.version>
<hazelcast-hibernate5.version>2.2.1</hazelcast-hibernate5.version>
<hibernate.version>5.6.14.Final</hibernate.version>
<hibernate-validator.version>6.2.5.Final</hibernate-validator.version>
<hikaricp.version>4.0.3</hikaricp.version>
<hsqldb.version>2.5.2</hsqldb.version>
<htmlunit.version>2.60.0</htmlunit.version>
<httpasyncclient.version>4.1.5</httpasyncclient.version>
<httpclient.version>4.5.14</httpclient.version>
<httpclient5.version>5.1.4</httpclient5.version>
<httpcore.version>4.4.16</httpcore.version>
<httpcore5.version>5.1.5</httpcore5.version>
<infinispan.version>13.0.14.Final</infinispan.version>
<influxdb-java.version>2.22</influxdb-java.version>
<jackson-bom.version>2.13.4.20221013</jackson-bom.version>
<jakarta-activation.version>1.2.2</jakarta-activation.version>
<jakarta-annotation.version>1.3.5</jakarta-annotation.version>
<jakarta-jms.version>2.0.3</jakarta-jms.version>
<jakarta-json.version>1.1.6</jakarta-json.version>
<jakarta-json-bind.version>1.0.2</jakarta-json-bind.version>
<jakarta-mail.version>1.6.7</jakarta-mail.version>
<jakarta-management.version>1.1.4</jakarta-management.version>
<jakarta-persistence.version>2.2.3</jakarta-persistence.version>
<jakarta-servlet.version>4.0.4</jakarta-servlet.version>
<jakarta-servlet-jsp-jstl.version>1.2.7</jakarta-servlet-jsp-jstl.version>
<jakarta-transaction.version>1.3.3</jakarta-transaction.version>
<jakarta-validation.version>2.0.2</jakarta-validation.version>
<jakarta-websocket.version>1.1.2</jakarta-websocket.version>
<jakarta-ws-rs.version>2.1.6</jakarta-ws-rs.version>
<jakarta-xml-bind.version>2.3.3</jakarta-xml-bind.version>
<jakarta-xml-soap.version>1.4.2</jakarta-xml-soap.version>
<jakarta-xml-ws.version>2.3.3</jakarta-xml-ws.version>
<janino.version>3.1.9</janino.version>
<javax-activation.version>1.2.0</javax-activation.version>
<javax-annotation.version>1.3.2</javax-annotation.version>
<javax-cache.version>1.1.1</javax-cache.version>
<javax-jaxb.version>2.3.1</javax-jaxb.version>
<javax-jaxws.version>2.3.1</javax-jaxws.version>
<javax-jms.version>2.0.1</javax-jms.version>
<javax-json.version>1.1.4</javax-json.version>
<javax-jsonb.version>1.0</javax-jsonb.version>
<javax-mail.version>1.6.2</javax-mail.version>
<javax-money.version>1.1</javax-money.version>
<javax-persistence.version>2.2</javax-persistence.version>
<javax-transaction.version>1.3</javax-transaction.version>
<javax-validation.version>2.0.1.Final</javax-validation.version>
<javax-websocket.version>1.1</javax-websocket.version>
<jaxen.version>1.2.0</jaxen.version>
<jaybird.version>4.0.8.java8</jaybird.version>
<jboss-logging.version>3.4.3.Final</jboss-logging.version>
<jdom2.version>2.0.6.1</jdom2.version>
<jedis.version>3.8.0</jedis.version>
<jersey.version>2.35</jersey.version>
<jetty-el.version>9.0.52</jetty-el.version>
<jetty-jsp.version>2.2.0.v201112011158</jetty-jsp.version>
<jetty-reactive-httpclient.version>1.1.13</jetty-reactive-httpclient.version>
<jetty.version>9.4.50.v20221201</jetty.version>
<jmustache.version>1.15</jmustache.version>
<johnzon.version>1.2.19</johnzon.version>
<jolokia.version>1.7.1</jolokia.version>
<jooq.version>3.14.16</jooq.version>
<json-path.version>2.7.0</json-path.version>
<json-smart.version>2.4.8</json-smart.version>
<jsonassert.version>1.5.1</jsonassert.version>
<jstl.version>1.2</jstl.version>
<jtds.version>1.3.1</jtds.version>
<junit.version>4.13.2</junit.version>
<junit-jupiter.version>5.8.2</junit-jupiter.version>
<kafka.version>3.1.2</kafka.version>
<kotlin.version>1.6.21</kotlin.version>
<kotlin-coroutines.version>1.6.4</kotlin-coroutines.version>
<lettuce.version>6.1.10.RELEASE</lettuce.version>
<liquibase.version>4.9.1</liquibase.version>
<log4j2.version>2.17.2</log4j2.version>
<logback.version>1.2.11</logback.version>
<lombok.version>1.18.24</lombok.version>
<mariadb.version>3.0.9</mariadb.version>
<maven-antrun-plugin.version>3.0.0</maven-antrun-plugin.version>
<maven-assembly-plugin.version>3.3.0</maven-assembly-plugin.version>
<maven-clean-plugin.version>3.2.0</maven-clean-plugin.version>
<maven-compiler-plugin.version>3.10.1</maven-compiler-plugin.version>
<maven-dependency-plugin.version>3.3.0</maven-dependency-plugin.version>
<maven-deploy-plugin.version>2.8.2</maven-deploy-plugin.version>
<maven-enforcer-plugin.version>3.0.0</maven-enforcer-plugin.version>
<maven-failsafe-plugin.version>2.22.2</maven-failsafe-plugin.version>
<maven-help-plugin.version>3.2.0</maven-help-plugin.version>
<maven-install-plugin.version>2.5.2</maven-install-plugin.version>
<maven-invoker-plugin.version>3.2.2</maven-invoker-plugin.version>
<maven-jar-plugin.version>3.2.2</maven-jar-plugin.version>
<maven-javadoc-plugin.version>3.4.1</maven-javadoc-plugin.version>
<maven-resources-plugin.version>3.2.0</maven-resources-plugin.version>
<maven-shade-plugin.version>3.3.0</maven-shade-plugin.version>
<maven-source-plugin.version>3.2.1</maven-source-plugin.version>
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
<maven-war-plugin.version>3.3.2</maven-war-plugin.version>
<micrometer.version>1.9.6</micrometer.version>
<mockito.version>4.5.1</mockito.version>
<mongodb.version>4.6.1</mongodb.version>
<mssql-jdbc.version>10.2.2.jre8</mssql-jdbc.version>
<mysql.version>8.0.31</mysql.version>
<nekohtml.version>1.9.22</nekohtml.version>
<neo4j-java-driver.version>4.4.11</neo4j-java-driver.version>
<netty.version>4.1.86.Final</netty.version>
<okhttp.version>4.9.3</okhttp.version>
<oracle-database.version>21.5.0.0</oracle-database.version>
<pooled-jms.version>1.2.4</pooled-jms.version>
<postgresql.version>42.3.8</postgresql.version>
<prometheus-client.version>0.15.0</prometheus-client.version>
<quartz.version>2.3.2</quartz.version>
<querydsl.version>5.0.0</querydsl.version>
<r2dbc-bom.version>Borca-SR2</r2dbc-bom.version>
<rabbit-amqp-client.version>5.14.2</rabbit-amqp-client.version>
<rabbit-stream-client.version>0.5.0</rabbit-stream-client.version>
<reactive-streams.version>1.0.4</reactive-streams.version>
<reactor-bom.version>2020.0.26</reactor-bom.version>
<rest-assured.version>4.5.1</rest-assured.version>
<rsocket.version>1.1.3</rsocket.version>
<rxjava.version>1.3.8</rxjava.version>
<rxjava-adapter.version>1.2.1</rxjava-adapter.version>
<rxjava2.version>2.2.21</rxjava2.version>
<saaj-impl.version>1.5.3</saaj-impl.version>
<selenium.version>4.1.4</selenium.version>
<selenium-htmlunit.version>3.61.0</selenium-htmlunit.version>
<sendgrid.version>4.9.3</sendgrid.version>
<servlet-api.version>4.0.1</servlet-api.version>
<slf4j.version>1.7.36</slf4j.version>
<snakeyaml.version>1.30</snakeyaml.version>
<solr.version>8.11.2</solr.version>
<spring-amqp.version>2.4.8</spring-amqp.version>
<spring-batch.version>4.3.7</spring-batch.version>
<spring-data-bom.version>2021.2.6</spring-data-bom.version>
<spring-framework.version>5.3.24</spring-framework.version>
<spring-graphql.version>1.0.3</spring-graphql.version>
<spring-hateoas.version>1.5.2</spring-hateoas.version>
<spring-integration.version>5.5.16</spring-integration.version>
<spring-kafka.version>2.8.11</spring-kafka.version>
<spring-ldap.version>2.4.1</spring-ldap.version>
<spring-restdocs.version>2.0.7.RELEASE</spring-restdocs.version>
<spring-retry.version>1.3.4</spring-retry.version>
<spring-security.version>5.7.6</spring-security.version>
<spring-session-bom.version>2021.2.0</spring-session-bom.version>
<spring-ws.version>3.1.4</spring-ws.version>
<sqlite-jdbc.version>3.36.0.3</sqlite-jdbc.version>
<sun-mail.version>1.6.7</sun-mail.version>
<thymeleaf.version>3.0.15.RELEASE</thymeleaf.version>
<thymeleaf-extras-data-attribute.version>2.0.1</thymeleaf-extras-data-attribute.version>
<thymeleaf-extras-java8time.version>3.0.4.RELEASE</thymeleaf-extras-java8time.version>
<thymeleaf-extras-springsecurity.version>3.0.5.RELEASE</thymeleaf-extras-springsecurity.version>
<thymeleaf-layout-dialect.version>3.0.0</thymeleaf-layout-dialect.version>
<tomcat.version>9.0.70</tomcat.version>
<unboundid-ldapsdk.version>6.0.7</unboundid-ldapsdk.version>
<undertow.version>2.2.22.Final</undertow.version>
<versions-maven-plugin.version>2.10.0</versions-maven-plugin.version>
<webjars-locator-core.version>0.50</webjars-locator-core.version>
<wsdl4j.version>1.6.3</wsdl4j.version>
<xml-maven-plugin.version>1.0.2</xml-maven-plugin.version>
<xmlunit2.version>2.9.0</xmlunit2.version>
</properties>
4)如果在项目中,不想使用某个默认依赖版本,可通过修改pom.xml文件的单独依赖配置覆盖即可,比如需要用Spring版本3.x 覆盖2.x。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>3.x版本号</version>
</dependency>
5)@SpringBootApplication注解属于Spring Boot项目的最核心注解,主要作用开启Spring自动配置。如果在启动类上去掉该注解,那么就不会启动SpringBoot程序。
package com.xintu.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@SpringBootApplication
public class SpringbootApplication {
//...
}
6)main方法是一个标准的Java程序的main方法,主要作用是作为项目启动运行的入口。
package com.xintu.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@SpringBootApplication
public class SpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootApplication.class, args);
}
//...
}
7)@RestController 依然是我们之前的Spring MVC,这个注解类内部包含了@Controller 和 @ResponseBody注解,简化了原来Spring的配置方式。
package com.xintu.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@SpringBootApplication
public class SpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootApplication.class, args);
}
@GetMapping("/hello")
public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
return String.format("欢迎 %s 来到<a href=\"http://www.35xintu.com\">35新途</a>!", name);
}
}
我们看下@RestController源码,
package org.springframework.web.bind.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.core.annotation.AliasFor;
import org.springframework.stereotype.Controller;
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller //原Spring配置
@ResponseBody //原Spring配置
public @interface RestController {
@AliasFor(
annotation = Controller.class
)
String value() default "";
}
其内部引入了@Controller 和 @ResponseBody这两个注解。
下一篇将详细介绍关于SpringBoot核心配置,便于我们进一步深入了解Springboot可管理的配置化能力。