SpringBoot项目搭建
作者:xcbeyond
疯狂源自梦想,技术成就辉煌!微信公众号:《程序猿技术大咖》号主,专注后端开发多年,拥有丰富的研发经验,乐于技术输出、分享,现阶段从事微服务架构项目的研发工作,涉及架构设计、技术选型、业务研发等工作。对于Java、微服务、数据库、Docker有深入了解,并有大量的调优经验。
一、引言:
什么是spring boot?
Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。用我的话来理解,就是spring boot其实不是什么新的框架,它默认配置了很多框架的使用方式,就像maven整合了所有的jar包,spring boot整合了所有的框架(不知道这样比喻是否合适)。
使用spring boot有什么好处?
其实就是简单、快速、方便!平时如果我们需要搭建一个spring web项目的时候需要怎么做呢?
1)配置web.xml,加载spring和spring mvc
2)配置数据库连接、配置spring事务
3)配置加载配置文件的读取,开启注解
4)配置日志文件
...
配置完成之后部署tomcat 调试
...
但是如果使用spring boot呢?
很简单,我仅仅只需要非常少的几个配置就可以迅速方便的搭建起来一套web项目或者是构建一个微服务!
使用sping boot是不是很爽,不信,接下来往下看!
二、基础环境准备
(以Spring Boot 2.1.0.BUILD-SNAPSHOT版本搭建)
1、JDK环境安装
Spring Boot 2.1.0.BUILD-SNAPSHOT要求java8以上版本。在开始之前需要检查开发环境jdk版本是否符合要求,使用以下命令检查当前jdk版本:
C:\Users\Administrator><strong><span style="color:#ff0000;">java -version</span></strong>
java version "1.8.0_121"
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)
2、Maven安装
Spring Boot与Apache Maven 3.2或更高版本兼容。如果您尚未安装Maven,则可以按照maven.apache.org上的说明进行操作。
(对于为何要安装Maven,请自行查阅Maven相关资料)
三、创建SpringBoot项目
开发工具:Eclipse Jee Oxygen(v4.7.0)
1、创建Maven 工程。
2、创建pom.xml
<?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.xcbeyond</groupId>
<artifactId>springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.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>
<spring-cloud.version>Finchley.M9</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
注:可以利用eclipse打开pom.xml图形界面方式添加依赖包。
3、编写代码。
(1)、SpringBoot启动类
新建SpringbootApplication.java文件,如下:
package com.xcbeyond.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* SpringBoot启动类
* @author xcbeyond
* 2018年7月2日下午5:41:45
*/
@SpringBootApplication
public class SpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootApplication.class, args);
}
}
注:注解@SpringBootApplication,稍后会有章节详细说明。
(2)Controller
新建ControllerDemo.java,如下:
package com.xcbeyond.springboot.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Controller demo
* @author xcbeyond
* 2018年7月5日下午11:22:49
*/
@RestController
public class ControllerDemo {
@RequestMapping("/print")
public String print() {
return "hello SpringBoot!";
}
}
说明:
@RestController的意思就是controller里面的方法都以json格式输出,不用再写什么配置!
四、运行SpringBoot工程
(1)启动。
直接运行上述的SpringbootApplication类即可,启动后可以看到类似于如下日志输出:
2018-07-05 23:02:49.681 INFO 8932 --- [ main] c.x.springboot.SpringbootApplication : Starting SpringbootApplication on xcbeyond with PID 8932 (E:\micro-service\micro-service\springboot\target\classes started by Administrator in E:\micro-service\micro-service\springboot)
2018-07-05 23:02:49.687 INFO 8932 --- [ main] c.x.springboot.SpringbootApplication : No active profile set, falling back to default profiles: default
2018-07-05 23:02:49.767 INFO 8932 --- [ main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@4c40b76e: startup date [Thu Jul 05 23:02:49 CST 2018]; root of context hierarchy
2018-07-05 23:02:51.628 INFO 8932 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2018-07-05 23:02:51.655 INFO 8932 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2018-07-05 23:02:51.655 INFO 8932 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.28
2018-07-05 23:02:51.679 INFO 8932 --- [ost-startStop-1] 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: [C:\Program Files\Java\jre1.8.0_171\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Program Files/Java/jre1.8.0_171/bin/server;C:/Program Files/Java/jre1.8.0_171/bin;C:/Program Files/Java/jre1.8.0_171/lib/amd64;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)\Java\jdk1.7.0_79\bin;C:\Program Files\Git\cmd;C:\Program Files\TortoiseGit\bin;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;;C:\Users\Administrator\Desktop\eclipse-jee-oxygen-R-win32-x86_64\eclipse;;.]
2018-07-05 23:02:51.895 INFO 8932 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2018-07-05 23:02:51.895 INFO 8932 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2134 ms
2018-07-05 23:02:52.084 INFO 8932 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/]
2018-07-05 23:02:52.091 INFO 8932 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-07-05 23:02:52.092 INFO 8932 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-07-05 23:02:52.093 INFO 8932 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-07-05 23:02:52.093 INFO 8932 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2018-07-05 23:02:52.534 INFO 8932 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@4c40b76e: startup date [Thu Jul 05 23:02:49 CST 2018]; root of context hierarchy
2018-07-05 23:02:52.634 INFO 8932 --- [ 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.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-07-05 23:02:52.637 INFO 8932 --- [ 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.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-07-05 23:02:52.685 INFO 8932 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-07-05 23:02:52.685 INFO 8932 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-07-05 23:02:52.732 INFO 8932 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-07-05 23:02:52.958 INFO 8932 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2018-07-05 23:02:53.111 INFO 8932 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2018-07-05 23:02:53.120 INFO 8932 --- [ main] c.x.springboot.SpringbootApplication : Started SpringbootApplication in 3.981 seconds (JVM running for 5.608)
(2)测试。
在浏览器中输入http://localhost:8080/print,则会如下正常显示,就说明已经OK啦,帅不帅!
项目源码:https://github.com/xcbeyond/micro-service/tree/master/springboot