SpringBoot項(xiàng)目搭建
作者:xcbeyond
瘋狂源自夢(mèng)想,技術(shù)成就輝煌!微信公眾號(hào):《程序猿技術(shù)大咖》號(hào)主,專注后端開發(fā)多年,擁有豐富的研發(fā)經(jīng)驗(yàn),樂(lè)于技術(shù)輸出、分享,現(xiàn)階段從事微服務(wù)架構(gòu)項(xiàng)目的研發(fā)工作,涉及架構(gòu)設(shè)計(jì)、技術(shù)選型、業(yè)務(wù)研發(fā)等工作。對(duì)于Java、微服務(wù)、數(shù)據(jù)庫(kù)、Docker有深入了解,并有大量的調(diào)優(yōu)經(jīng)驗(yàn)。
一、引言:
什么是spring boot?
Spring Boot是由Pivotal團(tuán)隊(duì)提供的全新框架,其設(shè)計(jì)目的是用來(lái)簡(jiǎn)化新Spring應(yīng)用的初始搭建以及開發(fā)過(guò)程。該框架使用了特定的方式來(lái)進(jìn)行配置,從而使開發(fā)人員不再需要定義樣板化的配置。用我的話來(lái)理解,就是spring boot其實(shí)不是什么新的框架,它默認(rèn)配置了很多框架的使用方式,就像maven整合了所有的jar包,spring boot整合了所有的框架(不知道這樣比喻是否合適)。
使用spring boot有什么好處?
其實(shí)就是簡(jiǎn)單、快速、方便!平時(shí)如果我們需要搭建一個(gè)spring web項(xiàng)目的時(shí)候需要怎么做呢?
1)配置web.xml,加載spring和spring mvc
2)配置數(shù)據(jù)庫(kù)連接、配置spring事務(wù)
3)配置加載配置文件的讀取,開啟注解
4)配置日志文件
...
配置完成之后部署tomcat 調(diào)試
...
但是如果使用spring boot呢?
很簡(jiǎn)單,我僅僅只需要非常少的幾個(gè)配置就可以迅速方便的搭建起來(lái)一套web項(xiàng)目或者是構(gòu)建一個(gè)微服務(wù)!
使用sping boot是不是很爽,不信,接下來(lái)往下看!
二、基礎(chǔ)環(huán)境準(zhǔn)備
(以Spring Boot 2.1.0.BUILD-SNAPSHOT版本搭建)
1、JDK環(huán)境安裝
Spring Boot 2.1.0.BUILD-SNAPSHOT要求java8以上版本。在開始之前需要檢查開發(fā)環(huán)境jdk版本是否符合要求,使用以下命令檢查當(dāng)前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上的說(shuō)明進(jìn)行操作。
(對(duì)于為何要安裝Maven,請(qǐng)自行查閱Maven相關(guān)資料)
三、創(chuàng)建SpringBoot項(xiàng)目
開發(fā)工具:Eclipse Jee Oxygen(v4.7.0)
1、創(chuàng)建Maven 工程。
2、創(chuàng)建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啟動(dòng)類
新建SpringbootApplication.java文件,如下:
package com.xcbeyond.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* SpringBoot啟動(dòng)類
* @author xcbeyond
* 2018年7月2日下午5:41:45
*/
@SpringBootApplication
public class SpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootApplication.class, args);
}
}
注:注解@SpringBootApplication,稍后會(huì)有章節(jié)詳細(xì)說(shuō)明。
(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!";
}
}
說(shuō)明:
@RestController的意思就是controller里面的方法都以json格式輸出,不用再寫什么配置!
四、運(yùn)行SpringBoot工程
(1)啟動(dòng)。
直接運(yùn)行上述的SpringbootApplication類即可,啟動(dòng)后可以看到類似于如下日志輸出:
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)測(cè)試。
在瀏覽器中輸入http://localhost:8080/print,則會(huì)如下正常顯示,就說(shuō)明已經(jīng)OK啦,帥不帥!
項(xiàng)目源碼:https://github.com/xcbeyond/micro-service/tree/master/springboot