JavaWeb——SpringBoot 系列(一)用IDEA+Maven搭建SpringBoot

一、关于 Spring Boot

  • Spring Boot 地出现是为了让程序员从繁多的配置、低下的开发效率、复杂的部署中解脱出来,Spring Boot 使用”习惯优于配置“的理念,让开发者不用或者只需要很少的 Spring 配置便可以让项目快速运行。
  • Spring Boot 有以下特点:
  • (1)可以创建独立的Spring应用程序,并且基于其Maven或Gradle插件,可以创建可执行的JARs和WARs;
  • (2)内嵌Tomcat或Jetty等Servlet容器;
  • (3)提供自动配置的“starter”项目对象模型(POMS)以简化Maven配置;
  • (4)尽可能自动配置Spring容器;
  • (5)提供准备好的特性,如指标、健康检查和外部化配置;
  • (6)绝对没有代码生成,不需要XML配置。

二、IDEA+Maven 搭建 Spring Boot

  • 利用编辑器 IDEA 和 Maven 搭建一个 Spring Boot 简单项目的一般步骤如下:

1、新建一个空白 Maven 项目

  • 在 IDEA 的开始页上选择新建一个项目
    在这里插入图片描述
  • 选择新建 Maven 项目
    在这里插入图片描述
  • 直接 NEXT
    在这里插入图片描述
  • 填写好项目名称后直接 FINISH。

2、编辑 POM 文件导入项目依赖

  • 打开项目根目录下的 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>org.example</groupId>
        <artifactId>Empty</artifactId>
        <version>1.0-SNAPSHOT</version>
    <!--    用于提供相关的 Maven 默认依赖-->
        <parent>
            <artifactId>spring-boot-starter-parent</artifactId>
            <groupId>org.springframework.boot</groupId>
            <version>1.3.0.M1</version>
            <relativePath/>
        </parent>
        <dependencies>
    <!--        添加Web依赖,以提供Web支持-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
        </dependencies>
        <build>
            <plugins>
    <!--            spring boot 编译插件-->
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <version>1.3.0.M1</version>
                </plugin>
            </plugins>
        </build>
        <repositories>
            <repository>
                <id>spring-snapshots</id>
                <name>Spring Snapshots</name>
                <url>https://repo.spring.io/snapshot</url>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </repository>
            <repository>
                <id>spring-milestones</id>
                <name>Spring Milestones</name>
                <url>https://repo.spring.io/milestone</url>
                <snapshots>
                    <enabled>false</enabled>
                </snapshots>
            </repository>
        </repositories>
        <pluginRepositories>
            <pluginRepository>
                <id>spring-snapshots</id>
                <name>Spring Snapshots</name>
                <url>https://repo.spring.io/snapshot</url>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </pluginRepository>
            <pluginRepository>
                <id>spring-milestones</id>
                <name>Spring Milestones</name>
                <url>https://repo.spring.io/milestone</url>
                <snapshots>
                    <enabled>false</enabled>
                </snapshots>
            </pluginRepository>
        </pluginRepositories>
    </project>
    

3、编写一个测试控制器

  • 编写一个 controller 用于测试,将 class 文件放置在 src/main/java 目录下的适当位置:
    //IntelliJ IDEA
    //Empty
    //Ch522Application
    //2020/2/1
    // Author:御承扬
    //E-mail:2923616405@qq.com
    
    package com.wisely.ch5_2_2;
    
    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 Ch522Application {
        @RequestMapping("/")
        String index(){
            return "Hello! I am testing spring boot";
        }
    
        public static void main(String[] args) {
            SpringApplication.run(Ch522Application.class,args);
        }
    }
    

4、测试运行

  • 点击 IDEA 编辑框里的三角符号运行:
    在这里插入图片描述
  • 打开浏览器,访问 localhost:8080
    在这里插入图片描述

上一篇
下一篇

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

御承扬

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值