如何使用IntelliJ IDEA创建Spring Boot项目

以前使用Spring会伴随着大量的XML配置文件以及复杂的Bean依赖关系,Spring Boot在开发过程中大量使用“约定优先配置”的思想来摆脱Spring框架中各种繁琐的配置。在开发工具方面我们使用IntelliJ IDEA,项目的打包和依赖的第三方工具会使用像Maven或Gradle这样的构建系统。

工具/原料

  • IntelliJ IDEA
  • Maven

方法/步骤

1.打开IntelliJ IDEA,在菜单栏选择File菜单-->New-->Project...-->Spring Initializr,Project SDK即选择JDK的版本,Choose Initializer Service URL选择Default:http://start.spring.io。

 

2.在接下来的页面我们创建一个Maven项目,我们在页面中填写项目相关的信息,接下来选择项目需要的依赖,我们在这里先勾选Web即可,在pom文件中变回生成Spring Boot Web相关的jar包,同时可以选择Spring Boot的版本。其他的依赖在此处不勾选也没有关系,需要的话可以在pom.xml文件中添加。

 

 

3.项目创建完成后会自动生成很多文件,其中DemoApplication是用于启动Spring Boot项目的,有一个main方法,我们知道main方法是程序的入口,因此右键点击Run 'DemoApplication'来运行程序。

 

4.IntelliJ IDEA中集成了Tomcat,因此我们不需要在开发工具中针对Tomcat做任何配置,在运行结果中我们可以看到已经启动了Tomcat的8080端口,这个是Tomcat的默认端口。

 

 

5.除了在IntelliJ IDEA中启动工程,我们还有其他启动项目方法。我们找到项目在本地环境存放的位置,在该位置打开命令行,在命令行中输入mvn spring-boot:run便可以启动项目。

 

 

6.还有另一种启动方法,我们同样在项目的目录下执行mvn install命令,便会在项目的target目录下生成一个jar文件,在命令行使用java命令:java -jar demo-0.0.1-SNAPSHOT.jar(生成的那个jar文件)便可执行程序。

 

若编辑主类中的代码:

package com.springboot.first;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@SpringBootApplication
public class FirstApplication {
    @RequestMapping("/home")
    String home() {
        return "Hello World!";
    }

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

}

在启动项目后,打开浏览器,在地址栏中输入locahost:8080/home

页面上会显示:Hello World!


 

Myeclipse下使用Maven搭建spring boot项目

第一步:
右键,New选择创建maven项目

idea快速搭建spring cloud_spring

第二步:
注意勾选create a simple project(skip archetype selection)//创建一个简单的项目跳过原型选择

idea快速搭建spring cloud_apache_02

第三步:
groupid和artifactId被统称为“坐标”是为了保证项目唯一性而提出的,如果你要把你项目弄到maven本地仓库去,你想要找到你的项目就必须根据这两个id去查找。

 

Group Id : groupId一般分为多个段,常用的为两段,第一段为域,第二段为公司名称。域又分为org、com、cn等等许多,其中org为非营利组织,com为商业组织。举个apache公司的tomcat项目例子:这个项目的groupId是org.apache,它的域是org(因为tomcat是非营利项目),公司名称是apache,artigactId是tomcat。
Artifact Id : 项目的名称
Compiler Level : 选择jdk版本

idea快速搭建spring cloud_spring_03


点击Finish,项目结构为:

idea快速搭建spring cloud_apache_04

第四步:
配置pom.xml

我自己创建springboot项目时的pom.xml

<project xmlns="http:///POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http:///POM/4.0.0 http:///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.3.RELEASE</version>
  </parent>
  <groupId>org.tiandao</groupId>
  <artifactId>helloworld</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <properties>
        <java.version>1.8</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>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
    </plugins>
  </build>
</project>

原文中的pom.xml,和图中项目的groupid等是一致的。

<project xmlns="http:///POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http:///POM/4.0.0 http:///xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.ahut</groupId>
    <artifactId>SpringBootDemo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

以上文件我们做了三步操作:

1.设置spring boot的parent

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.3.RELEASE</version>
</parent>

2.导入spring boot的web支持

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

3.添加Spring boot的插件

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

第五步:
在src/main/java下,创建我们自定义的包和java代码

我自己创建的主类:

package org.tiandao.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@SpringBootApplication
@Controller 
public class Hello {
    @RequestMapping("/hello")
    @ResponseBody
    public String hello() {
        return "hello world" ;
    }
    public static void main(String[] args) {
        SpringApplication.run(Hello.class, args);
    }

}

 

 

原文的:

package com.ahut.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@SpringBootApplication // Spring Boot项目的核心注解,主要目的是开启自动配置
@Controller // 标明这是一个SpringMVC的Controller控制器
public class HelloApplication {

    @RequestMapping("/hello")
    @ResponseBody
    public String hello() {
        return "hello world";
    }

    // 在main方法中启动一个应用,即:这个应用的入口
    public static void main(String[] args) {
        SpringApplication.run(HelloApplication.class, args);
    }

}

第六步:
右键 > Run as > Spring Boot App

第七步:
打开浏览器输入:http://localhost:8080/hello

页面显示:hello world

控制台输出如下:

idea快速搭建spring cloud_maven_05

由上述过程可知,创建maven项目后,重点就是修改pom.xml,自己创建主类时,重点在于使用注解。