Spring Boot 之 IDEA 搭建 Spring Boot 项目
- 一、SpringBoot 搭建Web项目
- 1.1 SpringBoot 项目搭建(使用Spring Initializr)
- 1.2 SpringBoot 项目搭建(使用MAVEN)
- 1.3 项目工程介绍
- 1.4 运行项目
- 二、Spring Boot 项目示例
- 2.1 导入SpringBoot的依赖
- 2.2 编写SpringBoot代码
- 三、附(常用依赖)
一、SpringBoot 搭建Web项目
1.1 SpringBoot 项目搭建(使用Spring Initializr)
1.1.1 创建项目
新建项目,选择Spring Initializr
1.1.2 配置JDK版本和Initializr Service URL
第一项是选择JDK版本,下面的Initializr Service URL是用来查询Spring Boot的当前版本和组件的网站。1.1.3 配置Project Metadata信息
Project Metadata配置信息包含如下内容
- Group:项目组织的标识符。
- Artifact:项目标识符。
- Type:构建项目的方式,包含Maven和Gradle,这里选择Maven Project。
- Language:编程语言,这里选择Java。
- Packaging:启动形式,包含JAR和WAR,这里我们选择JAR。
- Java Version:Java版本。
- Version:项目版本号。
- Name:项目名称。
- Description:项目描述。
- Package:实际对应Java包的结构,是main目录里Java的目录结构。
1.1.4 配置Spring Boot版本及默认引入组件
在Spring Boot下拉框中选择当前推荐的Spring Boot版本,在下方选择要使用的组件,然后单击Next按钮
1.1.5 配置项目名称和项目位置
在Project Name处配置项目名称,在Project Location处配置项目位置
创建好的Spring Boot 项目如图所示
1.2 SpringBoot 项目搭建(使用MAVEN)
1.2.1 创建一个Maven项目
创建项目,选择Maven项目,在右侧选择自己的jdk,下面的快速创建模板可以勾选也可以不勾选。
1.2.2 填写项目的GroupId和ArtifactId
- GroupId:项目组织唯一的标识符,实际对应JAVA的包的结构,是main目录里java的目录结构。
- ArtifactId:项目的唯一的标识符,实际对应项目的名称,就是项目根目录的名称。
1.2.3 修改项目名和存放地址
创建好的项目结构如下
1.3 项目工程介绍
1.3.1 Java类文件
src/main/java
下用于放置Java类文件,由于这是一个新建的项目,因此目前只有一个DemoApplication类,如图所示。
这个类是Spring Boot应用的主程序,其中@SpringBootApplication
注解用来说明这是Spring Boot应用的启动类,其中包含自动配置、包扫描等功能,main方法是启动应用的入口方法,命令行或者插件等任何方式启动,都会调用这个方法。1.3.2 资源文件
(1)配置文件
src/main/resources
下面主要用于放置Spring Boot应用的配置文件,新建项目的时候会默认创建一个application.properties
(默认是一个空文件),也可以将.properties
文件修改为.yml
文件,用缩进结构的键值对来进行配置。
(2)静态资源
src/main/resources/static
下面主要放置应用的静态资源文件,如HTML、JavaScript、图片等。
(3)模板文件src/main/resources/templates
下面主要放置应用的模板文件,比如使用Thymeleaf后的Thymeleaf模板文件等。
1.3.3 测试类文件
src/test/java
下用于放置Spring Boot测试类文件,默认会根据项目名称创建一个测试类,如图所示。打开该类可以发现@SpringBootTest
注解用于标明这是一个Spring Boot测试类。
1.3.4 pom文件
项目中还包含一个pom.xml文件,这是Maven项目用于构建项目的重要组成部分。从pom文件的完整代码中可以看到新建的Spring Boot项目默认的依赖以及版本号、Java版本等
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.test</groupId>
<artifactId>springbootdemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springbootdemo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
1.4 运行项目
运行项目下的SpringbootdemoApplication
即可启动项目
启动项目过程和结果如下
二、Spring Boot 项目示例
2.1 导入SpringBoot的依赖
利用上面使用Spring Initializr创建的Spring Bootw项目进行Web项目示例
2.1.1 导入jar包
SpringBoot的相关依赖在其官网可以查到,找到后在pom.xml文件中导入。
(1)spring-boot-starter-parent
依赖
这是一个依赖管理器的pom文件,其作用是管理SpringBoot相关的所有依赖,统一各种依赖jar包的版本号,避免在手动导入其他依赖出现版本不一致的问题,所以在引入其他的依赖时就可以省略版本号。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.13.RELEASE</version>
</parent>
spring-boot-starter-parent
是一个父项目,一般是用来进行依赖管理的,这个父项目还有一个父项目spring-boot-dependencies
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.13.RELEASE</version>
<relativePath>../../spring-boot-dependencies</relativePath>
</parent>
在spring-boot-dependencies
这个夫项目里面,规定了所有要使用的依赖的版本,所以它才是真正对所有依赖进行管理的父项目。
(2)配置源代码编码格式、输出编码格式、jdk版本
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
(3)spring-boot-starter-web
依赖:
这个依赖会自动嵌入tomcat容器,添加了这个依赖,表示支持全栈式Web开发,包括Tomcat和spring-webmvc。并且springboot也会根据classpath中的dependency来自动配置。比如:spring-boot-starter-web
会自动装配tomcat容器,并且自动从配置文件application.properties
中读取web应用的相关配置,如:server.port。当配置文件application.properties
没有配置相关的参数,则采用默认的配置信息,如:没有配置Tomcat的端口,spring-boot-starter-web
会自动采用其默认的端口号8080。
<!-- 包含 mvc,aop 等jar资源 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
2.2 编写SpringBoot代码
2.2.1 编写 SpringBoot 启动器
SpringBoot 项目中内置了 Tomcat 服务器,所以和 web 项目启动方式有很大的差别,以前是将项目发布到服务器中,通过运行服务器来运行 web 项目。在 SpringBoot 项目中需要添加主类作为 SpringBoot 启动器,启动器最好放到所有控制层、业务层、数据层的父包中,因为 SpringBoot 对于Spring的扫包地址默认为启动器所在的目录下。
启动器SpringBootStarter类的代码如下
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class SpringBootStarter {
public static void main(String[] args) {
SpringApplication.run(SpringBootStarter.class,args);
}
}
注解说明:@SpringBootApplicationL
:这是一个复合注解,包括@ComponentScan
,和@SpringBootConfiguration
,@EnableAutoConfiguration
。
-
@ComponentScan
,扫描当前包及其子包下被@Component
,@Controller
,@Service
,@Repository
注解标记的类并纳入到 Spring 容器中进行管理。和以前的<context:component-scan>
的作用一致。 -
@SpringBootConfiguration
继承自@Configuration
,二者功能也一致,标注当前类是配置类,并会将当前类内声明的一个或多个以@Bean
注解标记的方法的实例纳入到 Spring 容器中,并且实例名就是方法名。 -
@EnableAutoConfiguration
的作用是启动自动配置,@EnableAutoConfiguration
注解的意思就是 Springboot 根据你添加的jar包来配置你项目的默认配置,比如根据 spring-boot-starter-web ,来判断你的项目是否需要添加了 webmvc 和 tomcat,就会自动的帮你配置 web 项目中所需要的默认配置。 -
@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})
注解中的 exclude 属性,是指在启动时不加载该类,该类会自动去查找 DataSource 的配置,但是由于本案例中还未涉及到数据源的配置所以启动时会报错,所以取消加载该类。在以后的项目中不需要写该属性。
2.2.2 编写页面
在resources文件夹中新建static
文件夹和templates
文件夹,static
文件夹用于存放静态资源,如js、css、图片等等。而templates
文件夹用于保存 html 文件。
templates文件夹下新建index.html内容如下
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>SpringBoot</title>
</head>
<body>
Hello,SpringBoot!
</body>
</html>
完成后的项目简单结构如下
运行启动器SpringBootStarter类,结果如下
在浏览器中访问http://localhost:8080/index.html
,或者http://localhost:8080
,都可以得到下面的结果
注意:由于 SpringBoot 默认项目在服务器中的访问路径为/
,所以访问项目不再需要项目名,如果你要修改项目的访问路径,可以在 application.properties 中添加配置
server.servlet.context-path=/springbootdemo
在 SpringBoot2.x 以下的版本,该属性为server.context-path=/springbootdemo
,但是根据实际需求,项目的访问路径最好设置为/
,因为这样设置更利于网页中使用绝对路径来请求资源。
2.2.3 编写java代码
除了上面的访问页面,我们可以再测试一下访问java代码,具体实现如下:
(1)再controller层创建一个文件,简单编写几句代码,这里只是简单的测试,所以就不写server层等的代码了。
@Controller
public class SpringBootTest {
@GetMapping("/test")
@ResponseBody
public String test(){
return "This is SpringBoot !!!";
}
}
(2)将页面index.html也做相应的修改,使之能够访问后端java代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>SpringBoot</title>
<script src="../js/jquery-3.4.1.min.js"></script>
</head>
<body>
<button onclick="show()">Click Me!</button>
<h id="test"></h>
</body>
<script>
function show() {
$.ajax({
url: "test",
type:"get",
success: function (data) {
$("#test").html(data)
}
})
}
</script>
</html>
页面显示如下
当点击按钮时,页面会发送ajax请求访问后端代码,并得到返回结果如下
三、附(常用依赖)
要实现SpringBoot项目,除了上面的依赖,还可以导入其他的相关依赖,下面列举一下常用的依赖:
- Maven插件相关的配置
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
-
spring-boot-devtools
依赖
热部署依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
<scope>runtime</scope>
</dependency>
如果配置了热部署,还要加入下面的配置,否则热部署不生效
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!-- 没有该配置,热部署不生效 -->
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
-
spring-boot-starter-data-jpa
依赖
这个是数据库连接的依赖,可以使用数据持久层框架 Spring Data JPA 操作数据源,支持JPA(Java Persistence API. ,包括spring-data-jpa、spring-orm、Hibernate)。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
-
fastjson
依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.15</version>
</dependency>
-
Mysql
依赖
mysql驱动
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
-
spring-boot-starter-jdbc
依赖
对JDBC数据库的支持
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
-
spring-boot-starter-thymeleaf
依赖
thymeleaf模板引擎
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
-
spring-boot-starter-security
依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>-->
-
spring-boot-starter-aop
依赖
对于spring-aop的支持包括aspects包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
-
mybatis-spring-boot-starter
依赖
mybatis的支持 包含mybatis的包和mybatis-spring插件包
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
-
spring-boot-starter-test
依赖
支持常规的测试依赖,包括JUnit、Hamcrest、Mockito以及spring-test模块。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
-
spring-boot-starter-security
依赖
支持spring-security
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
-
spring-boot-starter-log4j
依赖
支持Log4J日志框架
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
</dependency>
-
spring-boot-starter-tomcat
依赖
<!--springboot中集成jsp需要用到下面三个依赖,需要注意的是scope属性会影响项目打成war包-->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
-
spring-boot-starter-loggingt
依赖
引入了Spring Boot默认的日志框架Logback
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
-
spring-boot-starter-websockett
依赖
支持WebSocket开发
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
-
spring-boot-starter-velocityt
依赖
支持Velocity模板引擎
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-velocity</artifactId>
</dependency>
-
spring-boot-starter-redist
依赖
支持Redis键值存储数据库,包括spring-redis
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
</dependency>
-
spring-boot-starter-groovy-templatest
依赖
支持Groovy模板引擎
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-templates</artifactId>
</dependency>
-
spring-boot-starter-freemarkert
依赖
支持FreeMarker模板引擎
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
-
spring-boot-starter-data-mongodbt
依赖
支持MongoDB数据,包括spring-data-mongodb
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mongodb</artifactId>
</dependency>