springboot学习之构建简单项目搭建

Posted 厨房小码农

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot学习之构建简单项目搭建相关的知识,希望对你有一定的参考价值。

概述

  相信对于Java开发者而言,spring和springMvc两个框架一定不陌生,这两个框架需要我们手动配置的地方非常多,各种的xml文件,properties文件,构建一个项目还是挺复杂的,在这种情况下,springboot应运而生,他能够快速的构建spring项目,而且让项目正常运行起来的配置文件非常少,甚至只需要几个注解就可以运行整个项目。

  总的说来,springboot项目可以打成jar包独立运行部署,因为它内嵌servlet容器,之前spring,springMvc需要的大量依赖,可以通过starter来帮助我们简化配置,当然还有其他好多优点,这里就不一一赘述,小伙伴们可以自行搜索解答。

 

简单项目构建

  工具

    eclipse maven

  首先,我们新建一个maven项目,在eclipse左侧右击选择new----》other,选择新建Maven project

输入group Id,artifact Id,点击完成

这样一个简单的项目架子就完成了,但是啥都没有,项目结构如下图所示:

下面我们就开始配置搭建springboot项目。

1.添加依赖

  

完整porm代码如下:

 1 <project xmlns="http://maven.apache.org/POM/4.0.0"
 2     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5 
 6     <groupId>com.cfxmn.springboot</groupId>
 7     <artifactId>springbootDemo</artifactId>
 8     <version>0.0.1-SNAPSHOT</version>
 9     <packaging>jar</packaging>
10 
11     <!-- 通过继承spring-boot-starter-parent项目来获得一些合理的默认配置 -->
12     <parent>
13         <groupId>org.springframework.boot</groupId>
14         <artifactId>spring-boot-starter-parent</artifactId>
15         <version>1.5.6.RELEASE</version>
16     </parent>
17 
18     <properties>
19         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
20     </properties>
21 
22     <dependencies>
23         <!-- Spring Boot Web 依赖 -->
24         <dependency>
25             <groupId>org.springframework.boot</groupId>
26             <artifactId>spring-boot-starter-web</artifactId>
27         </dependency>
28 
29         <!-- Spring Boot Test 依赖 -->
30         <dependency>
31             <groupId>org.springframework.boot</groupId>
32             <artifactId>spring-boot-starter-test</artifactId>
33             <scope>test</scope>
34         </dependency>
35         <!-- 使用Lombok可以减少很多重复代码的书写。比如说getter/setter/toString等方法的编写 -->
36         <dependency>
37             <groupId>org.projectlombok</groupId>
38             <artifactId>lombok</artifactId>
39         </dependency>
40     </dependencies>
41 </project>
View Code

 

  下面我们新建一些包和添加项目的启动类,如下图所示:

其中,控制器DemoController的内容非常简单,内容如下:

package com.cfxmn.springboot.springbootDemo.controller;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import lombok.extern.slf4j.Slf4j;

@RestController
@Slf4j
public class DemoController {
	
	@PostMapping("/demo")
	public void demoTest() {
		// 这边简单起见,打印一下日志
		log.info("success call");
	}
}

可能有些同学对其中的几个注解有些疑问,我这边简单说明下,

1.RestController

  这个注解其实就是@ResponseBody + @Controller

2.PostMapping

  这个注解其实就是@RequestMapping("xxxxxx", Method=RequestMethod.POST)

这两个其实都是组合注解,简化使用

我们再来看看,项目的启动类SpringbootDemoApplication的内容:

package com.cfxmn.springboot.springbootDemo;

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

@SpringBootApplication
public class SpringbootDemoApplication {

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

}

是的,你没看错,只要运行这个main方法,就能启动这个spring项目,具体是怎么启动的容器,我们之后再分析,其实主要就是在注解SpringBootApplication上。

下面我们就来运行下,看下启动日志:

 

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

2018-10-25 23:52:41.985  INFO 1700 --- [           main] c.c.s.s.SpringbootDemoApplication        : Starting SpringbootDemoApplication on DESKTOP-KB78HJK with PID 1700 (E:\\workspace\\springbootDemo\\target\\classes started by gepengfa in E:\\workspace\\springbootDemo)
2018-10-25 23:52:41.990  INFO 1700 --- [           main] c.c.s.s.SpringbootDemoApplication        : No active profile set, falling back to default profiles: default
2018-10-25 23:52:42.088  INFO 1700 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@7f416310: startup date [Thu Oct 25 23:52:42 CST 2018]; root of context hierarchy
2018-10-25 23:52:44.561  INFO 1700 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2018-10-25 23:52:44.584  INFO 1700 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2018-10-25 23:52:44.588  INFO 1700 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.16
2018-10-25 23:52:44.813  INFO 1700 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2018-10-25 23:52:44.813  INFO 1700 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2733 ms
2018-10-25 23:52:45.074  INFO 1700 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: \'dispatcherServlet\' to [/]
2018-10-25 23:52:45.083  INFO 1700 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: \'characterEncodingFilter\' to: [/*]
2018-10-25 23:52:45.083  INFO 1700 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: \'hiddenHttpMethodFilter\' to: [/*]
2018-10-25 23:52:45.083  INFO 1700 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: \'httpPutFormContentFilter\' to: [/*]
2018-10-25 23:52:45.085  INFO 1700 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: \'requestContextFilter\' to: [/*]
2018-10-25 23:52:45.582  INFO 1700 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@7f416310: startup date [Thu Oct 25 23:52:42 CST 2018]; root of context hierarchy
2018-10-25 23:52:45.705  INFO 1700 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/demo],methods=[POST]}" onto public void com.cfxmn.springboot.springbootDemo.controller.DemoController.demoTest()
2018-10-25 23:52:45.710  INFO 1700 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-10-25 23:52:45.711  INFO 1700 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-10-25 23:52:45.759  INFO 1700 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-10-25 23:52:45.759  INFO 1700 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-10-25 23:52:45.817  INFO 1700 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-10-25 23:52:46.321  INFO 1700 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-10-25 23:52:46.529  INFO 1700 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2018-10-25 23:52:46.599  INFO 1700 --- [           main] c.c.s.s.SpringbootDemoApplication        : Started SpringbootDemoApplication in 5.092 seconds (JVM running for 5.764)

 从启动日志标黄的部分可以看出,项目启动成功了,访问端口默认是8080(这个端口是可以改动的)

 下面我们通过postMan请求下,

查看控制台

2018-10-25 23:59:26.385  INFO 1700 --- [nio-8080-exec-2] c.c.s.s.controller.DemoController        : success call

 说明调用成功。

到此,一个简单的springboot项目就构建完成了,但这只是一个空的架子,内容还可载丰富。

下一篇文章我们主要来分析下,springboot启动的相关源码,敬请期待。

以上是关于springboot学习之构建简单项目搭建的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot学习之Helloworld

springboot 学习笔记

Spring Boot 学习之项目构建

Activiti学习之spring boot 与activiti整合

前端学习之Vue项目搭建

IntelliJ IDEA学习之七maven专题