起因

看springboot官网,已经进化到了2.6.3 。
Intellij IDEA 一般可以通过两种方式创建 Spring Boot 项目:

  • 使用 Maven 创建
  • 使用 Spring Initializr 创建(https://start.spring.io/)

网上大多是用idea专业版通过spring initializr来创建springboot项目,然而外网实在是慢。本节讲解通过Idea社区版通过maven来创建springboot项目。

步骤

1.通过maven创建一个空项目:
在这里插入图片描述
手动填写项目名称和包名 ,宣传maven环境,点击创建即可。
2.手动删除生成的App.java
3.修改pom.xml
我创建的项目是:SpringbooteduApplication

<?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.tuyuan</groupId>
  <artifactId>springboot2edu</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>springboot2edu</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

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

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>11</java.version>
  </properties>

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

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

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

4.增加代码SpringbooteduApplication

package com.tuyuan.springboot2edu;

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

@SpringBootApplication
public class SpringbooteduApplication {

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

}

如果代码报错 ,刷新一下maven。
执行maven的install会安装相应的包。
右键运行SpringbooteduApplication即可启动项目。

在这里插入图片描述

代码结构

现在的代码结构非常简单,严格意义上说,只有一个类:SpringbooteduApplication
在这里插入图片描述
里面有一个最核心的注解:

SpringBootApplication

这个注解让spring boot自动给程序进行必要的配置,这个配置等同于:

@Configuration ,@EnableAutoConfiguration 和 @ComponentScan 三个配置。

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐