• Spring In Action 5 学习笔记 chapter1


    环境

    win7x64

    idea 2018.3

    maven 3.3.9

    spring 4.0.4

    很久没摸spring boot了,idea还是2018的,生疏了,抓起来再看看。【我亦无他,惟手熟尔】

    IDEA搭建一个Spring boot项目

    概念厘清:SpringBoot基本上是 Spring框架的扩展。参考Spring和SpringBoot区别

    方式一:IDEA内选择Spring Initializr

    建立空工程-简略关键步骤

    1. IDEA选择New Project,创建项目时Artifact 需要小写字母,否则提示错误
    2. 左侧选spring-Initializr。
    3. 因国内网络限制,默认的url无法自动成功生成,需要将https://start.spring.io改为https://start.aliyun.com
    4. 选择spring boot版本
    5. 选择web->Spring Web
    6. 其他选择 例如Dev Tool或Thymeleaf

    建好的pom.xml如下

    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <modelVersion>4.0.0modelVersion>
    5. <groupId>com.wdh.springinactiongroupId>
    6. <artifactId>springbyinitializr2artifactId>
    7. <version>0.0.1-SNAPSHOTversion>
    8. <name>springbyinitializr2name>
    9. <description>Demo project for Spring Bootdescription>
    10. <properties>
    11. <java.version>1.8java.version>
    12. <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    13. <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
    14. <spring-boot.version>2.3.7.RELEASEspring-boot.version>
    15. properties>
    16. <dependencies>
    17. <dependency>
    18. <groupId>org.springframework.bootgroupId>
    19. <artifactId>spring-boot-starter-thymeleafartifactId>
    20. dependency>
    21. <dependency>
    22. <groupId>org.springframework.bootgroupId>
    23. <artifactId>spring-boot-starter-webartifactId>
    24. dependency>
    25. <dependency>
    26. <groupId>org.springframework.bootgroupId>
    27. <artifactId>spring-boot-devtoolsartifactId>
    28. <scope>runtimescope>
    29. <optional>trueoptional>
    30. dependency>
    31. <dependency>
    32. <groupId>org.springframework.bootgroupId>
    33. <artifactId>spring-boot-starter-testartifactId>
    34. <scope>testscope>
    35. <exclusions>
    36. <exclusion>
    37. <groupId>org.junit.vintagegroupId>
    38. <artifactId>junit-vintage-engineartifactId>
    39. exclusion>
    40. exclusions>
    41. dependency>
    42. dependencies>
    43. <dependencyManagement>
    44. <dependencies>
    45. <dependency>
    46. <groupId>org.springframework.bootgroupId>
    47. <artifactId>spring-boot-dependenciesartifactId>
    48. <version>${spring-boot.version}version>
    49. <type>pomtype>
    50. <scope>importscope>
    51. dependency>
    52. dependencies>
    53. dependencyManagement>
    54. <build>
    55. <plugins>
    56. <plugin>
    57. <groupId>org.apache.maven.pluginsgroupId>
    58. <artifactId>maven-compiler-pluginartifactId>
    59. <version>3.8.1version>
    60. <configuration>
    61. <source>1.8source>
    62. <target>1.8target>
    63. <encoding>UTF-8encoding>
    64. configuration>
    65. plugin>
    66. <plugin>
    67. <groupId>org.springframework.bootgroupId>
    68. <artifactId>spring-boot-maven-pluginartifactId>
    69. <version>2.3.7.RELEASEversion>
    70. <configuration>
    71. <mainClass>com.wdh.springinaction.springbyinitializr2.Springbyinitializr2ApplicationmainClass>
    72. configuration>
    73. <executions>
    74. <execution>
    75. <id>repackageid>
    76. <goals>
    77. <goal>repackagegoal>
    78. goals>
    79. execution>
    80. executions>
    81. plugin>
    82. plugins>
    83. build>
    84. project>

    application.properties文件内容如下

    1. # 应用名称
    2. spring.application.name=springbyinitializr2
    3. # 应用服务 WEB 访问端口
    4. server.port=8080
    5. # THYMELEAF (ThymeleafAutoConfiguration)
    6. # 开启模板缓存(默认值: true )
    7. spring.thymeleaf.cache=true
    8. # 检查模板是否存在,然后再呈现
    9. spring.thymeleaf.check-template=true
    10. # 检查模板位置是否正确(默认值 :true )
    11. spring.thymeleaf.check-template-location=true
    12. #Content-Type 的值(默认值: text/html )
    13. spring.thymeleaf.content-type=text/html
    14. # 开启 MVC Thymeleaf 视图解析(默认值: true )
    15. spring.thymeleaf.enabled=true
    16. # 模板编码
    17. spring.thymeleaf.encoding=UTF-8
    18. # 要被排除在解析之外的视图名称列表,⽤逗号分隔
    19. spring.thymeleaf.excluded-view-names=
    20. # 要运⽤于模板之上的模板模式。另⻅ StandardTemplate-ModeHandlers( 默认值: HTML5)
    21. spring.thymeleaf.mode=HTML5
    22. # 在构建 URL 时添加到视图名称前的前缀(默认值: classpath:/templates/ )
    23. spring.thymeleaf.prefix=classpath:/templates/
    24. # 在构建 URL 时添加到视图名称后的后缀(默认值: .html )
    25. spring.thymeleaf.suffix=.html

    目录结构

    创建MVC应用

    简要步骤

    1. 编写Controller(HomeController.java)
    2. 编写html页码(使用Thymeleaf模板编写home.html)
    3. IDEA启动运行(或启动调试),在IDEA的控制台console小窗口中显示如下字样【 Tomcat started on port(s): 8080 (http) with context path ''】表示成功
      1. "C:\Program Files\Java\jdk1.8.0_111\bin\java.exe" -XX:TieredStopAtLevel=1 -noverify -Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true "-javaagent:D:\JavaDevEnv\JetBrains\IntelliJ IDEA 2018.3.6\lib\idea_rt.jar=2368:D:\JavaDevEnv\JetBrains\IntelliJ IDEA 2018.3.6\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_111\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\rt.jar;D:\JavaWorkspace\IdeaProjects\SpringInAction\springbyinitializr2\target\classes;D:\JavaDevEnv\maven_repository\org\springframework\boot\spring-boot-starter-thymeleaf\2.3.7.RELEASE\spring-boot-starter-thymeleaf-2.3.7.RELEASE.jar;D:\JavaDevEnv\maven_repository\org\springframework\boot\spring-boot-starter\2.3.7.RELEASE\spring-boot-starter-2.3.7.RELEASE.jar;D:\JavaDevEnv\maven_repository\org\springframework\boot\spring-boot-starter-logging\2.3.7.RELEASE\spring-boot-starter-logging-2.3.7.RELEASE.jar;D:\JavaDevEnv\maven_repository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;D:\JavaDevEnv\maven_repository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;D:\JavaDevEnv\maven_repository\org\apache\logging\log4j\log4j-to-slf4j\2.13.3\log4j-to-slf4j-2.13.3.jar;D:\JavaDevEnv\maven_repository\org\apache\logging\log4j\log4j-api\2.13.3\log4j-api-2.13.3.jar;D:\JavaDevEnv\maven_repository\org\slf4j\jul-to-slf4j\1.7.30\jul-to-slf4j-1.7.30.jar;D:\JavaDevEnv\maven_repository\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;D:\JavaDevEnv\maven_repository\org\yaml\snakeyaml\1.26\snakeyaml-1.26.jar;D:\JavaDevEnv\maven_repository\org\thymeleaf\thymeleaf-spring5\3.0.11.RELEASE\thymeleaf-spring5-3.0.11.RELEASE.jar;D:\JavaDevEnv\maven_repository\org\thymeleaf\thymeleaf\3.0.11.RELEASE\thymeleaf-3.0.11.RELEASE.jar;D:\JavaDevEnv\maven_repository\org\attoparser\attoparser\2.0.5.RELEASE\attoparser-2.0.5.RELEASE.jar;D:\JavaDevEnv\maven_repository\org\unbescape\unbescape\1.1.6.RELEASE\unbescape-1.1.6.RELEASE.jar;D:\JavaDevEnv\maven_repository\org\slf4j\slf4j-api\1.7.30\slf4j-api-1.7.30.jar;D:\JavaDevEnv\maven_repository\org\thymeleaf\extras\thymeleaf-extras-java8time\3.0.4.RELEASE\thymeleaf-extras-java8time-3.0.4.RELEASE.jar;D:\JavaDevEnv\maven_repository\org\springframework\boot\spring-boot-starter-web\2.3.7.RELEASE\spring-boot-starter-web-2.3.7.RELEASE.jar;D:\JavaDevEnv\maven_repository\org\springframework\boot\spring-boot-starter-json\2.3.7.RELEASE\spring-boot-starter-json-2.3.7.RELEASE.jar;D:\JavaDevEnv\maven_repository\com\fasterxml\jackson\core\jackson-databind\2.11.3\jackson-databind-2.11.3.jar;D:\JavaDevEnv\maven_repository\com\fasterxml\jackson\core\jackson-annotations\2.11.3\jackson-annotations-2.11.3.jar;D:\JavaDevEnv\maven_repository\com\fasterxml\jackson\core\jackson-core\2.11.3\jackson-core-2.11.3.jar;D:\JavaDevEnv\maven_repository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.11.3\jackson-datatype-jdk8-2.11.3.jar;D:\JavaDevEnv\maven_repository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.11.3\jackson-datatype-jsr310-2.11.3.jar;D:\JavaDevEnv\maven_repository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.11.3\jackson-module-parameter-names-2.11.3.jar;D:\JavaDevEnv\maven_repository\org\springframework\boot\spring-boot-starter-tomcat\2.3.7.RELEASE\spring-boot-starter-tomcat-2.3.7.RELEASE.jar;D:\JavaDevEnv\maven_repository\org\apache\tomcat\embed\tomcat-embed-core\9.0.41\tomcat-embed-core-9.0.41.jar;D:\JavaDevEnv\maven_repository\org\glassfish\jakarta.el\3.0.3\jakarta.el-3.0.3.jar;D:\JavaDevEnv\maven_repository\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.41\tomcat-embed-websocket-9.0.41.jar;D:\JavaDevEnv\maven_repository\org\springframework\spring-web\5.2.12.RELEASE\spring-web-5.2.12.RELEASE.jar;D:\JavaDevEnv\maven_repository\org\springframework\spring-beans\5.2.12.RELEASE\spring-beans-5.2.12.RELEASE.jar;D:\JavaDevEnv\maven_repository\org\springframework\spring-webmvc\5.2.12.RELEASE\spring-webmvc-5.2.12.RELEASE.jar;D:\JavaDevEnv\maven_repository\org\springframework\spring-aop\5.2.12.RELEASE\spring-aop-5.2.12.RELEASE.jar;D:\JavaDevEnv\maven_repository\org\springframework\spring-context\5.2.12.RELEASE\spring-context-5.2.12.RELEASE.jar;D:\JavaDevEnv\maven_repository\org\springframework\spring-expression\5.2.12.RELEASE\spring-expression-5.2.12.RELEASE.jar;D:\JavaDevEnv\maven_repository\org\springframework\boot\spring-boot-devtools\2.3.7.RELEASE\spring-boot-devtools-2.3.7.RELEASE.jar;D:\JavaDevEnv\maven_repository\org\springframework\boot\spring-boot\2.3.7.RELEASE\spring-boot-2.3.7.RELEASE.jar;D:\JavaDevEnv\maven_repository\org\springframework\boot\spring-boot-autoconfigure\2.3.7.RELEASE\spring-boot-autoconfigure-2.3.7.RELEASE.jar;D:\JavaDevEnv\maven_repository\org\springframework\spring-core\5.2.12.RELEASE\spring-core-5.2.12.RELEASE.jar;D:\JavaDevEnv\maven_repository\org\springframework\spring-jcl\5.2.12.RELEASE\spring-jcl-5.2.12.RELEASE.jar" com.wdh.springinaction.springbyinitializr2.Springbyinitializr2Application
      2. . ____ _ __ _ _
      3. /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
      4. ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
      5. \\/ ___)| |_)| | | | | || (_| | ) ) ) )
      6. ' |____| .__|_| |_|_| |_\__, | / / / /
      7. =========|_|==============|___/=/_/_/_/
      8. :: Spring Boot :: (v2.3.7.RELEASE)
      9. 2022-08-24 11:26:58.891 INFO 2544 --- [ restartedMain] c.w.s.s.Springbyinitializr2Application : Starting Springbyinitializr2Application on 14JPYI7CBESDNFK with PID 2544 (D:\JavaWorkspace\IdeaProjects\SpringInAction\springbyinitializr2\target\classes started by Administrator in D:\JavaWorkspace\IdeaProjects\SpringInAction\springbyinitializr2)
      10. 2022-08-24 11:26:58.897 INFO 2544 --- [ restartedMain] c.w.s.s.Springbyinitializr2Application : No active profile set, falling back to default profiles: default
      11. 2022-08-24 11:26:58.980 INFO 2544 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
      12. 2022-08-24 11:26:58.980 INFO 2544 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
      13. 2022-08-24 11:27:00.102 INFO 2544 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
      14. 2022-08-24 11:27:00.110 INFO 2544 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat]
      15. 2022-08-24 11:27:00.110 INFO 2544 --- [ restartedMain] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.41]
      16. 2022-08-24 11:27:00.167 INFO 2544 --- [ restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
      17. 2022-08-24 11:27:00.167 INFO 2544 --- [ restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1186 ms
      18. 2022-08-24 11:27:00.316 INFO 2544 --- [ restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
      19. 2022-08-24 11:27:00.414 WARN 2544 --- [ restartedMain] org.thymeleaf.templatemode.TemplateMode : [THYMELEAF][restartedMain] Template Mode 'HTML5' is deprecated. Using Template Mode 'HTML' instead.
      20. 2022-08-24 11:27:00.454 INFO 2544 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
      21. 2022-08-24 11:27:00.484 INFO 2544 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
      22. 2022-08-24 11:27:00.494 INFO 2544 --- [ restartedMain] c.w.s.s.Springbyinitializr2Application : Started Springbyinitializr2Application in 1.887 seconds (JVM running for 2.493)
      23. 2022-08-24 11:27:22.438 INFO 2544 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
      24. 2022-08-24 11:27:22.438 INFO 2544 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
      25. 2022-08-24 11:27:22.442 INFO 2544 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 4 ms
    4. 在本机浏览器中输入“http://localhost:8080/”即可打开HomeController返回的home.html页面。

    Controller代码示例

    注意其目录结构(新建包com.wdh.springinaction.springbyinitializr2.controller;将HomeController 放置于包内)

    1. package com.wdh.springinaction.springbyinitializr2.controller;
    2. import org.springframework.stereotype.Controller;
    3. import org.springframework.web.bind.annotation.GetMapping;
    4. /**
    5. * @author WangDH
    6. * @create 2022-08-24 10:10
    7. */
    8. @Controller
    9. public class HomeController {
    10. @GetMapping("/")
    11. public String home(){
    12. return "home";
    13. }
    14. }

    home.html代码示例(基于Thymeleaf模板)

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>This is my spring boot home pagetitle>
    6. head>
    7. <body>
    8. <h1>hello world! spring boot.Welcome to my site.h1>
    9. <img th:src="@{/images/debug.png}" />
    10. body>
    11. html>

    关于启动运行

    方式一:在Springbyinitializr2Application.java中点击右键菜单的“Run Springbyinitializr2Application”

    方式二:在IDEA顶部工具栏点击绿色三角形按钮Run按钮

    关于调试运行

    方式一:在Springbyinitializr2Application.java中点击右键菜单的“Debug Springbyinitializr2Application”;

    方式二:在IDEA顶部工具栏点击绿色虫子按钮Debug按钮;

    断点:在调试前和调试中可以在代码行头点击增加断点;

    单步调试:在底部小窗口上方有调试详细工具栏;

    继续到下一断点:在底部小窗口左侧有竖着的多个按钮,绿色带竖线的三角形按钮表示继续到下一个断点或直接运行到程序结束;

    双红色圆点:表示查看所有断点;

    红色带黑斜线圆点:表示暂时跳过所有断点;

    方式二:IDEA内选择Maven (下一篇文章再续写)

  • 相关阅读:
    01 Java语言简介
    【脑机接口】基于脑机接口和经皮脊髓电刺激的下肢康复新方法
    【模板】2-SAT
    挂耳式耳机哪种好、适合户外运动的耳机推荐
    【Audio】正弦波生成原理及C++代码
    数字孪生智慧建筑解决方案
    iNFTnews | 元宇宙的欢乐世界:别开生面的游戏、音乐会、主题公园和电影
    【绘图案例-绘图的步骤 Objective-C语言】
    ZCMU--5230: 排练方阵(C语言)
    百度秋招突击手册面试算法题:三数之和
  • 原文地址:https://blog.csdn.net/wangdonghao137/article/details/126497678