码迷,mamicode.com
首页 > 编程语言 > 详细

20分钟1快速上手Spring Boot应用

时间:2019-08-21 23:08:48      阅读:105      评论:0      收藏:0      [点我收藏+]

标签:class   绑定   tar   success   ram   tomcat端口   end   目录   prope   

20分钟快速上手Spring Boot应用

  • Spring Boot(简称SB)用于简化Spring应用的配置过程。
  • 采用“习惯优于配置”的方式开发
  • 学习SPB其实就是掌握它的各项约束与要求。

学习视频: http://www.itlaoqi.com/chapter/1683.html

源码地址: QQ群 814077650 , 群共享中自助下载

老齐的官网: itlaoqi.com (更多干货就在其中)


前置准备

  • JDK 8以上版本
  • 建议安装Intellj Idea Ultimate(旗舰版)
  • 会用Maven和Spring MVC

    Spring Boot目录结构

  • /java Java源代码目录
  • /resources 资源目录
  • /resources/static 静态资源目录
  • /resources/templates 表示层页面目录
  • /resources/application.properties Spring Boot配置文件
  • /test 测试文件目录

    Spring Boot开发过程总结

  1. 创建Maven工程,构建项目结构
  2. 配置pom.xml,引用各种starter启动器简化配置
  3. 配置运行参数
  4. 编码与测试
  5. 打包与独立运行

    1.创建Maven工程,构建项目结构

  • 新建Maven工程,例如:com.itlaoqi.myspringboot
  • 创建resources/templates目录,存放模板引擎
  • 创建resources/static目录,存放静态页面
  • 创建resources/application.properties,SPB核心配置文件

    2.打开pom.xml,引入依赖

  • spring-boot-starter-parent 所有Spring Boot组件的基础引用
  • spring-boot-starter-web 对SB应用提供Web的支持
  • spring-boot-starter-thymeleaf 提供thymeleaf模板引擎的支持
  • spring-boot-maven-plugin 提供SPB应用打包的功能

<?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.qiyi</groupId>
    <artifactId>myspringboot</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

3. 打开application.properties ,配置Tomcat端口

server.port=80

4. 创建测试用Controller,验证配置是否成功

package com.qiyi.myspringboot.controller;

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

@Controller
public class MyController {
    @RequestMapping("/out") //绑定到out地址
    @ResponseBody //直接向浏览器输出
    public String out(){
        return "success";
    }
}

5. 创建SpringBoot应用入口类

package com.qiyi.myspringboot;

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

//告诉SpringBoot我是一个入口类,运行我就能启动SB
//会自动扫描可以被注入的类,并初始化
//@Repository / @Service / @Controller / @Component / @Entity
@SpringBootApplication
public class MySpringBootApplication {
    public static void main(String[] args) {
        //启动SpringBoot,并初始化相关的组件
        SpringApplication.run(MySpringBootApplication.class);
    }
}

6. 启动运行MySpringBootApplication

7. 打开浏览器输入 localhost/out 地址,看到success字样说明配置成功

20分钟1快速上手Spring Boot应用

标签:class   绑定   tar   success   ram   tomcat端口   end   目录   prope   

原文地址:https://www.cnblogs.com/itlaoqi/p/11391679.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!