🏡JAVA码农探花:
** 🔥 推荐专栏**:《SSM笔记》《SpringBoot笔记》
🛸学无止境,不骄不躁,知行合一
前言
SpringBoot项目结构,特性介绍(常用到的基本都讲解了,很详细,基于官方文档讲解)。
一、SpringBoot项目结构
IDEA创建非常简单,不是主要讲解,我使用的是IDEA2023.3(这里默认你Maven环境都配好了)
创建完成后的文件结构如下:
- java:java包下存放的是开发创建的类、接口等。 - 这里restart是我的工程名,同时它就是项目工程文件夹,开发所创建的包、类基本都在这文件夹下。- SpringBoot启动类(这里的启动类是RestartApplication,SpringBoot项目创建完成后默认的启动类名称是首字母大写的项目工程名+Application)放在工程文件夹下,即:工程文件夹 / 启动类。不在嵌套文件夹。
- resources - 存放静态资源文件的地方:html、css、照片、配置文件(properties文件,yaml文件等)。
- pom.xml - 引入依赖文件,用于引入你工程所需要的依赖。
1.启动类的位置
- 官方建议最佳实战就是将启动类放在工程文件夹下的第一层,不需要再嵌套文件夹。
- 官方说:**@SpringBootApplication 注解一般都是注解在启动类上的。它默认会扫描当前类下的所有子包(也就是与当前启动类同级文件夹的包及其下的所有包,如下图的customer、order包及其下的所有包)。例如,如果你正在编写一个JPA应用程序,你的 @Entity 类只有定义在启动类的子包下才能被扫描加载到。这样的好处也显而易见,@SpringBootApplication 默认只会扫描加载你项目工程**中的组件。
<span style="color:#000000"><span style="background-color:#fafafa"><code class="language-java"><span style="color:#708090">//初始创建的SpringBoot项目的启动类都是如下代码:</span>
<span style="color:#0077aa">import</span> org<span style="color:#999999">.</span>springframework<span style="color:#999999">.</span>boot<span style="color:#999999">.</span>SpringApplication<span style="color:#999999">;</span>
<span style="color:#0077aa">import</span> org<span style="color:#999999">.</span>springframework<span style="color:#999999">.</span>boot<span style="color:#999999">.</span>autoconfigure<span style="color:#999999">.</span>SpringBootApplication<span style="color:#999999">;</span>
<span style="color:#999999">@SpringBootApplication</span>
<span style="color:#0077aa">public</span> <span style="color:#0077aa">class</span> RestartApplication <span style="color:#999999">{</span>
<span style="color:#0077aa">public</span> <span style="color:#0077aa">static</span> <span style="color:#0077aa">void</span> <span style="color:#dd4a68">main</span><span style="color:#999999">(</span>String<span style="color:#999999">[</span><span style="color:#999999">]</span> args<span style="color:#999999">)</span> <span style="color:#999999">{</span>
SpringApplication<span style="color:#999999">.</span><span style="color:#dd4a68">run</span><span style="color:#999999">(</span>RestartApplication<span style="color:#999999">.</span><span style="color:#0077aa">class</span><span style="color:#999999">,</span> args<span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
<span style="color:#999999">}</span>
</code></span></span>
2.pom文件
- 大多数Spring Boot应用在POM的 parent 部分使用 spring-boot-starter-parent。 spring-boot-starter-parent 是一个特殊的starter,提供有用的Maven默认值。
- 需要的依赖在 < dependency > 标签内导入需要的< dependeny>
start
- Starter是一系列开箱即用的依赖,你可以在你的应用程序中导入它们。 通过你Starter,可以获得所有你需要的Spring和相关技术的一站式服务,免去了需要到处大量复制粘贴依赖的烦恼。 - 例如,如果你想开始使用Spring和JPA进行数据库访问,那么可以直接在你的项目中导入 spring-boot-starter-data-jpa 依赖。(也就是说现在你想要什么依赖,只需要spring-boot-start-xxx就可以帮你自动导入这方面的所有常规依赖)
- Starter含了很多你需要的依赖,以使项目快速启动和运行,并拥有一套一致的、受支持的可管理的过渡性依赖。
例子:
在idea的终端输入
mvn dependency:tree
打印了一个项目依赖的树状图,如下:
你可以看到 spring-boot-starter-parent 本身没有导入什么依赖(就是一些基本的依赖如spring方面的,日志方面的等)。 要导入必须的依赖,需要编辑 pom.xml 文件。
假如正在开发一个Web应用程序,我们添加一个 spring-boot-starter-web 依赖项:
<span style="color:#000000"><span style="background-color:#fafafa"><code class="language-xml"><span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>dependencies</span><span style="color:#999999">></span></span>
<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>dependency</span><span style="color:#999999">></span></span>
<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>groupId</span><span style="color:#999999">></span></span>org.springframework.boot<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>groupId</span><span style="color:#999999">></span></span>
<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>artifactId</span><span style="color:#999999">></span></span>spring-boot-starter-web<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>artifactId</span><span style="color:#999999">></span></span>
<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>dependency</span><span style="color:#999999">></span></span>
<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>dependencies</span><span style="color:#999999">></span></span>
</code></span></span>
如果你再次运行
mvn dependency:tree
,你会看到现在有一些额外的依赖,包括Tomcat web服务器等。基本将常规依赖都引入进来了。
parent
- spring-boot-starter-parent 是一个特殊的starter,它在POM.xml里面声明了spring boot的各个依赖及其版本。子项目直接继承它,子项在导入 spring boot 依赖时,不需要声明版本号。 - 通俗的说parent部分就是在你导入spring-boot-start–parent时,自动帮你冲裁了版本号,你在dependenys标签里的依赖可以不用写版本号
提问:不想使用仲裁版本怎么办?
在pom.xml重写配置,如下面的代码(这里以mysql为例,换成5.1.43版本):
<span style="color:#000000"><span style="background-color:#fafafa"><code class="language-xml"><span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>properties</span><span style="color:#999999">></span></span>
<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>mysql.version</span><span style="color:#999999">></span></span>5.1.43<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>mysql.version</span><span style="color:#999999">></span></span>
<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>properties</span><span style="color:#999999">></span></span>
</code></span></span>
打包
使用Maven快速打包
IDEA内终端运行
mvn package
<span style="color:#000000"><span style="background-color:#fafafa"><code class="language-java">$ mvn <span style="color:#0077aa">package</span>
<span style="color:#999999">[</span><span style="color:#986801">INFO</span><span style="color:#999999">]</span> Scanning <span style="color:#0077aa">for</span> projects<span style="color:#999999">.</span><span style="color:#999999">.</span><span style="color:#999999">.</span>
<span style="color:#999999">[</span><span style="color:#986801">INFO</span><span style="color:#999999">]</span>
<span style="color:#999999">[</span><span style="color:#986801">INFO</span><span style="color:#999999">]</span> <span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span>
<span style="color:#999999">[</span><span style="color:#986801">INFO</span><span style="color:#999999">]</span> Building myproject <span style="color:#986801">0.0</span><span style="color:#986801">.1</span><span style="color:#a67f59">-</span><span style="color:#986801">SNAPSHOT</span>
<span style="color:#999999">[</span><span style="color:#986801">INFO</span><span style="color:#999999">]</span> <span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span>
<span style="color:#999999">[</span><span style="color:#986801">INFO</span><span style="color:#999999">]</span> <span style="color:#999999">.</span><span style="color:#999999">.</span><span style="color:#999999">.</span><span style="color:#999999">.</span> <span style="color:#999999">.</span><span style="color:#999999">.</span>
<span style="color:#999999">[</span><span style="color:#986801">INFO</span><span style="color:#999999">]</span> <span style="color:#a67f59">--</span><span style="color:#a67f59">-</span> maven<span style="color:#a67f59">-</span>jar<span style="color:#a67f59">-</span>plugin<span style="color:#a67f59">:</span><span style="color:#986801">2.4</span><span style="color:#a67f59">:</span>jar <span style="color:#999999">(</span><span style="color:#0077aa">default</span><span style="color:#a67f59">-</span>jar<span style="color:#999999">)</span> @ myproject <span style="color:#a67f59">--</span><span style="color:#a67f59">-</span>
<span style="color:#999999">[</span><span style="color:#986801">INFO</span><span style="color:#999999">]</span> Building jar<span style="color:#a67f59">:</span> <span style="color:#a67f59">/</span>Users<span style="color:#a67f59">/</span>developer<span style="color:#a67f59">/</span>example<span style="color:#a67f59">/</span>spring<span style="color:#a67f59">-</span>boot<span style="color:#a67f59">-</span>example<span style="color:#a67f59">/</span>target<span style="color:#a67f59">/</span>myproject<span style="color:#a67f59">-</span><span style="color:#986801">0.0</span><span style="color:#986801">.1</span><span style="color:#a67f59">-</span><span style="color:#986801">SNAPSHOT</span><span style="color:#999999">.</span>jar
<span style="color:#999999">[</span><span style="color:#986801">INFO</span><span style="color:#999999">]</span>
<span style="color:#999999">[</span><span style="color:#986801">INFO</span><span style="color:#999999">]</span> <span style="color:#a67f59">--</span><span style="color:#a67f59">-</span> spring<span style="color:#a67f59">-</span>boot<span style="color:#a67f59">-</span>maven<span style="color:#a67f59">-</span>plugin<span style="color:#a67f59">:</span><span style="color:#986801">3.2</span><span style="color:#986801">.0</span><span style="color:#a67f59">-</span><span style="color:#986801">SNAPSHOT</span><span style="color:#a67f59">:</span>repackage <span style="color:#999999">(</span><span style="color:#0077aa">default</span><span style="color:#999999">)</span> @ myproject <span style="color:#a67f59">--</span><span style="color:#a67f59">-</span>
<span style="color:#999999">[</span><span style="color:#986801">INFO</span><span style="color:#999999">]</span> <span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span>
<span style="color:#999999">[</span><span style="color:#986801">INFO</span><span style="color:#999999">]</span> <span style="color:#986801">BUILD</span> <span style="color:#986801">SUCCESS</span>
<span style="color:#999999">[</span><span style="color:#986801">INFO</span><span style="color:#999999">]</span> <span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span><span style="color:#a67f59">--</span>
</code></span></span>
另一种:
运行后你会发现工程内多个target文件夹:
如果你查看 target 目录,你应该看到 项目名-0.0.1-SNAPSHOT.jar
(没有target可以在从电脑中找到项目文件夹,里面应该能找到target文件夹,在jar包所在文件夹打开电脑终端,运行
java -jar 项目名-1.0-SNAPSHOT.jar
)
IDEA终端运行
java -jar target/myproject-0.0.1-SNAPSHOT.jar
:
<span style="color:#000000"><span style="background-color:#fafafa"><code class="language-java">$ java <span style="color:#a67f59">-</span>jar target<span style="color:#a67f59">/</span>myproject<span style="color:#a67f59">-</span><span style="color:#986801">0.0</span><span style="color:#986801">.1</span><span style="color:#a67f59">-</span><span style="color:#986801">SNAPSHOT</span><span style="color:#999999">.</span>jar
<span style="color:#999999">.</span> ____ _ __ _ _
<span style="color:#a67f59">/</span>\\ <span style="color:#a67f59">/</span> ___'_ __ _ <span style="color:#dd4a68">_</span><span style="color:#999999">(</span>_<span style="color:#999999">)</span>_ __ __ _ \ \ \ \
<span style="color:#999999">(</span> <span style="color:#999999">(</span> <span style="color:#999999">)</span>\___ <span style="color:#a67f59">|</span> <span style="color:#50a14f">'_ | '</span>_<span style="color:#a67f59">|</span> <span style="color:#a67f59">|</span> '_ \<span style="color:#a67f59">/</span> _` <span style="color:#a67f59">|</span> \ \ \ \
\\<span style="color:#a67f59">/</span> ___<span style="color:#999999">)</span><span style="color:#a67f59">|</span> <span style="color:#a67f59">|</span>_<span style="color:#999999">)</span><span style="color:#a67f59">|</span> <span style="color:#a67f59">|</span> <span style="color:#a67f59">|</span> <span style="color:#a67f59">|</span> <span style="color:#a67f59">|</span> <span style="color:#a67f59">||</span> <span style="color:#999999">(</span>_<span style="color:#a67f59">|</span> <span style="color:#a67f59">|</span> <span style="color:#999999">)</span> <span style="color:#999999">)</span> <span style="color:#999999">)</span> <span style="color:#999999">)</span>
' <span style="color:#a67f59">|</span>____<span style="color:#a67f59">|</span> <span style="color:#999999">.</span>__<span style="color:#a67f59">|</span>_<span style="color:#a67f59">|</span> <span style="color:#a67f59">|</span>_<span style="color:#a67f59">|</span>_<span style="color:#a67f59">|</span> <span style="color:#a67f59">|</span>_\__<span style="color:#999999">,</span> <span style="color:#a67f59">|</span> <span style="color:#a67f59">/</span> <span style="color:#a67f59">/</span> <span style="color:#a67f59">/</span> <span style="color:#a67f59">/</span>
<span style="color:#a67f59">==</span><span style="color:#a67f59">==</span><span style="color:#a67f59">==</span><span style="color:#a67f59">==</span><span style="color:#a67f59">=</span><span style="color:#a67f59">|</span>_<span style="color:#a67f59">|=</span><span style="color:#a67f59">==</span><span style="color:#a67f59">==</span><span style="color:#a67f59">==</span><span style="color:#a67f59">==</span><span style="color:#a67f59">==</span><span style="color:#a67f59">==</span><span style="color:#a67f59">=</span><span style="color:#a67f59">|</span>___<span style="color:#a67f59">/=</span><span style="color:#a67f59">/</span>_<span style="color:#a67f59">/</span>_<span style="color:#a67f59">/</span>_<span style="color:#a67f59">/</span>
<span style="color:#a67f59">::</span> Spring Boot <span style="color:#a67f59">::</span> <span style="color:#999999">(</span>v3<span style="color:#999999">.</span><span style="color:#986801">1.2</span><span style="color:#a67f59">-</span><span style="color:#986801">SNAPSHOT</span><span style="color:#999999">)</span>
<span style="color:#999999">.</span><span style="color:#999999">.</span><span style="color:#999999">.</span><span style="color:#999999">.</span><span style="color:#999999">.</span><span style="color:#999999">.</span><span style="color:#999999">.</span> <span style="color:#999999">.</span> <span style="color:#999999">.</span> <span style="color:#999999">.</span>
<span style="color:#999999">.</span><span style="color:#999999">.</span><span style="color:#999999">.</span><span style="color:#999999">.</span><span style="color:#999999">.</span><span style="color:#999999">.</span><span style="color:#999999">.</span> <span style="color:#999999">.</span> <span style="color:#999999">.</span> <span style="color:#999999">.</span> <span style="color:#999999">(</span>log output here<span style="color:#999999">)</span>
<span style="color:#999999">.</span><span style="color:#999999">.</span><span style="color:#999999">.</span><span style="color:#999999">.</span><span style="color:#999999">.</span><span style="color:#999999">.</span><span style="color:#999999">.</span> <span style="color:#999999">.</span> <span style="color:#999999">.</span> <span style="color:#999999">.</span>
<span style="color:#999999">.</span><span style="color:#999999">.</span><span style="color:#999999">.</span><span style="color:#999999">.</span><span style="color:#999999">.</span><span style="color:#999999">.</span><span style="color:#999999">.</span><span style="color:#999999">.</span> Started MyApplication in <span style="color:#986801">0.999</span> seconds <span style="color:#999999">(</span>process running <span style="color:#0077aa">for</span> <span style="color:#986801">1.253</span><span style="color:#999999">)</span>
</code></span></span>
要退出应用程序,按 ctrl-c。
如果你想看看这jar里面到底有什么东西,你可以使用 jar tvf:
jar tvf target/项目名-0.0.1-SNAPSHOT.jar
二、依赖管理特性
Spring Boot的每个版本都提供了一个它所支持的依赖的列表。 在实践中,你不需要在构建配置中为这些依赖声明版本,因为Spring Boot会帮你管理这些。 当你升级Spring Boot本身时,这些依赖也会一同升级。(所以现在你联系一下parent和start是不是豁然开朗)
- 开发导入starter场景启动器 1. spring-boot-starter-* : * 就是某种场景2. 只要引入starter,这个场景的所有常规需要的依赖我们都自动引入3. 更多SpringBoot所有支持的场景4. *-spring-boot-starter: 第三方为我们提供的简化开发的场景启动器。
<span style="color:#000000"><span style="background-color:#fafafa"><code class="language-xml">所有场景启动器最底层的依赖
<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>dependency</span><span style="color:#999999">></span></span>
<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>groupId</span><span style="color:#999999">></span></span>org.springframework.boot<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>groupId</span><span style="color:#999999">></span></span>
<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>artifactId</span><span style="color:#999999">></span></span>spring-boot-starter<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>artifactId</span><span style="color:#999999">></span></span>
<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>version</span><span style="color:#999999">></span></span>3.1.2<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>version</span><span style="color:#999999">></span></span>
<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>scope</span><span style="color:#999999">></span></span>compile<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>scope</span><span style="color:#999999">></span></span>
<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>dependency</span><span style="color:#999999">></span></span>
</code></span></span>
- 无需关注版本号,自动版本仲裁1. 引入依赖默认都可以不写版本2. 引入非版本仲裁的jar,要写版本号。
- 可以修改默认版本号1. 查看spring-boot-dependencies里面规定当前依赖的版本 用的 key。2. 在当前项目里面重写配置,如下面的代码。
<span style="color:#000000"><span style="background-color:#fafafa"><code class="language-xml"><span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>properties</span><span style="color:#999999">></span></span>
<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>mysql.version</span><span style="color:#999999">></span></span>5.1.43<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>mysql.version</span><span style="color:#999999">></span></span>
<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>properties</span><span style="color:#999999">></span></span>
</code></span></span>
IDEA快捷键:
ctrl + shift + alt + U
:以图的方式显示项目中依赖之间的关系。alt + ins
:相当于Eclipse的 Ctrl + N,创建新类,新包等。
三、自动配置特性
- 自动配好Tomcat - 引入Tomcat依赖。- 配置Tomcat
<span style="color:#000000"><span style="background-color:#fafafa"><code class="language-xml"><span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>dependency</span><span style="color:#999999">></span></span>
<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>groupId</span><span style="color:#999999">></span></span>org.springframework.boot<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>groupId</span><span style="color:#999999">></span></span>
<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>artifactId</span><span style="color:#999999">></span></span>spring-boot-starter-tomcat<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>artifactId</span><span style="color:#999999">></span></span>
<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>version</span><span style="color:#999999">></span></span>3.1.2<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>version</span><span style="color:#999999">></span></span>
<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"><</span>scope</span><span style="color:#999999">></span></span>compile<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>scope</span><span style="color:#999999">></span></span>
<span style="color:#e45649"><span style="color:#e45649"><span style="color:#999999"></</span>dependency</span><span style="color:#999999">></span></span>
</code></span></span>
- 自动配好SpringMVC- 引入SpringMVC全套组件- 自动配好SpringMVC常用组件(功能)
- 自动配好Web常见功能,如:字符编码问题- SpringBoot帮我们配置好了所有web开发的常见场景
<span style="color:#000000"><span style="background-color:#fafafa"><code class="language-java"><span style="color:#0077aa">public</span> <span style="color:#0077aa">static</span> <span style="color:#0077aa">void</span> <span style="color:#dd4a68">main</span><span style="color:#999999">(</span>String<span style="color:#999999">[</span><span style="color:#999999">]</span> args<span style="color:#999999">)</span> <span style="color:#999999">{</span>
<span style="color:#708090">//1、返回我们IOC容器</span>
ConfigurableApplicationContext run <span style="color:#a67f59">=</span> SpringApplication<span style="color:#999999">.</span><span style="color:#dd4a68">run</span><span style="color:#999999">(</span>MainApplication<span style="color:#999999">.</span><span style="color:#0077aa">class</span><span style="color:#999999">,</span> args<span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#708090">//2、查看容器里面的组件</span>
String<span style="color:#999999">[</span><span style="color:#999999">]</span> names <span style="color:#a67f59">=</span> run<span style="color:#999999">.</span><span style="color:#dd4a68">getBeanDefinitionNames</span><span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#0077aa">for</span> <span style="color:#999999">(</span>String name <span style="color:#a67f59">:</span> names<span style="color:#999999">)</span> <span style="color:#999999">{</span>
System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#dd4a68">println</span><span style="color:#999999">(</span>name<span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
<span style="color:#999999">}</span>
</code></span></span>
这里截取了一部分,可以看出里面SpringBoot真的很强大,不需要SSM一样导入一堆依赖。
- 默认的包结构 - 主程序所在包及其下面的所有子包里面的组件都会被默认扫描进来- 无需以前的包扫描配置- 想要改变扫描路径 - @SpringBootApplication(scanBasePackages=“com.dragon”)- @ComponentScan 指定扫描路径
<span style="color:#000000"><span style="background-color:#fafafa"><code class="language-java"><span style="color:#999999">@SpringBootApplication</span>
等同于
<span style="color:#999999">@SpringBootConfiguration</span>
<span style="color:#999999">@EnableAutoConfiguration</span>
<span style="color:#999999">@ComponentScan</span><span style="color:#999999">(</span><span style="color:#50a14f">"com.lun"</span><span style="color:#999999">)</span>
</code></span></span>
- 各种配置拥有默认值- 默认配置最终都是映射到某个类上,如:
MultipartProperties
- 配置文件的值最终会绑定每个类上,这个类会在容器中创建对象 - 按需加载所有自动配置项- 非常多的starter- 引入了哪些场景这个场景的自动配置才会开启- SpringBoot所有的自动配置功能都在 spring-boot-autoconfigure 包里面
下面是参考文档,有兴趣的朋友可以看看:
Spring Boot Maven 插件文档
中文文档——SpringBoot入门
总结
以上就是SpringBoot的特性及结构的具体讲解。
版权归原作者 码农理发 所有, 如有侵权,请联系我们删除。