1. 程式人生 > >簡易Spring-boot專案的搭建demo

簡易Spring-boot專案的搭建demo

最近剛開始學習Spring-boot框架,從最開始搭建一個Maven工程--配置檔案--測試demo--啟動Spring—boot專案。

1.新建一個maven project(Create a simple project)

 

2.Next   

填寫好專案名稱及打包方式,Finish

3.這時構建完專案後pom.xml檔案會報錯,則這時候需要我們匯入依賴的jar包

<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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
    </parent>
    
    <groupId>cn.itcast.springboot</groupId>
    <artifactId>itcast-springboot</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.jolbox</groupId>
            <artifactId>bonecp-spring</artifactId>
            <version>0.8.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.7.RELEASE</version>
        </dependency>
        <!-- 連線池 -->
        <dependency>
            <groupId>com.jolbox</groupId>
            <artifactId>bonecp-spring</artifactId>
            <version>0.8.0.RELEASE</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <!-- 資原始檔拷貝外掛 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <!-- java編譯外掛 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
        <pluginManagement>
            <plugins>
                <!-- 配置Tomcat外掛 -->
                <plugin>
                    <groupId>org.apache.tomcat.maven</groupId>
                    <artifactId>tomcat7-maven-plugin</artifactId>
                    <version>2.2</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>


</project>

4.這時候pom.xml檔案可能還會有報錯,如果有web.xml is missing and <failOnMissingWebXml> is set to true錯誤,則看如下連結有解決方案:https://blog.csdn.net/qq_38383402/article/details/82625095

5.此處注意要設定Spring boot的parent

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent

</artifactId>

<version>1.5.2.RELEASE</version>

</parent>

說明:Spring boot的專案必須要將parent設定為spring boot的parent,該parent包含了大量預設的配置,大大簡化了我們的開發。

設定Spring boot的web支援

<dependency>

        <groupId>org.springframework.boot</groupId>

       <artifactId>spring-boot-starter-web</artifactId>

</dependency>

新增Spring boot的外掛

<plugin>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>

</plugin>

6.接下來我們可以測試寫第一個Spring boot應用

@Controller

@SpringBootApplication

@Configuration

public class HelloApplication {

    @RequestMapping("hello")

    @ResponseBody

    public String hello(){

        return "hello world!";

    }

    public static void main(String[] args) {

        SpringApplication.run(HelloApplication.class, args);

    }

}

程式碼說明:

    1、@SpringBootApplication:Spring Boot專案的核心註解,主要目的是開啟自動配置。;

    2、@Configuration:這是一個配置Spring的配置類;

    3、@Controller:標明這是一個SpringMVC的Controller控制器;

    4、main方法:在main方法中啟動一個應用,即:這個應用的入口;

 

7.在Spring Boot專案中,啟動的方式有兩種,一種是直接run Java Application另外一種是通過Spring Boot的Maven外掛執行。

看到有  Started HelloApplication in 6.148 seconds (JVM running for 6.762)  說明Spring boot專案啟動成功。

第二種方式啟動Spring boot專案

執行Run

控制檯輸出 Started HelloApplication in 5.111 seconds (JVM running for 48.644)  則Spring boot 專案啟動成功。

8.訪問頁面檢視效果

http://localhost:8080/hello

如果看到頁面顯示 Hello World! 則表示Spring boot專案成功啟動並能訪問。