1. 程式人生 > >Intellij IDEA下 Spring boot debug配置

Intellij IDEA下 Spring boot debug配置

1 介紹

Spring Boot 仍然是基於Spring的並且內建Servlet Container , 早去的的容器為Jetty ,最新的容器為Tomcat

Name Servlet Version Java Version

Tomcat 8

3.1

Java 7+

Tomcat 7

3.0

Java 6+

Jetty 9.3

3.1

Java 8+

Jetty 9.2

3.1

Java 7+

Jetty 8

3.0

Java 6+

Undertow 1.3

3.1

Java 7+

Spring boot 把專案打包為jar檔案 ,到時候直接執行
java -jar spring-boot01-1.0-SNAPSHOT.jar --server.port=9000 就可以直接執行jar。當然在linux環境下可以nohup java -jar spring-boot01-1.0-SNAPSHOT.jar --server.port=9000 & 來後臺執行 。

 2 實戰

專案結構


maven 內容 

<?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.jackie</groupId> <artifactId>springboottest</artifactId> <version>1.0-SNAPSHOT</version> <!-- Inherit defaults from Spring Boot -->
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.0.BUILD-SNAPSHOT</version> </parent> <!-- Add typical dependencies for a web application --> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <!-- Package as an executable jar --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <!-- Add Spring repositories --> <!-- (you don't need this if you are using a .RELEASE version) --> <repositories> <repository> <id>spring-snapshots</id> <url>http://repo.spring.io/snapshot</url> <snapshots><enabled>true</enabled></snapshots> </repository> <repository> <id>spring-milestones</id> <url>http://repo.spring.io/milestone</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-snapshots</id> <url>http://repo.spring.io/snapshot</url> </pluginRepository> <pluginRepository> <id>spring-milestones</id> <url>http://repo.spring.io/milestone</url> </pluginRepository> </pluginRepositories> </project>
Application.java
package com.jackie;

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

/**
 * Created by luhaiming on 2018/1/9 0009.
 */
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
Example.java
package com.jackie;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by luhaiming on 2018/1/9 0009.
 */
@RestController
@EnableAutoConfiguration
public class Example {

    @RequestMapping("/")
    String home() {
        return "Hello World!";
    }

    @RequestMapping("/hello/{myName}")
    String index(@PathVariable String myName) {
        return "Hello "+myName+"!!!";
    }
}
然後直接執行maven install 命令生成jar 檔案就可以按照上面方法來運行了 

Intellij IDEA spring boot run/debug configuration


點選run/debug Edit Configuration
點選+ button 選擇Spring Boot
詳細配置 只要有 Main class : 配置Spring boot 的入口VM options:-Xms512m
-Xmx512m
-Xmn164m
-XX:MaxPermSize=250m
-XX:ReservedCodeCacheSize=64m
-Dserver.port=8090
-ea這樣就可以DEBUG 了