SpringBoot环境搭建详解一
目录
- 更多分享www.catbro.cn
一、前言:
- 我们用Java开发一个简单的“Hello World!”Web应用程序,重点介绍Spring Boot的一些主要特性。我们将使用Maven来构建这个项目,因为大多数IDE都支持它。
一、环境搭建
-
检查jdk环境,未安装自行安装:
$ java -version java version "1.8.0_73" Java(TM) SE Runtime Environment (build 1.8.0_73-b02) Java HotSpot(TM) 64-Bit Server VM (build 25.73-b02, mixed mode)
-
检查maven:
Apache Maven 3.5.2 (138edd61fd100ec658bfa2d307c43b76940a5d7d; 2017-10-18T15:58:13+08:00) Maven home: /usr/local/Cellar/maven/3.5.2/libexec Java version: 1.8.0_73, vendor: Oracle Corporation Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_73.jdk/Contents/Home/jre Default locale: zh_CN, platform encoding: UTF-8 OS name: "mac os x", version: "10.13.2", arch: "x86_64", family: "mac" guanguandeMacBook-Pro:~ zaizai$
-
创建springboot项目
-
1、 通过https://start.spring.io直接快速创建项目
-
2、手动搭建
-
maven pom 配置
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com</groupId> <artifactId>aircraft</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>aircraft</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </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-starter-web </artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
-
创建好文件目录,在src下
-
src/main/java :存放源代码
-
src/main/resources :存放资源文件
-
src/main/resources/static:存放静态资源
-
src/main/resources/templates:存放模版文件
-
src/main/resources 中创建application.properties文件作为配置文件
-
在src/main/java 对应的包目录下创建一个application类,作为springBoot的配置管理类,也是主入口:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class AircraftApplication { public static void main(String[] args) { SpringApplication.run(AircraftApplication.class, args); } }
三、热加载配置
-
spring 官方为我们提供了非常便捷的配置热部署的方案,你只需要在maven配置文件中添加
-
更多分享:www.catbro.cn
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> </dependencies>
-
spring.devtools.restart.exclude=static/,public/
-
配置那些文件夹下的变化会引起改变,默认/META-INF/maven, /META-INF/resources, /resources, /static, /public or /templates下的文件的变化都不会引起重启
-
ok,结构基本出来了