• Spring Boot


    Spring Boot

    https://www.cnblogs.com/qyf404/p/learning_spring_boot.html

    Spring boot 是什么 ?
    简单说, spring boot 是一个构建项目的工具, 一个脚手架.

    Spring boot 能干什么?
    spring boot 做非常少的配置就可以构建生产级别的单体应用.

    Spring boot 怎么干的?
    下面让我们来用spring boot 做一个hello world.

    环境准备, 需要保证你的机器上已经有了如下软件

    JDK1.8+
    gradle4+
    创建项目目录, 假设这个新的项目叫apple.

    $ cd /tmp
    $ mkdir apple
    创建gradle配置文件,在这个目录下创建build.gradle文件.

    $ cd apple
    $ vi build.gradle
    将如下代码复制到build.gradle文件中.

    plugins {
    id 'org.springframework.boot' version '2.0.1.RELEASE'
    }

    apply plugin: 'java'
    apply plugin: 'io.spring.dependency-management'

    repositories {
    mavenCentral()
    }

    dependencies {
    compile 'org.springframework.boot:spring-boot-starter-web'
    testCompile 'org.springframework.boot:spring-boot-starter-test'
    testCompile 'org.springframework.boot:spring-boot-starter-webflux'
    }

    bootRun {
    main = 'com.thoughtworks.apple.Launcher'
    }
    写一个Hello world.

    按照如下目录结构初始化项目内容
    apple
    -src
    -main
    -java
    -com.thoughtworks.apple
    |-controller
    | -HomeController.java
    -Launcher.java
    HomeController.java内容如下
    package com.thoughtworks.apple.controller;

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

    @RestController
    public class HomeController {
    @RequestMapping("/")
    String home() {
    return "Hello World!";
    }
    }
    Launcher.java内容如下
    package com.thoughtworks.apple;

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.SpringBootConfiguration;
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    import org.springframework.context.annotation.ComponentScan;

    @SpringBootConfiguration
    @EnableAutoConfiguration
    @ComponentScan("com.thoughtworks.apple")
    public class Launcher {
    public static void main(String[] args) {
    SpringApplication.run(Launcher.class, args);
    }
    }
    运行

    执行命令gradle bootRun启动应用,之后会打印如下日志:

    $ gradle bootRun

    Task :bootRun

    . ____ _ __ _ _
    / / ' __ _ () __ __ _
    ( ( )_
    _ | '_ | '| | ' / ` |
    / )| |)| | | | | || (| | ) ) ) )
    ' |
    | .__|| ||| |__, | / / / /
    =|_|======|/=////
    :: Spring Boot :: (v2.0.1.RELEASE)

    2018-05-05 10:23:41.383 INFO 38941 --- [ main] com.thoughtworks.apple.Launcher : Starting Launcher on CNyfqi.local with PID 38941 (/private/tmp/apple/build/classes/java/main started by yfqi in /private/tmp/apple)
    2018-05-05 10:23:41.386 INFO 38941 --- [ main] com.thoughtworks.apple.Launcher : No active profile set, falling back to default profiles: default
    2018-05-05 10:23:41.432 INFO 38941 --- [ main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@3b2c72c2: startup date [Sat May 05 10:23:41 CST 2018]; root of context hierarchy
    2018-05-05 10:23:42.289 INFO 38941 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
    2018-05-05 10:23:42.313 INFO 38941 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
    2018-05-05 10:23:42.313 INFO 38941 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.29
    2018-05-05 10:23:42.323 INFO 38941 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/Users/yfqi/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.]
    2018-05-05 10:23:42.402 INFO 38941 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
    2018-05-05 10:23:42.403 INFO 38941 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 973 ms
    2018-05-05 10:23:42.522 INFO 38941 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/]
    2018-05-05 10:23:42.525 INFO 38941 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/]
    2018-05-05 10:23:42.526 INFO 38941 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/
    ]
    2018-05-05 10:23:42.526 INFO 38941 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/]
    2018-05-05 10:23:42.526 INFO 38941 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/
    ]
    2018-05-05 10:23:42.627 INFO 38941 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [//favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
    2018-05-05 10:23:42.809 INFO 38941 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@3b2c72c2: startup date [Sat May 05 10:23:41 CST 2018]; root of context hierarchy
    2018-05-05 10:23:42.878 INFO 38941 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/]}" onto java.lang.String com.thoughtworks.apple.controller.HomeController.home()
    2018-05-05 10:23:42.884 INFO 38941 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
    2018-05-05 10:23:42.885 INFO 38941 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
    2018-05-05 10:23:42.910 INFO 38941 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/
    ] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
    2018-05-05 10:23:42.910 INFO 38941 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
    2018-05-05 10:23:43.048 INFO 38941 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
    2018-05-05 10:23:43.100 INFO 38941 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
    2018-05-05 10:23:43.105 INFO 38941 --- [ main] com.thoughtworks.apple.Launcher : Started Launcher in 2.148 seconds (JVM running for 2.523)
    2018-05-05 10:24:03.416 INFO 38941 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
    2018-05-05 10:24:03.416 INFO 38941 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
    2018-05-05 10:24:03.436 INFO 38941 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 20 ms
    <=========----> 75% EXECUTING [1m 28s]

    :bootRun
    之后在浏览器中访问http://localhost:8080/ 能看到如下页面说明程序运行成功了.

    进阶
    如何将gradle命令引入项目
    当我们本地有多个项目, 且每个项目的gradle版本不一样时, 我们就需要把gradle命令保存在项目内, 然后每个项目用自己的gradle来构建. 下面这个命令可以直接初始化引入一个gradle命令到项目内部.

    $ gradle wrapper --gradle-version 4.0
    之后项目目录下会多出四个文件

    drwxr-xr-x 4 yfqi wheel 128 May 5 10:49 .gradle
    drwxr-xr-x 3 yfqi wheel 96 May 5 10:49 gradle
    -rwxr-xr-x 1 yfqi wheel 5296 May 5 10:49 gradlew
    -rw-r--r-- 1 yfqi wheel 2260 May 5 10:49 gradlew.bat
    以后就可以在项目根目录下执行 ./gradlew来运行gradle的tasks了.

    测试Hello World
    程序成功运行了, 让我们来写一个测试用例测试一下hello world.

    测试目录结构如下

    apple
    -src
    |-main
    -test
    -java
    -com.thoughtworks.apple.controller
    -HomeControllerTest.java
    HomeControllerTest.java内容如下

    package com.thoughtworks.apple.controller;

    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    import org.springframework.test.web.reactive.server.WebTestClient;

    @RunWith(SpringRunner.class)
    @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
    public class HomeControllerTest {
    @Autowired
    private WebTestClient webClient;

    @Test
    public void should_get_home() {
        this.webClient.get().uri("/").exchange()
                .expectStatus().isOk()
                .expectBody(String.class).isEqualTo("Hello World!");
    }
    

    }
    运行测试使用命令./gradlew test能看到如下日志说明测试执行成功

    $ gradle test
    Starting a Gradle Daemon (subsequent builds will be faster)

    Task :test
    2018-05-05 10:36:18.034 INFO 39291 --- [ Thread-6] ConfigServletWebServerApplicationContext : Closing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@918accb: startup date [Sat May 05 10:36:15 CST 2018]; root of context hierarchy

    BUILD SUCCESSFUL in 12s
    3 actionable tasks: 3 executed
    打包部署
    执行命令./grdlew bootJar来打包spring boot项目. 命令执行成功后会在apple/build/libs下多出一个jar包apple.jar.这就打包成功了.
    把打包好的jar包复制到运行的服务器, 执行命令java -jar apple.jar &就可以把应用启动起来.
    Spring Boot Starters
    上面的例子大家会发现项目只是依赖了三个Spring Boot的Starter就完成了对所有第三方jar包的引用.Starter其实就是对第三方jar包依赖按照业务需要做了一个聚合,Spring Boot提供了大量的Starter来帮助开发人员快速开始构建项目, 降低了开发人员调包依赖的工作量.在这两个地址可以查看Spring Boot的所有的Starters.

    https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#using-boot-starter
    https://github.com/spring-projects/spring-boot/tree/master/spring-boot-project/spring-boot-starters
    源码
    文章的完整代码可以在这里找到: https://github.com/qyf404/learning-spring-boot

    在Windows上安装Gradle

    https://blog.csdn.net/wellplaying/article/details/78616520

    1.开发环境

    (1)Java:JDK8(必须是JDK或JRE7以上,使用java -version查看当前电脑java版本)

    (2)操作系统:Windows 7

    2.安装步骤
    

    (1)下载最新的Gradle压缩包

    Gradle官网:https://gradle.org/

    当前最新版本下载地址:https://gradle.org/releases/,下载binary-only即可

    (2)解压下载的压缩包到指定位置,比如:

    C:Gradlegradle-4.3.1

    (3)配置环境变量

    右键计算机->属性->高级系统设置->环境变量,在系统变量区域,点击新建,输入变量名为:GRADLE_HOME,变量值为:D:SoftwareGradlegradle-4.3.1(根据实际情况)。找到Path,在原始内容基础上加入%GRADLE_HOME%in;,必须以分号结束

    (3)测试配置是否成功

    打开一个新的cmd命令窗口或者powershell或者其他命令行工具(比如cmder),输入命令gradle -v,如果出现类似下面的消息,则说明配置成功

    [html] view plain copy
    C:WindowsSystem32>gradle -v


    Gradle 4.3.1

    Build time: 2017-11-08 08:59:45 UTC
    Revision: e4f4804807ef7c2829da51877861ff06e07e006d

    Groovy: 2.4.12
    Ant: Apache Ant(TM) version 1.9.6 compiled on June 29 2015
    JVM: 1.8.0_131 (Oracle Corporation 25.131-b11)
    OS: Windows 7 6.1 amd64
    到此为止,Gradle安装结束
    关于作者

    作者:辵鵵
    出处:http://qyf404.cnblogs.com

  • 相关阅读:
    教你修改Linux下高并发socket最大连接数所受的各种限制
    nginx浏览pdf
    Spring3 M2 quartz-2.1.7 解决bean不能注入问题
    DCSync
    Linux下python2.7安装pip
    ._cache_问题
    php-mvc概念
    php第十天-面向对象命名空间
    php第九天-session/cookice会话控制
    fastadmin V1.0.0.20200506_beta 漏洞复现
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/9025350.html
Copyright © 2020-2023  润新知