SpringBoot(一、入门)

本文介绍了SpringBoot的特点及其如何简化Spring应用的开发流程。通过一个Hello World案例,详细讲解了项目搭建步骤,包括依赖配置及常见问题解决方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、SpringBoot介绍

Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can “just run”. We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration.

Features

  • Create stand-alone Spring applications
  • Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)
  • Provide opinionated ‘starter’ POMs to simplify your Maven configuration
  • Automatically configure Spring whenever possible
  • Provide production-ready features such as metrics, health checks and externalized configuration
  • Absolutely no code generation and no requirement for XML configuration

二、入门Demo(Helloworld)

(1)https://start.spring.io/ 从这上面下载demo(直接点击generate Project)
这里写图片描述
(2)eclipse –>inport–>Existing Maven Projects,导入(1)中下载的Maven项目(Demo)
如下图示:
这里写图片描述

(3)在pom.xml中配置以下依赖

<!-- 在生成的Demo里,parent已经存在,不需要重复配置  -->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.8.RELEASE</version>
</parent>
<dependencies>
<!--以下依赖需要配置-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

(4)创建SampleController.java类,创建完成后会报错,因为找不到framework下相关的包。

package hello;

import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;

@Controller
@EnableAutoConfiguration
public class SampleController {

    @RequestMapping("/")
    @ResponseBody
    String home() {
        return "Hello World!";
    }

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

(5)在eclipse里,project–>maven–>update project,下载项目依赖(注:需要配置好eclipse的maven环境)
下载依赖的jar后,第(3)中的错误将消失。

(6)点击这个类,右键 run as–>java application
console中打印如下内容,则表示启动成功:
这里写图片描述

(7)在浏览器中访问:
这里写图片描述

三、遇到的错误

1、eclipse的Maven配置不正确,无法下载依赖的jar
解决方法:重新配置Maven环境

2、run as–>java application 运行报错
这里写图片描述

问题原因:8080端口被占用导致
解决方法:
1、在CMD里,输入netstat -nao,打印端口使用情况
2、找到8080端口,查看对应PID
这里写图片描述
3、打开任务管理器,切到详细信息界面,找到PID对应的进程,结束此进程。
这里写图片描述
4、重新run as–>java application

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值