栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java > SpringBoot

Spring Boot系列——5分钟构建一个应用

SpringBoot 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring Boot系列——5分钟构建一个应用

环境配置

1、JDK版本:Java8

2、IDE版本:Intellij IDEA 2018.1

3、系统:MAC OS

构建步骤1、创建项目

打开Intellij IDEA,点击File->New->Project

image

选择“Spring Initializer”,点击next

image

这步可以直接next,也可以填写自己需要的信息,然后next

image

无脑next

image

点击Finish,完成项目构建,生成初始项目及项目结构如下

image

maven文件

 
    4.0.0

    com.spring.boot
    tutorial
    0.0.1-SNAPSHOT
    jar

    tutorial
    Demo project for Spring Boot

    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.4.RELEASE
         
 

    
        UTF-8
        UTF-8
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
            org.springframework.boot
            spring-boot-starter-web
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

注意:

  • 自动生成的起步依赖,如spring-boot-starter、spring-boot-starter-test和spring-boot-starter-web。点击进入起步依赖spring-boot-starter,发现其依赖了spring-core等。spring-boot-starter-test依赖了junit、spring-test等,而spring-boot-starter-web则包括启动web应用所需要的依赖,比如spring-boot-starter-tomcat、spring-webmvc等(从这些实现细节可以看出Spring Boot是为了方便日常开发做了针对性抽象和封装,底层用的还是你熟悉的Spring)

  • build内容是声明部署插件,该pom定义该项目是以jar方式运行,其实也是可以通过war包方式运行的

TutorialApplication

该类是Spring Boot的核心注解,项目的启动入口。

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

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

注意:

  • 点击进入注解SpringBootApplication,你会发现,其本质是一个组合注解,包括@SpringBootConfiguration、@EnableAutoConfiguration和@ComponentScan等注解。具体各个注解类的作用后面有机会再说

  • Spring Boot项目启动是通过执行SpringApplication的静态方法run启动项目,该方法底层是初始化上下文并启动容器。

其他文件

TutorialApplicationTests文件和appication.properties文件都是空壳,可以按照需要实现。

2、启动项目

在TutorialApplication类上右键Run 'TutorialApplication'即可启动应用

image

添加Controller类

如果你觉得上面的效果太空洞了,需要验证,那么可以新建一个HelloController类

package com.spring.boot.tutorial.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController @RequestMapping("/")public class HelloController {

    @GetMapping(value = "/index")    public String index() {        return "hello jackie";

    }
}

此时重新运行TutorialApplication,打开浏览器,输入localhost:8080/index,效果如下

image



作者:Jackie_Zheng
链接:https://www.jianshu.com/p/fe4dda32a0e2


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/235680.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号