spring boot 學習--01--springboot開始

spring boot 學習–01–springboot開始

什麼是springboot

spring boot 是一個開箱即用,可以基本不用配置文件,直接用默認配置來進行項目開發的框架,並且springboot是自帶服務器的框架

springboot的優勢

  1. 配置少
  2. 部署簡單
  3. 一注到底
  4. 自帶容器(jetty,tomcat)

基本配置

springboot服務器和jdk基本配置

springboot使用

  1. 檢查java (java -version)版本,看看是否是jdk1.7及以上
  2. 查看maven(mvn -v) 版本,最好是maven3.2以上

開發第一個springboot應用

1. 創建pom.xml

pom.xml

```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>com.study.spring</groupId>
  <artifactId>boot-hello</artifactId>
  <version>1.0.0</version>

  <!-- springboot 父類,版本控制-->
  <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.0.RELEASE</version>
    </parent>


    <dependencies>
            <!--web應用配置 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
        <!-- 可執行jar配置 -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

2.編寫主類


package com.springboot.study;

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;

/**
 * Springboot 啓動類,一般啓動類都放在最上層包,就是根路徑包
 * 
 * SpringBootApplication = (@Configuration,@EnableAutoConfiguration,@ComponentScan)
 * 
 * RestController restful形式的控制器和 Controller作用一樣
 * 
 * @author like
 *
 */
@RestController
@SpringBootApplication
public class App {

    @RequestMapping("/")
    String home() {
        return "Hello World!";
    }


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

}

3. 啓動應用

  1. 直接啓動main主類,右鍵RUN AS
    RunAs

  2. 啓動界面
    啓動界面

  3. 訪問 http://localhost:8080/
  4. 結果
    運行結果

  5. OK

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章