SpringBoot - 初识

SpringBoot是什么?

它是建立在Spring框架基础之上的一个项目,提供了快速且简单的方式来开发、配置和运行简单或Web应用。

简单讲,它是Spring框架和内嵌服务器的组合,并且它使用约定大于配置的软件设计范式,大大简化了开发。

创建第一个简单应用

1. 新建一个maven工程,增加springboot的父pom

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

2. 增加web的jar包引用

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

3. 增加springboot的打包的maven插件,打包成一个可执行的jar包

<plugins>
  <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
      <mainClass>com.exmaple.MyApp</mainClass>
      <outputDirectory>target</outputDirectory>
    </configuration>
    <executions>
      <execution>
        <goals>
          <goal>repackage</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
</plugins>

整体的maven的pom.xml如下:

<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>SpringBootTest</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

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

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

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

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
          <mainClass>com.exmaple.MyApp</mainClass>
          <outputDirectory>target</outputDirectory>
        </configuration>
        <executions>
          <execution>
            <goals>
              <goal>repackage</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

4. 添加main方法, 主类MyApp上面增加@SpringBootApplication注解

@SpringBootApplication
public class MyApp {
    public MyApp() {
    }

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

5. 添加controller

@RestController
public class UserController {
    public UserController() {
    }

    @GetMapping({"/hello"})
    public String hello() {
        return "hello, world";
    }
}

整体项目结构如下:

 6. 执行maven打包,打包成jar包在target目录下面,通过java -jar SpringBootTest-1.0-SNAPSHOT.jar命令运行jar包。

 7. 浏览器运行,看返回结果

8. 总结

是不是很简单几步,一个可执行的Web应用程序就搞好了,这里省去了很多以前MVC需要的配置以及生成的war包后的部署。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值