Maven構建springBoot Demo案例

來源:互聯網
上載者:User
一.簡介

springBoot的理念是讓你不需要在項目中進行大量的配置就能讓項目快速運行起來。使用springBoot輕易的就可以建立一個獨立啟動並執行,基於Spring准生產層級的架構項目,而不用或者很少的Spring配置。 二.開發環境

eclipse版本:4.6.1

maven版本:3.3.3

JDK版本:1.8.0_111

SpringBoot版本:1.4.3 三.項目結構

圖 四.操作步驟

1.建立一個Maven項目

圖

圖

圖

2.建立完成後,如果項目報紅色,(1).需要對項目右鍵-》屬性-》Generate Deloyment Desriptor Stub。(2).項目右鍵-》Maven-》Update Project

圖

圖

圖 五.檔案清單

項目建好後,修改以下檔案。

pom.xml

<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>springbootDemo</groupId><artifactId>springbootDemo</artifactId><version>1.0</version><packaging>war</packaging><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.4.3.RELEASE</version><relativePath /></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

說明:添加spring-boot-starter-web依賴,會自動添加Tomcat和Spring MVC的依賴,那麼Spring Boot會對Tomcat和Spring MVC進行自動設定。

Application.java

package com.dqiang.demo;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;/** * @author StemQ * @version v1.0 * Blog:http://blog.csdn.net/stemq * Web:www.dqiang.com */@SpringBootApplication@RestControllerpublic class Application {@RequestMapping("/")public String greeting() {return "Hello World!";}public static void main(String[] args) {SpringApplication.run(Application.class, args);}}

說明:@SpringBootApplication是Spring Boot的核心註解,也是一個組合註解。主要組合了@Configuration、@EnableAutoConfiguration、@ComponentScan。如果不使用組合註解@SpringBootApplication則可以直接使用@Configuration、@EnableAutoConfiguration、@ComponentScan。

application.properties

#tomcat\u7AEF\u53E3\u4FEE\u6539server.port=9090

說明:此檔案可以不配置,預設springBoot整合的Tomcat啟動連接埠是8080,因為我系統有別的環境佔用8080連接埠,所以將項目的運行連接埠修改為9090 六.運行

1.項目右鍵-》Run As-》Spring Boot App

圖

2.訪問

圖 七.Demo下載

點擊下載

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.