SpringBoot快速入门
第一步:新建 Spring Initializr 项目:
(1)选择 Spring Initializr
(2)选择 SDK,点击【new】这里就是使用 JAVA SDK 要求版本 1.8+,选择你默认安装在 C:\Program Files\Java\jdk1.8.0_191 目录:
(3)然后选择默认的 url (不用管)点击【Next】:
(4)然后填写一下项目的信息
(5)先勾选上 Web 依赖:
(6)勾选 SQl 下的 MySQL,MyBatis 依赖:
(根据自己的项目选择,后面可加)
(7)选择好项目的位置,点击【Finish】:
(8)如果是第一次配置 Spring Boot 的话可能需要等待一会儿 IDEA 下载相应的 依赖包,默认创建好的项目结构如下:
项目结构还是看上去挺清爽的,少了很多配置文件,我们来了解一下默认生成的有什么:
SpringbootApplication: 一个带有 main() 方法的类,用于启动应用程序
SpringbootApplicationTests:一个空的 Junit 测试了,它加载了一个使用 Spring Boot 字典配置功能的 Spring 应用程序上下文
application.properties:一个空的 properties 文件,可以根据需要添加配置属性
pom.xml: Maven 构建说明文件
第二步:HelloController
在 【main/java/com.xpwi.springboot】包下新建一个【HelloController】:
package com.example.demo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 测试控制器
*/
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello() {
return "Hello Spring Boot!";
}
}
- 第三步:利用 IDEA 启动 Spring Boot
(1)我们回到 SpringbootApplication 这个类中,加入@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
然后右键点击运行:
(2)会提示 Maven 导包,点击 import
(3)注意:我们之所以在上面的项目中没有手动的去配置 Tomcat 服务器,是因为 Spring Boot 内置了 Tomcat
等待一会儿就会看到下方的成功运行的提示信息:
(4)此时,可以看到我们的 Tomcat 运行在 8080 端口,我们来访问下面地址试一下:
http://localhost:8080/hello
此时一个简单的springboot项目就创建成功了。
Spring Boot 项目文件介绍
一、解析 pom.xml 文件:
(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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</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.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
(2)我们可以看到一个比较陌生一些的标签 ,这个标签是在配置 Spring Boot 的父级依赖:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
有了这个,当前的项目才是 Spring Boot 项目,spring-boot-starter-parent 是一个特殊的 starter ,它用来提供相关的 Maven 默认依赖,使用它之后,常用的包依赖就可以省去 version 标签。
关于具体 Spring Boot 提供了哪些 jar 包的依赖,我们可以查看本地 Maven 仓库下:\repository\org\springframework\boot\spring-boot-dependencies\2.1.0.RELEASE\spring-boot-dependencies-2.1.0.RELEASE.pom 文件来查看,挺长的…
二、应用入口类 SpringbootApplication.java
Spring Boot 项目通常有一个名为 *Application 的入口类,入口类里有一个 main 方法, 这个 main 方法其实就是一个标准的 Javay 应用的入口方法。
@SpringBootApplication 是 Spring Boot 的核心注解,它是一个组合注解,该注解组合了:@Configuration、@EnableAutoConfiguration、@ComponentScan; 若不是用 @SpringBootApplication 注解也可以使用这三个注解代替。
其中,@EnableAutoConfiguration 让 Spring Boot 根据类路径中的 jar 包依赖为当前项目进行自动配置,例如,添加了 spring-boot-starter-web 依赖,会自动添加 Tomcat 和 Spring MVC 的依赖,那么 Spring Boot 会对 Tomcat 和 Spring MVC 进行自动配置。
Spring Boot 还会自动扫描 @SpringBootApplication 所在类的同级包以及下级包里的 Bean ,所以入口类建议就配置在 grounpID + arctifactID 组合的包名下(这里为 com.xpwi.springboot 包)
三、Spring Boot 的配置文件:
Spring Boot 使用一个全局的配置文件 application.properties 或 application.yml,放置在【src/main/resources】目录或者类路径的 /config 下。
Spring Boot 不仅支持常规的 properties 配置文件,还支持 yaml 语言的配置文件。yaml 是以数据为中心的语言,在配置数据的时候具有面向对象的特征。
Spring Boot 的全局配置文件的作用是对一些默认配置的配置值进行修改。
修改 properties 配置文件实例:
(1)打开 resources 下的 application.properties,加入如下 内容:
server.port=8080
server.servlet.context-path=/cn
(2)在这里我们可以设置访问的端口,将 Tomcat 默认端口设置为 8080 (默认的不修改) ,并将默认的访问路径从 “/” 修改为 “/cn” 时,再访问 http://localhost:8080/ 是什么都没有的,此时要访问 hello 是要使用 http://localhost:8080/cn/hello
注意:如果是 yml 需要在 “:” 后加一个空格,幸好 IDEA 很好地支持了 yml 文件的格式有良好的代码提示;
我们可以自己配置多个属性
(3)使用 yml 文件作为配置文件,我们直接把 .properties 后缀的文件删掉,使用 .yml 文件来进行简单的配置
(4)在然后使用在我们的 HelloController.java 类中使用 @Value 来获取配置属性,代码(请看注释):
package com.example.demo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
//获取name
@Value("${name}")
private String name;
// 获取 age
@Value("${age}")
private Integer age;
//路径映射,对应浏览器访问的地址,访问该路径则执行下面函数
@RequestMapping("/hello")
public String hello() {
return name + ":"+ age;
}
}
(5)重启 Spring Boot ,输入地址:http://localhost:8080/hello 能看到正确的结果:
【注意】:此时如果你第一次使用 idea 出现中文乱码,解决办法:
(6)【注意】: 我们并没有在 yml 文件中注明属性的类型,而是在使用的时候定义的。你也可以在配置文件中使用当前配置:
(7)任然可以得到正确结果:
(8)【问题】: 这样写配置文件繁琐而且可能会造成类的臃肿,因为有许许多多的 @Value 注解。
可以,封装配置信息
封装配置信息使用方法:
(1)application.yml 文件内容如下:
(2)新建 author.java 和 HelloController.java 同级,内容如下:
package com.example.demo;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "author")
public class author {
private String name;
private Integer age;
public author() {
}
public author(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
(3)此时可能会报错,如果报错,打开 pom.xml 文件,添加内容(注意位置):
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
(4)重新编写 HelloController 类,内容如下:
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
//获取name
//@Value("${name}")
//private String name;
// 获取 age
//@Value("${age}")
//private Integer age;
@Autowired
private author author;
//路径映射,对应浏览器访问的地址,访问该路径则执行下面函数
@RequestMapping("/hello")
public String hello() {
return "名字:" + author.getName() + " 年龄:" + author.getAge();
}
}
(5)重启 Spring Boot,访问浏览器:
http://localhost:8080/hello
【提示】:我们可以把配置信息封装成一个类,首先在我们的 name 和 age 前加一个 student 前缀,然后新建一个 StudentProperties 的类用来封装这些信息,并用上两个注解:
@Component:表明当前类是一个 Java Bean
@ConfigurationProperties(prefix = “author”):表示获取前缀为 author 的配置信息