1. 程式人生 > >idea+springboot helloworld入門

idea+springboot helloworld入門

1.開發環境準備

  • IntelliJ IDEA 2018.1.4 x64
  • apache-maven-3.3.9
  • jdk1.8.0_92
  • 2.1.1.RELEASE

idea maven設定 採取預設配置

2.建立springboot專案

3.啟動springboot專案

忽視

package com.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.
SpringBootApplication; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }

4.編寫相關的Controller、Service 並測試##


package com.springboot.controller;

import org.springframework.stereotype.
Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class HelloController { @ResponseBody @RequestMapping("/hello") public String hello(){ return "Hello World!"; } }

5.解析springboot helloworld 專案


<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.1.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<!--依賴於下面的專案 該專案包含了很多配置 所以不需要手動配置-->

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.1.1.RELEASE</version>
    <relativePath>../../spring-boot-dependencies</relativePath>
</parent>


<!--spring-boot-starter是場景啟動器 下面的依賴匯入了web所需要的模組-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>



/**
 * @SpringBootApplication 用來標註一個主程式 說明這是一個springboot應用
 */
@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
		//啟動springboot程式
        SpringApplication.run(DemoApplication.class, args);
    }
}



@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
    excludeFilters = {@Filter(
    type = FilterType.CUSTOM,
    classes = {TypeExcludeFilter.class}
), @Filter(
    type = FilterType.CUSTOM,
    classes = {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication {



  • @SpringBootConfiguration 是springboot的配置類
  • 配置類 本質上是配置檔案,配置類是容器的一個元件
  • @EnableAutoConfiguration 開啟欄位註解功能 會自動引入很多註解


@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {


6.專案打包編譯


<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <mainClass>com.springboot.DemoApplication</mainClass>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>


Building jar: D:\sdk\IdeaProjects\springboot\target\demo-0.0.1-SNAPSHOT.jar

可以使用java -jar命令執行 該專案

Exception in thread “main" java.lang.UnsupportedClassVersionError

說明專案的jdk 與 系統配置的jdk版本不一致