SpringBoot初級教程

最近項目中須要使用Spring來開發具備restful風格url的後臺應用。因而我就想到了使用SpringBoot進行快速的構建和發佈。不浪費時間了,下面就直接切入主題。
主要參考:Spring官網文檔java

一、使用IDEA建立Maven項目

輸入圖片說明

二、修改pom.xml

<?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.baron</groupId>
    <artifactId>spring-boot-test</artifactId>
    <version>0.1</version>

    <!-- 指定阿里雲的maven倉庫,訪問國外的實在太慢 -->
    <repositories>
        <repository>
            <id>alimaven</id>
            <name>aliyun maven</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public//</url>
        </repository>
    </repositories>
    
    <!-- 設置pom的父參照 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.2.RELEASE</version>
    </parent>

    <dependencies>
        <!-- Spring Boot 依賴 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <properties>
        <!-- 指定JDK版本 -->
        <java.version>1.8</java.version>
    </properties>
    
    <!-- 指定build 須要使用的maven plugin -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <artifactId>maven-failsafe-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

輸入完成以後,右鍵從新導入maven依賴 輸入圖片說明web

三、編寫代碼

Application.java // 用於啓動應用spring

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;

// 註解表示了這是一個SpringBoot的Application
// 這個在咱們進行打包的時候會使用到,由於咱們使用spring-boot-maven-plugin打成可執行jar包的時候,
// jar包的Main Class並非咱們的Application,因此加上這個註解便於Spring找到須要啓動的應用
// 你也能夠測試是否能夠在一個jar包內使用SpringBootApplication,從而啓動多個SpringBoot應用
@SpringBootApplication
// 雖然@SpringBootApplication註解至關於@Configuration, @EnableAutoConfiguration 和 @ComponentScan
// 可是須要注意,是至關於沒有參數的@ComponentScan,沒有參數就只會在和Application類的同級目錄下尋找
// Spring的Bean,因此咱們這邊添加上參數"com.*",就會把com目錄下全部的類都掃描一遍
@ComponentScan("com.*")
public class Application {
    public static void main(String[] args) {
        // 啓動
        SpringApplication.run(Application.class, args);
    }
    
    // 用於配置SpringBoot啓動時候的參數
    // 這個仍是比較有用的
    @Bean
    public EmbeddedServletContainerCustomizer containerCustomizer() {
        return container -> {
            // 配置內部的tomcat容器的監聽端口
            container.setPort(80);
            // 配置SpringBoot項目的根路徑
            container.setContextPath("/hello");
        };
    }
}

HelloController.java 用於對外提供服務apache

// 此處和Application.java處於不一樣的包
package com.baron.controller;

import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

// RestConroller註解的做用就是專門給restful的服務的Controller使用
// 使用此註解解釋的類,內部全部提供服務的類的返回值都會被自動解析成json
@RestController
public class HelloController {
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String sayHello() {
        return "Hello, SpringBoot";
    }
}

四、Maven打包成可執行Jar

Tips:此處假設maven安裝成功
直接在cmd輸入
mvn clean package
便可,會自動把全部的依賴都添加進去。 輸入圖片說明json

相關文章
相關標籤/搜索