1. 程式人生 > >Spring Boot項目搭建

Spring Boot項目搭建

pri resources 以及 urn ext tar tom 管理 tex

1.Spring Boot概述

Spring Boot是由Pivotal團隊提供的全新框架,其設計目的是用來簡化新Spring應用的初始搭建以及開發過程。該框架使用了特定的方式來進行配置,從而使開發人員不再需要定義樣板化的配置。總所周知,Spring平臺飽受非議的一點就是大量的XML配置以及復雜的依賴管理,而Spring Boot的出現就是用來簡化操作的。相比傳統的Spring,項目搭建更簡單、方便、快速。

2.項目搭建

本文采用IDEA搭建Spring Boot,Demo結構圖如下:

技術分享

通過IDEA生成Spring Boot項目很方便,具體步驟不再贅述,可以參考網上其他資料,如上圖,主要生成:

    • src/main/java 程序開發以及主程序入口
    • src/main/resources 配置文件
    • src/test/java 測試程序

默認pom.xml生成jar包依賴項如下所示:

<?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.snail</groupId> <artifactId>bootdemo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>bootdemo</name> <description
>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.4.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> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> </dependencies>

<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>

3.測試用例

Demo並沒有配置數據庫,只是簡單地測試一下Http 請求,並結合現實中的場景,我們的配置文件往往在線下環境、生產環境的配置是不一樣的,可以通過配置來獲取對應的文件,如下所示。

(1)實體類

其中ConfigurationProperties註解的目的是用來映射配置文件的,會在配置文件yml文件裏新建配置項application.yml、application-product.yml(生產環境)、application-test.yml(測試環境),通過前綴CustomerInfo可以獲取配置文件裏對應的映射。

/**
 * 客戶基本信息
 * Create by snailTech
 * 2017-07-24 16:31
 **/
@Component
@ConfigurationProperties(prefix = "CustomerInfo")
public class CustomerInfo {
    /**
     * 姓名
     */
    private String name;

    /**
     * 手機號碼
     */
    private String mobile;

    /**
     * 年齡
     */
    private Integer age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

(2)application.yml

spring:
  profiles:
    active: product

通過這個配置active為product可以得知讀取的是application-product.yml文件,其中port是Web容器(默認是Tomcat)的端口號,以及context-path是虛擬路徑。

(3)application-product.yml

server:
  port: 8080
  context-path: /bootdemo
testUrl: http://www.baidu.com
CustomerInfo:
  name: snail
  mobile: 18818718711(生產環境)
  age: 30

(4)application-test.yml

server:
  port: 8080
  context-path: /bootdemo
testUrl: http://www.baidu.com
CustomerInfo:
  name: snail
  mobile: 18818718711(測試環境)
  age: 30

(5)配置Controller層

@[email protected][email protected],都是以json格式返回,@Value("${testUrl}")是用來獲取配置文件裏的testUrl配置項的,@RequestParam用來接收請求參數,其余的寫法與SpringMvc的寫法並沒什麽區別

package com.snail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
/**
 * Create by snailTech
 * 2017-07-24 16:10
 **/
@RestController
@RequestMapping(value = "/test")
public class TestController {
    @Value("${testUrl}")
    private String testUrl;

    @Autowired
    private CustomerInfo customerInfo;

    //@RequestMapping(value="/hello" ,method = RequestMethod.GET)
    @GetMapping(value = "/hello")
    public String hello(@RequestParam("id") Integer xx){
        return customerInfo.getMobile()+xx;
    }
}

(6)編譯

編譯非常簡單,內置Tomcat,無需像SSM項目裏還需要手動配置Tomcat,只要運行程序就可以了。

(7)運行

技術分享

Spring Boot項目搭建