利用Maven快速創建一個簡單的spring boot 實例


 

Spring Boot的好處:spring boot 大大減少了 使用spring的配置 和大量 xml 文件,並有效解決的項目之間的依賴問題,為想使用 spring項目 大大減輕的工作量

1.先創建一個Maven項目

2.配置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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>SpringBootDemo</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>SpringBootDemo Maven Webapp</name>
  <url>http://maven.apache.org</url>
        <!-- parent 對應的父依賴,自動為你添加常用的容器依賴 -->
      <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.6.RELEASE</version>
      </parent>
      <dependencies>
          <!-- 開發web項目需要下面jar包 -->
          <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
      </dependencies>
  <build>
    <finalName>SpringBootDemo</finalName>
  </build>
</project>

3.簡單的Application類

@EnableAutoConfiguration:自動載入應用程序所需的所有Bean

SpringApplication.run()將引導我們的應用,啟動Spring,相應地啟動被自動配置的Tomcat web服務器

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@EnableAutoConfiguration
public class Example {
    
    @RequestMapping("/")
    String home() {
        return "Hello World!";
    }
    public static void main(String[] args) throws Exception {
        SpringApplication.run(Example.class, args);
    }
}

然后Run As啟動

訪問:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM