美文网首页
Spring:Hello World

Spring:Hello World

作者: 酸奶不错 | 来源:发表于2020-09-18 10:41 被阅读0次

一、Spring 与 Spring Boot

Spring 是什么?简单来说,Spring 就是让 Java 更好用的工具。

我们可以通过 start.spring.io 来创建项目,它内置了 Spring Boot。

Spring Boot 目前是创建 Spring 项目最快和最流行的方式。

二、使用 IDEA 创建 Spring 项目

最后,点击 Finish,等待 Maven 导入各种依赖包,初步完成准备工作。

三、项目的基本目录

初始化后大致目录如上,不同的 IDEA 版本可能有所差别,我这里删掉了 .mvnHELP.mdmvnwmvnw.cmd 四个文件,因为我暂时用不上,好像还有个 test 目录也被删了。

目前我们只关心上图中标记的三处,其他的暂不研究。

  1. 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <!-- 继承Springboot父级项目的依赖 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <!-- 关于项目的说明 -->
    <groupId>cc.sneaky</groupId>
    <artifactId>xiaomingweb</artifactId>
    <version>0.0.1</version>
    <packaging>war</packaging>
    <name>xiaomingweb</name>
    <description>Personal website for xiaoming</description>

    <!-- 属性配置 -->
    <properties>
        <java.version>1.8</java.version>
    </properties>

    <!-- 项目的依赖配置 -->
    <dependencies>
        <!-- Springboot开发Web项目的起步依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- Springboot提供的项目编译打包插件 -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

  1. application.properties

这个主要是项目的一些属性配置,如数据库连接信息。默认为空,暂时不用管。

  1. Application.java

Spring 程序的启动类,main 方法就在这个类里,我的根据项目名叫做 XiaomingwebApplication.java

package cc.sneaky.xiaomingweb;

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

@SpringBootApplication      //此注解,表明这是一个Springboot应用程序
public class XiaomingwebApplication {

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

四、Hello World

修改 Application.java 的代码,如下:

package cc.sneaky.xiaomingweb;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication      //此注解,表明这是一个Springboot应用程序
@RestController
public class XiaomingwebApplication {

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

    @GetMapping("/hello")
    public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
        return String.format("Hello %s!", name);
    }

}

运行程序:

在地址栏输入http://localhost:8080/hello,即可在页面上看到 Hello World! 字样,这样一个最基础的 Spring 项目就完成了。

相关文章

  • 易百教程

    https://www.yiibai.com/spring/spring-3-hello-world-exampl...

  • Spring Hello World

    今天开始学习 Java 的 Spring 框架,从头开始学,一点点的记录所学的内容,一点点点入门。 Spring ...

  • Spring:Hello World

    一、Spring 与 Spring Boot Spring 是什么?简单来说,Spring 就是让 Java 更好...

  • Spring 3 MVC hello world example

    In this tutorial, we show you a Spring 3 MVC hello world ...

  • Spring @cacheable

    1. Hello cache 1.1. spring cache XML配置 1.2. hello world 调...

  • Spring Boot 学习笔记

    Spring Boot 学习笔记 源码地址 Spring Boot 学习笔记(一) hello world Spr...

  • 常用markdown语法

    Hello World! Hello World! Hello World! Hello World! Hello...

  • hello

    hello, world hello, world hello, world hello, world

  • Markdown

    标题: hello world hello world hello world hello world hello...

  • 2018-06-11

    markdown hello world hello world hello world hello world ...

网友评论

      本文标题:Spring:Hello World

      本文链接:https://www.haomeiwen.com/subject/tfssjktx.html