AdSense

網頁

2019/10/5

IntelliJ IDEA Community 建立Spring Boot專案教學
create spring boot project

在IntelliJ IDEA Community建立Spring Boot專案的方式如下。


範例環境:

  • Windows 64 Bit
  • Java 11
  • IntelliJ IDEA version:2019.2.3 (Community Edition)

Spring Initializr產生Spring Boot專案的基本目錄及設定zip檔。

選擇[Maven Project], Language選[Java],Spring Boot版本選擇穩定的2.1.9



設定Maven專案名稱,可任意命名。
這裡[Group]命名為com.abc
[Artifact]命名為spring-boot-demo

[Packaging]選擇[Jar];
[Java]選擇[11]。



在搜尋欄輸入Web,點選Spring Web加入為依賴。



最後點選[Generate],則Spring Initializr會根據以上設定產生下載的zip檔。



把下載的zip檔案移到想擺放的目錄位置並解壓縮。本範例放在D:\MyProject\IdeaProjects



開啟Intellij IDEA,選擇[Import Project]。



找到剛解壓縮目錄中的pom.xml,選擇後按OK



以下皆維持預設,直按Next




by肉豬



如果是第一次匯入因為IntelliJ IDEA需要載入一些maven相關的檔案,所以會比較慢。

匯入後的Spring Boot專案目錄結構如下。



Spring Intitializr生成的Spring Boot Web專案的pom.xml

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 https://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>2.1.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.abc</groupId>
    <artifactId>spring-boot-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-boot-demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>11</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>
    </dependencies>

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

</project>

Spring Intitializr生成的Spring Boot Web專案的進入點SpringBootWebApplication類別。

SpringBootDemoApplication

package com.abc.springbootdemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootDemoApplication.class, args);
    }
    
}

SpringBootDemoApplication上按滑鼠右鍵,點選[Run 'SpringBootDemo....main()']啟動專案。


啟動成功會在IntelliJ IDEA下方的Run視窗印出下面訊息。

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::  (v2.1.9.RELEASE)

2019-10-06 14:02:17.495  INFO 1116 --- [           main] c.a.s.SpringBootDemoApplication          : Starting SpringBootDemoApplication on matt-PC with PID 1116 (D:\MyProject\IdeaProjects\spring-boot-demo\target\classes started by matt in D:\MyProject\IdeaProjects\spring-boot-demo)
2019-10-06 14:02:17.499  INFO 1116 --- [           main] c.a.s.SpringBootDemoApplication          : No active profile set, falling back to default profiles: default
2019-10-06 14:02:20.488  INFO 1116 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2019-10-06 14:02:20.507  INFO 1116 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-10-06 14:02:20.507  INFO 1116 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.26]
2019-10-06 14:02:20.670  INFO 1116 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-10-06 14:02:20.670  INFO 1116 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2966 ms
2019-10-06 14:02:20.948  INFO 1116 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-10-06 14:02:21.275  INFO 1116 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2019-10-06 14:02:21.282  INFO 1116 --- [           main] c.a.s.SpringBootDemoApplication          : Started SpringBootDemoApplication in 4.905 seconds (JVM running for 6.568)


沒有留言:

AdSense