02 Spring Boot 入门开发

环境要求

  • jdk1.8:Spring Boot 推荐jdk1.8及以上
  • Maven 3.x:推荐maven 3.2 以上版本
  • SpringBoot: 使用当前最新稳定版本
  • IntelliJ IDEA:推荐使用IntelliJ IDEA

Maven配置文件

在 Maven 安装目录下的 settings.xml 配置文件中, 添加如下配置

<!--开始处更改下载依赖的存放路径,以下目录需要已经创建-->
<localRepository>D:\javasource\maven-repository</localRepository>

<mirrors>
	<!--在  mirrors 标签下  添加阿里云maven私服库-->
    <mirror>  
		<id>alimaven</id>  
        <mirrorOf>central</mirrorOf>  
        <name>aliyun maven</name>  
        <url>http://maven.aliyun.com/nexus/content/groups/public/</url>  
        </mirror>
    </mirrors>

    <profiles>
        <!-- 在  profiles 标签下指定jdk版本  -->
        <profile>
          <id>jdk-1.8</id>
          <activation>
          	<activeByDefault>true</activeByDefault>
          	<jdk>1.8</jdk>
          </activation>
          <properties>
            <maven.compiler.source>1.8</maven.compiler.source>
            <maven.compiler.target>1.8</maven.compiler.target>
        	<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
          </properties>
    </profile>
</profiles>

IntelliJ IDEA 设置

打开setting,在搜索中输入Maven,点击Maven,在Maven home path中设置Maven安装路径;在user settings 把override打钩,选择Maven设置的setting.xml 文件
在这里插入图片描述

快速构建 Spring Boot 项目

浏览器发送 /hello 请求,服务器接受请求并处理,响应 Hello World 字符串
构建 Spring Boot 项目,事实上建立的就是一个 Maven 项目

创建 Maven工程

在 IDEA上新建一个空的jar类型 的 maven 工程

修改 pom.xml

  • 在 pom.xml 中添加 Spring Boot 相关的父级依赖, spring-boot-starter-parent 是一个特殊的starter,它提供了项目相关的默认依赖,使用它之后 ,常用的包依赖可以省去 version 标签。
  • 在 dependencies 添加构建 Web 项目相关的依赖
	<?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 https://maven.apache.org/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.7.9</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>springboot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>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>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
    

我们会发现,我们的工程自动添加了好多好多jar包, 这些 jar包正是开发时需要导入的jar包

创建控制器 Controller

package com.example.springboot.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @GetMapping("/hello")
    public String hello() {
        return "Hello, Spring Boot!";
    }
}

创建一个引导类

package com.example.springboot;

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

/**
 * @SpringBootApplication 用于标识一个主程序类,说明当前是Spring Boot项目
 */
@SpringBootApplication
public class SpringbootApplication {

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

}

运行效果

在这里插入图片描述
在浏览器地址栏输入localhost:8080/hello即可看到运行结果(默认端口是8080),由于端口被占用,我这里换了8981端口,可在配置文件中修改端口
在这里插入图片描述

简化部署

在 pom.xml 添加如下插件后, 将这个工程打成 jar 包后,可直接通过 java -jar 的命令运行

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

氵我是大明星

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

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

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

打赏作者

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

抵扣说明:

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

余额充值