SpringBoot--HelloWorld

本博客主要记录SpringBoot学习之路
从HelloWorld到SpringBoot实战



1. 创建一个SpringBoot项目

这里看图完成配置

在这里插入图片描述
在这里插入图片描述

2. SpringBoot相关的依赖

pop.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 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.2.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.xizi</groupId>
    <artifactId>springboot-01-helloworld1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-01-helloworld1</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</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-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>


    <!--这个插件,可以将应用打包成一个可执行的jar 包-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

3. SpringBoot主程序,启动SpringBoot

SpringBootApplication 启动类

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

/**
 *  @SpringBootApplication ;来标记一个主程序类 ,说明这是一个SpringBoot应用
 */

@SpringBootApplication
public class Springboot01Helloworld1Application {

    public static void main(String[] args) {
        //spring 应用启动起来
        SpringApplication.run(Springboot01Helloworld1Application.class, args);
    }
}

4. 编写Controller层

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("hello")
public class HelloController {
    @GetMapping("hello")
    @ResponseBody
    public String hello(){
        return "Hello,SpringBoot!";
    }
}

5. SpringBoot项目结构

在这里插入图片描述

6. Springboot项目启动

在这里插入图片描述

7.测试接口

在这里插入图片描述

8.SpringBoot 打成jar包,并运行

-这个插件,可以将应用打包成一个可执行的jar 包

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

在这里插入图片描述

java -jar 直接运行

在这里插入图片描述

9. Helloworld探究

//按住ctrl键  鼠标点击进入源码查看
   <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    //他的父类  管理各种技术的依赖版本
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.2.7.RELEASE</version>
    <relativePath>../../spring-boot-dependencies</relativePath>
  </parent>

10. 启动器

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

spring-boot-starter:spring-boot场景启动器;帮我们导入了web模块正常运行所依赖的组件;

11. @SpringBootApplication深入理解

@SpringBootApplication: Spring Boot应用标注在某个类上说明这个类是SpringBoot的主配置类

点进@SpringBootApplication源码查看
在这里插入图片描述

点进@SpringBootConfiguration配置类
在这里插入图片描述


点进@EnableAutoConfiguration
在这里插入图片描述

Spring Boot在启动的时候从类路径下的META-INF/spring.factories中获取
EnableAutoConfiguration指定的值,将这些值作为自动配置类导入到容器中,自动
配置类就生效,帮我们进行自动配置工作;以前我们需要自己配置的东西,自动配置类都帮我们;

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值