zoukankan      html  css  js  c++  java
  • Spring Boot(1)——开发你的第一款Spring Boot应用(Edition1)

    Spring Boot(1)——开发你的第一款Spring Boot应用(Edition1)

    • 准备工作:
      1. java:java 8 或者 java 9;
      2. Spring框架:5.0.8.RELEASE或以上;
      3. Maven 3.2+ 或 Gradle 4。

    • 开发步骤:
      1. 新建一个maven项目;
      2. 在pom文件中添加parent,以及spring-boot-starter-web【确保jar包都下载成功】;
        <?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
            <modelVersion>4.0.0</modelVersion>
        
            <groupId>com.example</groupId>
            <artifactId>myproject</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        
            <parent>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-parent</artifactId>
                <version>2.0.4.RELEASE</version>
            </parent>
        
            <!-- Additional lines to be added here... -->
            <dependencies>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-web</artifactId>
                </dependency>
            </dependencies>
        
        </project>
        View Code
      3. 编写应用的代码。
        import org.springframework.boot.*;
        import org.springframework.boot.autoconfigure.*;
        import org.springframework.web.bind.annotation.*;
        
        /**
         * 
         * @description 
         *         @RestController,固定类型注解,属于Spring MVC的注解。
         *             1、为人们阅读代码时提供提示;
         *             2、提示spring,这个类扮演着特殊的角色。
         *                 这里的Example就是一个web的@Controller,所以Spring处理即将到来的web请求的时候会考虑到它。
         * @author 
         *        LSH
         * @date
         *        2018年8月11日下午5:06:37
         *
         */
        @RestController
        /**
         * 
         * @description 
         *         @EnableAutoConfiguration,让Spring Boot基于你添加的jar包依赖来判断你想怎样配置Spring。
         *             这里,我们添加了spring-boot-starter-web,而它又添加了Tomcat和Spring MVC,
         *             所以自动配置会认为你是在开发一个web应用,并且完成相应的Spring配置。
         * @author 
         *        LSH
         * @date
         *        2018年8月11日下午5:07:13
         *
         */
        @EnableAutoConfiguration
        public class Example {
        
            /**
             * 
             * @Desc
             *     @RequestMapping,提供“路由”信息,属于Spring MVC的注解。
             *             它告诉Spring,任何以“/”为路径的HTTP请求都应该映射到home方法
             * 
             */
            @RequestMapping("/")
            String home() {
                return "Hello World!";
            }
        
            /**
             * 
             * @Desc
             *     这里的main方法,会把控制权通过调用Spring Boot的SpringApplication类的run方法委派给SpringApplication类,
             *     SpringApplication类启动我们的应用,启动Spring,接着启动自动配置好的Tomcat Web服务器;
             *     我们需要将Examle.class作为一个参数传给run方法,来告诉SpringApplication哪一个是Spring基本组件;
             *     args数组也传递过去了,用来暴露任何命令行参数
             * @param args
             */
            public static void main(String[] args){
                SpringApplication.run(Example.class, args);
            }
        
        }
        View Code 

      • 运行验证
        1. 直接运行Example类里的main方法
        2. 在浏览器里输入,localhost:8080,回车;
        3. 浏览器里看到,“hello, world”字样,项目运行成功!

    • 以上内容源自Spring Boot官方文档
  • 相关阅读:
    论文笔记:语音情感识别(五)语音特征集之eGeMAPS,ComParE,09IS,BoAW
    《Grammar and Punctuation》课堂笔记
    《Sequence Models》课堂笔记
    【翻译】学术写作中的数字
    生成器
    使用序列生成字典
    字典 (dictionary) 的默认值
    如何阅读文献 (How to Read a Paper)
    Python 列表的连接和联合
    《Convolutional Neural Networks》课堂笔记
  • 原文地址:https://www.cnblogs.com/InformationGod/p/9460296.html
Copyright © 2011-2022 走看看