zoukankan      html  css  js  c++  java
  • SpringBoot初探

    一:项目创建

    个人用的是IDEA来做Demo的;

    1.先创建一个空项目,然后创建一个基于Maven的java application项目;

    2.创建好后配置pom.xml文件并最终reimport

    <?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>me.silentdoer</groupId>
        <artifactId>spring-boot-test</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <!-- 配置parent,这个就像applicationContext.xml里配置的一样,作为其它子元素的抽象父元素,但是允许子元素重写配置 -->
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <!-- 注意大小写 -->
            <version>1.5.7.RELEASE</version>
            <relativePath/>
        </parent>
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.build.outputEncoding>UTF-8</project.build.outputEncoding>
            <java.version>1.8</java.version>
            <bootgi>org.springframework.boot</bootgi>
        </properties>
        <dependencies>
            <dependency>
                <!-- 由于上面配置了parent,故这里只需要配置那些需要覆写的即可(groupId还是得有),这个包里有包括web所需的所有SpringMVC的jar包 -->
                <groupId>${bootgi}</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
        </dependencies>
        <build>
            <plugins>
                <plugin>
              <groupId>${bootgi}</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>

    3.创建me.silentdoer.springboot.MySpringBootApplication类

    package me.silentdoer.springboot;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    /**
     * @author silentdoer
     * @version 1.0
     * @description the description
     * @date 4/12/18 7:17 PM
     */
    
    /**
     * TODO 此注解指定下面的类是springboot的启动类,然后SpringApplication.run(...)时会自动去加载依赖的jar包/模块等(如tomcat)
     * 顺便还从这个注解的源码里学到了创建注解对象时还可以通过“构造方法”初始化“属性”,然后注解的声明里还可以声明内部注解
     */
    @SpringBootApplication
    public class MySpringBootApplication {
        public static void main(String[] args){
            // TODO 注意,tomcat本质上是一个java application,这里内部实际上是运行了一个tomcat
            // TODO 实现原理可以看:org.apache.catalina.startup.Bootstrap.main()方法
            SpringApplication.run(MySpringBootApplication.class, args);
        }
    }

    4.创建一个Controller类为me.silentdoer.springboot.controller.TestController

    package me.silentdoer.springboot.controller;
    
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
     * @author silentdoer
     * @version 1.0
     * @description the description
     * @date 4/12/18 8:02 PM
     */
    @RestController  // 使得不返回视图
    public class TestController {
    
        // RESTFul api with get method.
        @GetMapping("/hello")  // 可以理解为RequestMethod.GET的RequestMapping,这里默认就是返回字符串而不需要viewResolver,如果需要要配置pom和application.properties
        public String hello(){
            return "hello.";
        }
    }

    到这一步就可以对MySpringBootApplication run as java application了,然后浏览器里输入http://localhost:8080/hello得到输出hello.说明第一个SpringBoot程序创建成功;

    二:SpringBoot中的一些特殊文件和目录

    下面的内容全都是在resources下,即就是classes根目录下

    1.application.properties文件,用于配置SpringBoot,但并不是applicationContext.xml一样的角色,里面可以做一些viewResolver/热部署等的配置

    2.static目录(public也行),静态文件存放的目录(有那么点像webapp的角色,放在这里面的静态文件可以直接被访问)

    注:比如我在static里有个test.html,那么访问http://localhost:8080/test.html最终能够访问到此静态页面,但是前提是test这个关键字不被Mapping到才最终去找static目录,即如果我有个@GetMapping("/test")那么是会优先被对应的handler方法拦截处理;

    3.templates目录,存放模版文件(即jsp/freemaker之类的动态页面,不过SpringBoot官方不建议使用jsp而是默认用Thymeleaf来做动态页面)

    三:SpringBoot的热部署

    当我们做一些文件的更新操作时,需要重新启动项目才能令我们的更新生效。但是这种方式太浪费时间了,通过热部署可以让项目自动去探测那些变化文件并重新编译;

    IDEA需要做一些额外操作,具体看:https://blog.csdn.net/wjc475869/article/details/52442484;

    1.先添加devtools依赖

    <dependency>
                <groupId>${bootgi}</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <optional>true</optional>
                <scope>true</scope>
            </dependency>

    2.在application.properties文件里添加

    # 当classpath中有文件被devtools探测到有更新,则向SpringBoot发送restart的请求从而重启tomcat和重新加载Controller等bean
    spring.devtools.restart.enabled=true

    至此热部署便配置好了,只要修改了classpath中的文件都会导致SpringBoot的restart,这种方式只适合开发或测试环境,部署环境不能全部重启而应该只重新加载部分文件或redeploy,等有时间再查一下时候有相应配置(看了下有个livereload不知道是不是);

    四:待续

  • 相关阅读:
    Array.sort()对象数组排序
    ES6极简数组去重 & 深拷贝
    中文字符按拼音首字母排序(转)
    浏览器常用插件 & 开发常用类库
    异步&分段处理海量数据
    线上环境window.open()新窗口被拦截(转)
    git 版本回退(转)
    CSS字体渐变 & 隐藏浏览器滚动条 & grid布局(转载)
    Python3安装使用SaltStack以及salt-api详解
    Python3之 contextlib
  • 原文地址:https://www.cnblogs.com/silentdoer/p/8810316.html
Copyright © 2011-2022 走看看