设为首页 加入收藏

TOP

SpringBoot系列一:SpringBoot入门(二)
2018-11-21 14:08:43 】 浏览:341
Tags:SpringBoot 系列 入门
tializr,右边菜单选择 JDK 版本,然后点击Next

3) 填写 Maven 坐标 GroupId/ArtifactId、打包形式(本例使用Jar的方式,war包形式请看后续文章)、项目名称/版本等基本信息,然后点击Next

4) 选择 SpringBoot 版本,以及需要依赖的包,这里我们选择最新版2.1.0,导入Web模块依赖包,点击Next

5) 修改项目名称以及项目存放路径,默认即可,点击 Finish

2.2 SpringBoot项目结构分析

通过 2.1 的讲解,我们熟悉了如何快速创建 SpringBoot 项目。接下来分析 SpringBoot 项目结构,上节中创建好的 SpringBoot 项目结构如下:

2.2.1 pom.xml文件
2.2.1.1 父项目
<parent>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-parent</artifactId>
   <version>2.1.0.RELEASE</version>
   <relativePath/> <!-- lookup parent from repository -->
</parent>

按住Ctrl,点击 spring-boot-starter-parent 进入 spring-boot-starter-parent 项目,它的父项目是:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.1.0.RELEASE</version>
    <relativePath>../../spring-boot-dependencies</relativePath>
</parent>

如上,可以看到 spring-boot-dependencies。它是真正管理 SpringBoot 项目所有依赖版本,是 Spring Boot 的版本仲裁中心,以后我们导入依赖默认是不需要写版本(没有在 dependencies 里面管理的依赖自然需要声明版本号)。

2.2.1.2 启动器(Starter)
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
</dependency>

spring-boot-starter:spring-boot 场景启动器。帮我们导入了 Web 模块正常运行所依赖的组件。Spring Boot 将所有的功能场景都抽取出来,做成许多启动器(starter),只需要在项目里面引入这些启动器,相关场景的所有依赖都会导入进来。要用什么功能就导入什么场景的启动器。

1) 以下列举 SpringBoot 官方提供的 starter pom

更多请参考 “spring-boot-reference.pdf” 2.1.0.RELEASE,13.5节Starters。

2) 除了官方的 starter pom 外,还有第三方为 SpringBoot 所写的 starter pom

如下表所示:

2.2.2 resources文件目录结构
  1. static:保存所有的静态资源,如 js、css、images 等。
  2. templates:保存所有的模板文件,SpringBoot 默认 jar 使用嵌入式的 Tomcat,默认不支持 JSP 页面,可以使用模板引擎(Thymeleaf、Freemarker )。
  3. application.properties:SpringBoot 应用配置文件,可以修改一些默认的配置,如修改 Tomcat 端口,项目根路径等。
2.2.3 主程序类

打开2.1节创建的 springboot-demo 项目,找到 SpringBootDemoApplication 类:

package com.seagetech.springbootdemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringbootDemoApplication {

   public static void main(String[] args) {
      SpringApplication.run(SpringbootDemoApplication.class, args);
   }
}

基本和1.3.3节创建的主程序类一样,main 方法加注解的形式,不同的是,Idea 创建的主程序类使用的是 @SpringBootApplication 注解,它是 SpringBoot 的核心注解,也是一个组合注解,打开这个注解,源码如下:

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan

@SpringBootApplication 注解的核心功能其实是:@SpringBootConfiguration、@EnableAutoConfigration、@ComponentScan 三个注解提供。有关这几个注解的详解,请阅读 Spring Boot 系列二:SpringBoot自动配置原理

首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Java反转字符串的10种方法(代码.. 下一篇谈谈 Java 类加载机制

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目