SpringBoot快速入门

11次阅读

共计 2967 个字符,预计需要花费 8 分钟才能阅读完成。

SpringBoot 疾速入门

形式一:应用 idea 疾速创立 SpringBoot 我的项目(举荐)

形式二:代码实现

1. 创立 Maven 工程

应用 idea 工具创立一个 maven 工程,该工程为一般的 java 工程即可

2. 增加 SpringBoot 的起步依赖

SpringBoot 要求,我的项目要继承 SpringBoot 的起步依赖 spring-boot-starter-parent

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.1.RELEASE</version>
</parent>

SpringBoot 要集成 SpringMVC 进行 Controller 的开发,所以我的项目要导入 web 的启动依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

3. 编写 SpringBoot 疏导类

要通过 SpringBoot 提供的疏导类起步 SpringBoot 才能够进行拜访

package com.itheima;

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

@SpringBootApplication
public class MySpringBootApplication {public static void main(String[] args) {SpringApplication.run(MySpringBootApplication.class);
    }

}

4. 编写 Controller

在疏导类 MySpringBootApplication 同级包或者子级包中创立 QuickStartController

package com.itheima.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class QuickStartController {@RequestMapping("/quick")
    @ResponseBody
    public String quick(){return "springboot 拜访胜利!";}
    
}

5. 测试

执行 SpringBoot 起步类的主办法,控制台打印日志如下:

.   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
(()\___ | '_ |'_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.1.RELEASE)

2018-05-08 14:29:59.714  INFO 5672 --- [main] com.itheima.MySpringBootApplication      : Starting MySpringBootApplication on DESKTOP-RRUNFUH with PID 5672 (C:\Users\muzimoo\IdeaProjects\IdeaTest\springboot_quick\target\classes started by muzimoo in C:\Users\muzimoo\IdeaProjects\IdeaTest)
... ... ...
o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-05-08 14:30:03.126  INFO 5672 --- [main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-05-08 14:30:03.196  INFO 5672 --- [main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2018-05-08 14:30:03.206  INFO 5672 --- [main] com.itheima.MySpringBootApplication      : Started MySpringBootApplication in 4.252 seconds (JVM running for 5.583)

通过日志发现,Tomcat started on port(s): 8080 (http) with context path ”

tomcat 曾经起步,端口监听 8080,web 利用的虚构工程名称为空

关上浏览器拜访 url 地址为:http://localhost:8080/quick

一些问题

1. 创立我的项目失败

提醒 无奈连贯到 https://start.spring.io/ 或 连贯超时

解决方案

①控制面板零碎和平安 Windows 防火墙容许利用通过防火墙,把 IDEA 勾上

②更改 IDEA 设置

点击 IDEA setting 之后,找到 Http Proxy 抉择 Atuo-detect proxy settings 之后点击 check connection

③还不胜利 更换网络(网线 /wifi/ 热点)

2. 工程热部署

咱们在开发中重复批改类、页面等资源,每次批改后都是须要重新启动才失效,这样每次启动都很麻烦,节约了大量的工夫,咱们能够在批改代码后不重启就能失效,在 pom.xml 中增加如下配置就能够实现这样的性能,咱们称之为热部署。

<!-- 热部署配置 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
</dependency>

留神:IDEA 进行 SpringBoot 热部署失败起因

呈现这种状况,并不是热部署配置问题,其根本原因是因为 Intellij IEDA 默认状况下不会主动编译,须要对 IDEA 进行主动编译的设置,如下:

而后 Shift+Ctrl+Alt+/,抉择 Registry

正文完
 0