0
点赞
收藏
分享

微信扫一扫

Spring Boot环境架构与基本开发

guanguans 2022-07-12 阅读 53


对Spring Boot的开发环境进行搭建,并且对他的特点做进一步的了解,才能更好的对Spring
Boot深入了解。无论如何都需要先来搭建Spring Boot的工程。

1.通过IDEA 搭建Spring Boot的开发环境

首先启动IDEA,然后选择Create New Project,就可以看到一个新的窗口。选择Spring
Initializer,并将JDK切换为你想要的版本。

Spring Boot环境架构与基本开发_maven


点击next弹出另一个窗口,他将允许我们进行一定的配置。

Spring Boot环境架构与基本开发_mvc_02

之后可以根据自己的需要选择对应的starter进行依赖,IDEA会帮你建好工程。你可以看到一个建好的类,BootAllApplication和maven的pom.xml文件。运行BootAllApplication就可以启动Spring
Boot工程,而pom.xml则配置好了你选择的starter依赖。这样就可以基于IDEA开发 Spring Boot工程了。

  1. Spring Boot的依赖和自动配置

为什么他在很少的配置之下就可以运行? 下面以最常见的Spring
MVC为例说明。打开本地仓库的spring-boot-starter-web.pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starters</artifactId>
<version>2.2.4.RELEASE</version>
</parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.2.4.RELEASE</version>
<name>Spring Boot Web Starter</name>
<description>Starter for building web, including RESTful, applications using Spring
MVC. Uses Tomcat as the default embedded container</description>
<url>https://projects.spring.io/spring-boot/#/spring-boot-parent/spring-boot-starters/spring-boot-starter-web</url>
<organization>
<name>Pivotal Software, Inc.</name>
<url>https://spring.io</url>
</organization>
<licenses>
<license>
<name>Apache License, Version 2.0</name>
<url>https://www.apache.org/licenses/LICENSE-2.0</url>
</license>
</licenses>
<developers>
<developer>
<name>Pivotal</name>
<email>info@pivotal.io</email>
<organization>Pivotal Software, Inc.</organization>
<organizationUrl>https://www.spring.io</organizationUrl>
</developer>
</developers>
<scm>
<connection>scm:git:git://github.com/spring-projects/spring-boot.git</connection>
<developerConnection>scm:git:ssh://git@github.com/spring-projects/spring-boot.git</developerConnection>
<url>https://github.com/spring-projects/spring-boot</url>
</scm>
<issueManagement>
<system>Github</system>
<url>https://github.com/spring-projects/spring-boot/issues</url>
</issueManagement>
<dependencies>
<dependency>
<!--Spring Boot的依赖-->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.2.4.RELEASE</version>
<scope>compile</scope>
</dependency>
<!--JSON依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
<version>2.2.4.RELEASE</version>
<scope>compile</scope>
</dependency>
<!--Tomcat依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>2.2.4.RELEASE</version>
<scope>compile</scope>
</dependency>
<!-- Validator依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<version>2.2.4.RELEASE</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>tomcat-embed-el</artifactId>
<groupId>org.apache.tomcat.embed</groupId>
</exclusion>
</exclusions>
</dependency>
<!--SPring Web依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.3.RELEASE</version>
<scope>compile</scope>
</dependency>
<!--Spring Web MVC依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.3.RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>

代码注释自己加上,这里可以看出,加入spring-boot-starter-web之后,他会通过Maven将对应的资源加载到我们的工程中,这样便能够形成依赖。但是还不足以运行Spring
MVC项目,还需要对他进行配置,让压能够生产Spring MVC所需的对象,才能启用Spring MVC.

  1. 使用自定义配置

如果你按照IDEA来开发,那么你会发现一个application.properties属性文件。
他是一个默认的配置文件,通过它可以根据需要实现自定义。比如设置端口为8240,只需要添加一行。

server.port=8240

日志启动会有端口提示

Tomcat started on port(s): 8240 (http) with context path ''

我们只需要修改配置文件,就可以将默认配置变为自定义配置。 事实上,SPring
Boot的参数配置除了使用properties之外还有yml。他们按照如下优先级顺序加载:

  • 命令行参数(比如Dockerfile的run)
  • application-{profile}.properties或者yml
  • application.properties或者yml
  • @Configuration注解类上的@PropertySource 实际上,properties与yml的文件只是缩进和简写的区别,但是主流开发是yml
  1. 开发自己的Spring Boot项目

Spring
MVC视图解析器(ViewREsolver)的作用就是定位视图,也就是当控制器只返回一个逻辑名称的时候,是没办法找到视图的,这就需要视图解析器进行解析了。

- 新增JSP和JSTL的maven依赖

<!--JSP依赖-->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<!--JSTL标签依赖-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>

为了配置视图解析器(ViewResolver),将application.properties文件修改为

  • 定义视图后缀,前缀固定

server:
port: 8240
spring:
mvc:
view:
suffix: .html

这里的suffix是Spring Boot与我们约定的视图后缀配置,意思是找到resource底下的以.html后缀的html文件,那么前缀后缀之间很显然少一个文件名称,在Spring MVC机制中,这个名称是有控制器(controller给出的),为此新建一个控制器

@Controller
public class ControllerTest {
@RequestMapping("/index")
public String index(){
return "index";
}
}

另外在static之下,新建一个index.html文件,什么也不用写

Spring Boot环境架构与基本开发_spring_03

这样就完成了让视图解析器找到视图的功能。 然后看到即将运行的BootAllApplication.java文件

package cn.hctech2006.boot.bootall;

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

@SpringBootApplication
public class BootAllApplication {

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

}

这里的注解@SpringBootApplication标志着这是一个Spriing
Boot入门文件。加粗的代码则是以BootAllApplication类作为配置类来运行Spring Boot项目的,于是Spring
Boot就会根据你在Maven加载的依赖完成运行。

我们是以JavaApplication的形式运行类BootAllApplication.java。运行结果如下:

Spring Boot环境架构与基本开发_spring_04


举报

相关推荐

0 条评论