SpringBoot-Helloworld

     SpringBoot是創建在Spring基礎之上,經過最少的Spring前期配置使應用程序儘快啓動並運行。學習SpringBoot以前,預先掌握使用Spring、Maven工具。html

特色:java

  • 嵌入式Servlet容器,不用打包WAR
  • jar與版本自動管理
  • 應用運行跟蹤與指標情況
  • .......

建立工程web

       1) pom.xml文件spring

<?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.demo</groupId>
    <artifactId>spring-boot-helloworld</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <!-- 項目打包爲可執行jar包的插件-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

     2) 建立@SpringBootApplication主程序類apache

package com.demo; 

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; 
/**
 *  @SpringBootApplication 標註主程序類,說明是SpringBoot應用
 */
@SpringBootApplication
public class HelloWorldApplication {

    public static void main(String[] args) { 
        // Spring應用啓動起來
        SpringApplication.run(HelloWorldApplication.class,args);
    }
}

    3) 定義請求app

       說明:程序啓動時,自動掃描加載主程序類所在包以及所有子包的組件,範圍之外的組件註解不自動掃描。maven

package com.demo.controller; 

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HelloController {

    @ResponseBody
    @RequestMapping("/hi")
    public String hi(){
        return "Hello World!";
    }
}

   4)運行spring-boot

        4.1 選擇   HelloWorldApplication --> Run as --> Java Application工具

     4.2 測試學習

                     

相關文章
相關標籤/搜索