zoukankan      html  css  js  c++  java
  • Spring Boot 2 入门


    Spring Boot其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。

    参考网上资料,一路踩了几个坑,终于搞出了第一个例子。

    1、访问http://start.spring.io
    我选择的是Spring Boot 2.0.5,点击Generate Project下载项目压缩包。

    2、解压后,eclipse,Import -> Existing Maven Projects -> Next ->选择解压后的文件夹-> Finsh。

    项目结构如下:

    3、pom.xml中添加支持web的模块:

    <dependency>
    	<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    一定要添加上面模块,否则使用@RestController注解的时候提示”RestController cannot be resolved to a type”。

    说明:
    Spring Boot 提供了很多starter模块,在项目中加入对应框架的starter依赖,可以免去到处寻找依赖包的烦恼。
    官方starter模块命名规则为“spring-boot-starter-*”,其中*代表对应的类型。
    常用的starter模块:
    spring-boot-starter-web:构建Web应用,包含Spring MVC框架,默认内嵌Tomcat窗口。
    spring-boot-starter-jpa:构建Spring Data JPA应用,使用Hibernate作为ORM框架。
    spring-boot-starter-test:用于单元测试。
    spring-boot-starter-redis:构建Spring Data Redis应用,使用Jedis框架操作Redis数据库。
    spring-boot-starter-thymeleaf:构建一个使用Thymeleaf作为视图的Web应用。


    4、编写第一个HelloWorld例子

    @RestController
    public class HelloWorldController {
    	
        @RequestMapping("/")
        public String index() {
            return "Hello World!";
        }
    }
    

      

    5、修改默认的端口号:

    由于本机的8080已经被使用,所以修改一下端口号,打开resources目录的application.properties文件,里面加入自定义端口号(这里使用9001):

    server.port = 9001


    6、如何启动Spring Boot

    方法一:

    在SpringBoot项目中找到 有@SpringBootApplication注解的文件,即启动文件,本项目为DemoApplication.java文 件,右键 Run As -> Java Application

    package com.example.demo;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class DemoApplication {
    
    	public static void main(String[] args) {
    		SpringApplication.run(DemoApplication.class, args);
    	}
    }
    

    方法二:

    项目 -> 右键 -> Run As -> Maven build -> Goals里面输入spring-boot:run -> 点击run按钮。


    启动后,Console窗口的输出有一行

    [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 9001 (http) with context path ''

    说明项目正常启动,端口号为9001。

    访问:
    http://localhost:9001/
    页面显示:Hello World!


    8、实现热部署模式

    (1)pom.xml文件增加 

    <dependency>
    	 <groupId>org.springframework.boot</groupId>
    	 <artifactId>spring-boot-devtools</artifactId>
    	 <optional>true</optional>
    </dependency>
    

    (2)工程配置

    Project -> Build Automatically 选中

    9、其它 (20191104添加)

    (1)pom.xml添加依赖时,可以不指定版本,“spring-boot-starter-parent”会提供常用jar的版本管理,也可以指定,会覆盖官方默认的版本。
    依赖版本查询地址:https://mvnrepository.com/
    搜索框输入名称:如ribbon,结果如下:

    点击列表的Ribbon进去,出现了所有版本列表

  • 相关阅读:
    angular组件开发
    APICloud案例源码、模块源码、考试源码、开发工具大集合!赶快收藏
    Vue-router实现单页面应用在没有登录情况下,自动跳转到登录页面
    原生js实现架子鼓特效
    A Beginner’s Introduction to CSS Animation中文版
    react开发教程(三)组件的构建
    体验javascript之美第五课 五分钟彻底明白 匿名函数自执行和闭包
    这些例子很炫,感兴趣的童鞋可以了解一下
    体验javascript之美6:如果你觉得什么都会了或者不知道js学什么了看这里-面向对象编程
    傅里叶变换,小波变换,EMD,HHT,VMD(经典和现代信号处理方法基本原理概念)
  • 原文地址:https://www.cnblogs.com/gdjlc/p/9708402.html
Copyright © 2011-2022 走看看