一、引言:
什么是spring boot?
Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。用我的话来理解,就是spring boot其实不是什么新的框架,它默认配置了很多框架的使用方式,就像maven整合了所有的jar包,spring boot整合了所有的框架(不知道这样比喻是否合适)。
使用spring boot有什么好处?...
但是如果使用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>java -version
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
4.0.0
com.xcbeyond
springboot
0.0.1-SNAPSHOT
jar
springboot
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.0.0.RELEASE
UTF-8
UTF-8
1.8
Finchley.M9
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false
注:可以利用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类即可,启动后可以看到类似于如下日志输出:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.0.0.RELEASE)
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> 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