SpringBoot学习:IDEA中快速搭建springboot项目

时间:2022-06-20 00:49:15

项目下载地址:http://download.csdn.net/detail/aqsunkai/9805821

(一)IDEA中创建maven web项目

SpringBoot学习:IDEA中快速搭建springboot项目SpringBoot学习:IDEA中快速搭建springboot项目SpringBoot学习:IDEA中快速搭建springboot项目

创建好项目后设置项目的编译路径:

SpringBoot学习:IDEA中快速搭建springboot项目

(二)引入spring-boot项目所需的jar包:

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-boot.version>1.5.2.RELEASE</spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<!-- spring-web, spring-webmvc Spring WebMvc框架 -->
<!-- tomcat-embed-* 内嵌Tomcat容器 -->
<!-- jackson 处理json数据 -->
<!-- spring-* Spring框架 -->
<!-- spring-boot-autoconfigure Spring Boot提供的自动配置功能 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring-boot.version}</version>
<!--<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>-->
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>${spring-boot.version}</version>
</dependency>
</dependencies>
<build>
<finalName>springBoot</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
(三)配置文件、启动类:

项目路径如下:

SpringBoot学习:IDEA中快速搭建springboot项目

application.yml配置文件中内容为:

#server
server:
# 项目contextPath,一般在正式发布版本中,我们不配置
context-path: /boot
# 该服务绑定IP地址,启动服务器时如本机不是该IP地址则抛出异常启动失败,只有特殊需求的情况下才配置
#address: 192.168.0.101
# 错误页,指定发生错误时,跳转的URL。请查看BasicErrorController源码便知
error:
path: /error
# 服务端口
port: 8080
# session最大超时时间(分钟),默认为30
session:
timeout: 60
tomcat:
# tomcat的URI编码
uri-encoding: utf-8
# tomcat最大线程数,默认为200
max-threads: 1000
# 存放Tomcat的日志、Dump等文件的临时文件夹,默认为系统的tmp文件夹(如:C:\Users\Shanhy\AppData\Local\Temp)
basedir: G:/springboot-tomcat-tmp
# 打开Tomcat的Access日志,并可以设置日志格式的方法:
#server.tomcat.access-log-enabled=true
#server.tomcat.access-log-pattern=
Application.java启动类如下,spring会扫描该类所在目录下的java类:

package com.sun;

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

/**
* @Configuration:标注此文件为一个配置项
* @EnableAutoConfiguration:使用自动配置
* @ComponentScan:可扫描的
* @SpringBootApplication 包含上面三个
* Application:启动管理器
* Created by sun on 2017-1-14.
*/
@SpringBootApplication
public class Application implements CommandLineRunner {

/* Servlet容器默认的Context路径是/
DispatherServlet匹配的路径(servlet-mapping中的url-patterns)是*//*
@ComponentScan路径被默认设置为SampleController的同名package,
也就是该package下的所有@Controller,@Service, @Component, @Repository都会被实例化后并加入Spring Context中。*/
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(Application.class);
springApplication.run(args);
}

// springboot运行后此方法首先被调用
// 实现CommandLineRunner抽象类中的run方法
@Override
public void run(String... args) throws Exception {
System.out.println("项目启动完成!");
}
}
TestController类:

package com.sun.test.controller;

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

import javax.servlet.http.HttpServletRequest;

/**
* 测试类
* Created by sun on 2017-1-14.
*/
@Controller
@RequestMapping("/test")
public class TestController {

@RequestMapping("/")
@ResponseBody
String test(HttpServletRequest req){
return "Hello World!";
}

}

启动Application,在页面上输入http://localhost:8080/boot/test/ 页面上打印出Hello World!