1. 新建普通maven工程
2. 在pom.xml中添加父
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
此处需要注意artifactId
的内容不能错:spring-boot-starter-parent
(刚开始写成了spring-boot-start
-parent,导致一直报错…)
PS:如果写正确了还是报错,可能是镜像源的问题,可以更换为阿里云镜像
3. 导入web启动依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.1.6.RELEASE</version>
</dependency>
</dependencies>
4.创建引导类MySpringBootApplication
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//添加该注解,表明这个是Springboot的引导类
@SpringBootApplication
public class MySpringBootApplication {
public static void main(String[] args) {
//启动
SpringApplication.run(MySpringBootApplication.class);
}
}
运行结果
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.0.RELEASE)
2019-10-07 22:35:17.974 INFO 14756 --- [ main] com.imxf.MySpringBootApplication : Starting MySpringBootApplication on DESKTOP-0DD8SS2 with PID 14756 (D:\JAVA\springbootquickstart\target\classes started by IMXF in D:\JAVA\springbootquickstart)
2019-10-07 22:35:17.976 INFO 14756 --- [ main] com.imxf.MySpringBootApplication : No active profile set, falling back to default profiles: default
2019-10-07 22:35:19.810 INFO 14756 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2019-10-07 22:35:19.838 INFO 14756 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-10-07 22:35:19.838 INFO 14756 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/9.0.12
2019-10-07 22:35:19.877 INFO 14756 --- [ main] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [D:\Program Files (x86)\Java\jdk-10.0.2\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\dotnet\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\Git\cmd;D:\Program Files\MATLAB\R2018a\runtime\win64;D:\Program Files\MATLAB\R2018a\bin;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;D:\Program Files (x86)\java\jdk-10.0.2\bin;E:\JAVA\apache-maven-3.6.1\bin;;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;D:\Program\Anaconda3;D:\Program\Anaconda3\Library\mingw-w64\bin;D:\Program\Anaconda3\Library\usr\bin;D:\Program\Anaconda3\Library\bin;D:\Program\Anaconda3\Scripts;E:\Python\Scripts\;E:\Python\;D:\Program Files (x86)\java\jdk-10.0.2\bin;D:\Program Files (x86)\VC\Tools\WinNT;D:\Program Files (x86)\VC\MSDev98\Bin;D:\Program Files (x86)\VC\Tools;D:\Program Files (x86)\vc6.0\VC98\bin;C:\Users\IMXF\AppData\Local\Microsoft\WindowsApps;E:\java\mysql-5.7.25-winx64\bin;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;.]
2019-10-07 22:35:20.170 INFO 14756 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-10-07 22:35:20.170 INFO 14756 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2129 ms
2019-10-07 22:35:20.198 INFO 14756 --- [ main] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/]
2019-10-07 22:35:20.204 INFO 14756 --- [ main] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2019-10-07 22:35:20.205 INFO 14756 --- [ main] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2019-10-07 22:35:20.205 INFO 14756 --- [ main] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'formContentFilter' to: [/*]
2019-10-07 22:35:20.205 INFO 14756 --- [ main] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2019-10-07 22:35:20.497 INFO 14756 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-10-07 22:35:20.732 INFO 14756 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2019-10-07 22:35:20.736 INFO 14756 --- [ main] com.imxf.MySpringBootApplication : Started MySpringBootApplication in 3.533 seconds (JVM running for 10.457)
2019-10-07 22:35:49.906 INFO 14756 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2019-10-07 22:35:49.906 INFO 14756 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2019-10-07 22:35:49.916 INFO 14756 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 10 ms
可以看到Springboot成功启动
Tomcat started on port(s): 8080 (http) with context path ''
在浏览器访问http://localhost:8080/
5.编写Controller
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class QuickStartCtrler {
@RequestMapping("/quickstart")
@ResponseBody
public String quickstart(){
return "Hello SpringBoot";
}
}
在浏览器访问http://localhost:8080/quickstart
6.热部署
- 在pom.xml中添加依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency>
- 由于IDEA本身的问题,需要修改设置
按住shift + alt + ctrl + /
然后勾选compiler.automake.allow.app.running
7.IDEA快速创建SpringBoot工程
-
创建新Project/Module
-
填写Project/Module的相关基础内容
-
这里可以勾选你项目里面所需要的 依赖(Dependencies), 还有Spring Boot版本(右上角),我只选了简单的web
-
点击 Finish 完成项目创建
-
项目结构
-
pom文件内容,可以看见IDEA为我们自动添加的依赖等
···
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.imxf</groupId>
<artifactId>springboot_quickstart</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot_quickstart</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>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>