Spring Boot 快速入门 HelloWorld示例详细说明
Spring Boot 快速入门 再来一个 HelloWorld
上篇 Spring Boot 快速入门(上)HelloWorld示例 进行了
Sprint Boot的快速入门,以实际的示例代码来练手,总比光看书要强很多嘛,最好的就是边看、边写、边记、边展示。就是把看到的使用IDE工具进行实际的代码演练,在把演练过程写出来,最后发表在
blog或其他自媒体平台。
本篇主要以上篇的代码为准,说明
HelloWorld示例代码中的注意事项。相对于微软的
Net Core,
Spring Boot功能点更加清晰、易于记忆与应用。本篇主要涉及到
- 什么什么样的开发工具
- pom.xml 配置说明
- 示例中的代码注解含义
- 如何打包和其他的打包方式
- 热部署
注意,本示例系统环境为
mac,
windows中所有本示例的
Preferences菜单 改为
Setting其他没有差别。
1.开发工具的选择
除了全宇宙无敌的
Visual Studio外,推荐
Intellij IDEA,之前写过一篇 Spring Boot 2.x 入门前的准备-IntelliJ IDEA 开发工具的安装与使用 ,Intellij IDEA可能是Java中最好的开发者工具。可选Intellij IDEA是收费的,而且还小贵。大家学习用,可以用试用版本。也可以选择Eclipse和Visual Studio Code来替代。本示例HelloWorld使用的IDEA来开发。
最新更新:当然你也可以通过github 的开源项目来更新,但对于初学者来说,这恐怕比较勉强。
1.1 IDEA
IDEA如何运行Java项目,如何创建和编译运行Spring Boot项目在上一篇( Spring Boot 2.x 快速入门(上)HelloWorld示例)已经演示过。
1.2 Eclipse
开源,但是要自己设置的比较多,我之前是编写NET代码,有的时候也用,当初Android Studio也是选择的Eclipse,但是后来转战Intellij IDEA了,说明啥,对用户友好的还是Intellij IDEA
1.3 Visual Studio Code
近年来,vs code逐渐流行,并且有迅速扩大的态势。高效、简单、使用,是他的基本特点。我比较看好VS Code,简单精致,有无限可能。
2.Maven以及他的Pom.xml配置
在
https://start.spring.io/的网站上可以看到,除了使用
Maven外,我们还可以使用
Gradle Project来管理
SpringBoot项目。我目前使用Maven来管理
Spring Boot,
Android Studio则默认使用
Gradle Project来管理项目。
2.1 Maven 简介
如果你想详细了解 Maven,我之前也写过一篇文章介绍 Spring Boot 学习前你应该知道的 Maven 知识
第一次使用Maven,感觉跟Net的Nuget包管理器是一样的。是对第三方组件(类似Dll)Java中是Jar包的管理,可以自动管理他们的加载与卸载。
在没有
Maven的时候,我们可以通过 添加第三方包来向项目增加Jar包,这种方式jar很少的时候,非常方便,但多了之后非常的麻烦,特别是团队作业的时候。
所以我的理解是,
Maven感觉就是一个智能机器人,我们把对Jar包的管理托管给他,由他来管理包的加载、匹配、卸载等工作。
2.2 Pom.xml配置文件
Pom.xml是
Maven的配置文件,这个类似
NodeJs使用 json 文件来管理、
ASP.NET来使用
web.config来管理一样,
Pom.xml是
Maven管理第三方Jar包的可描述的文件。
实际操作过程中,我们只要对
Pom.xml进行变更,系统就会自动去管理jar包。
上一张的
Pom.xml如下描述:
<?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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.fishpro</groupId> <artifactId>springstudy</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springstudy</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
上面几个核心的节点说明
-
modelVersion
指定了当前Maven
模型的版本号,对于Maven2
和Maven3
来说,它只能是 4.0.0 -
groupId
组织名称,通常使用com.companyname.xx.xx
(或使用net 、org开头)中间使用.(点)号隔开。 -
artifactId
项目名称项目唯一的ID,一个 groupId 下可能有多个项目,靠 artifactId 区分,比如 springstudy 项目 -
version
版本号,SNAPSHOT意为快照,说明该项目还在开发中,是不稳定的版本。在Maven中很重要的一点是,groupId
、artifactId、version三个元素生成了一个Maven项目的基本坐标。 -
dependencies
依赖项管理节点,所有的依赖都是使用这个节点管理的, 每个依赖节点由包括了groupId
(必选)、artifactId
(必选)、version
(可选),注意这里的groupId-artifactiD-version
组成了唯一的识别号,也就是前面的节点的意义所在。 -
build
表示与构建相关,如在本章的 5热部署中需要配置build
下的plugins
配置,以达到支持热部署的效果。
Maven
的Pom.xml
配置项目很多,但实际上操作过程用的最多的就是依赖 的增加减少dependencies
操作。
3.注解-编码的核心
在Spring Boot中正是靠注解来大大简化编程代码,在 Spring Boot 代码中随处可见注解,包括
@SpringBootApplication、
@Autowired、
@Bean、
@Component等等,下面列出 HelloWorld 中的常见注解
3.1 @SpringBootApplication
申明让 spring boot 自动给程序进行必要的配置,这个配置等同于:
@Configuration,
@EnableAutoConfiguration和
@ComponentScan三个配置。
在启动类文件 com.fishpro.springstudy.SpringstudyApplication 中,
@SpringBootApplication在文件开头上。
@SpringBootApplication public class SpringstudyApplication { public static void main(String[] args) { SpringApplication.run(SpringstudyApplication.class, args); } }
按住ctrl 点击
@SpringBootApplication可以放心它是有三个注解组成
@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @SpringBootConfiguration @EnableAutoConfiguration @ComponentScan( excludeFilters = {@Filter( type = FilterType.CUSTOM, classes = {TypeExcludeFilter.class} ), @Filter( type = FilterType.CUSTOM, classes = {AutoConfigurationExcludeFilter.class} )} ) public @interface SpringBootApplication { @AliasFor( annotation = EnableAutoConfiguration.class ) Class<?>[] exclude() default {}; @AliasFor( annotation = EnableAutoConfiguration.class ) String[] excludeName() default {}; @AliasFor( annotation = ComponentScan.class, attribute = "basePackages" ) String[] scanBasePackages() default {}; @AliasFor( annotation = ComponentScan.class, attribute = "basePackageClasses" ) Class<?>[] scanBasePackageClasses() default {}; }
3.2 @RestController
com.fishpro.springstudy.controller.HelloWorldController.java 中用于标注控制层组件(在MVC中的Controller层),@ResponseBody和@Controller的合集。
按住Ctrl键 点击
@RestController可以看到他是
@Controller和
@ResponseBody集合
@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Controller @ResponseBody public @interface RestController { @AliasFor( annotation = Controller.class ) String value() default ""; }
3.3 @RequestMapping
提供路由信息,负责URL到Controller中的具体函数的映射。类似C#中的[Route(“hello/[controller]/[action]”)]
@RequestMapping是@Mapping的扩展
有了这些注解,HelloWorld示例程序就可以运行了。不仅仅在简单的RestApi中可以使用注解,在SpringBoot所有的特征功能中,都使用了注解。
总的来说,注解是一种AOP形态,类似于C#中的特性。类似一种标记,大大简化了代码的编写量。在后面会有单独一章讲解注解。
4.打包发布
4.1 使用内置的Tomcat来打包
在HelloWorld示例中,我们使用了内置的Tomcat来打包Spring Boot程序。这也是官方默认的打包方式,用起来也是非常方便。
无须其他的配置,直接编译,项目中的mvnw, mvnw.cmd文件是对mvn命令的封装。通常使用以下命令来编译
mvn clean mvn install
或者写成一个命令
mvn clean install
4.2 打包成War
war可以在独立的Tomcat服务器中部署。
修改Pom.xml
<groupId>com.fishpro</groupId> <artifactId>springstudy</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springstudy</name> <packaging>war</packaging>
使用命令
mvn clean mvn install
打包成war
把
war放到
tomcat服务器的
webapps文件夹下,在
tomcat运行时会自动将
war解压的
webapps下
5.热部署
热部署,就是当我们修改源代码或资源文件的时候,委托系统自动重启tomcat应用,这样,我们开发人员就不需要频发的手动操作。
下面以IDEA环境为例子说明
5.1 在IDEA设置中配置系统支持热部署
点击 Preferences>Build,Execution,Deployment>Compiler ,勾选 Build project automatically
并按住 ctrl+shift+alt+/ 组合键打开面板
Maintenance,点击Registry…
勾选 Compiler autoMake allow when app running
5.2 配置Pom.xml,引入热部署插件
引入插件
<!--Spring Boot 2.x 快速入门(下)HelloWorld示例详解中的热部署插件--> <!--添加热部署--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> <scope>true</scope> </dependency>
修改build>plugins节点
<build> <plugins> <plugin> <!--热部署配置--> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <!--fork:如果没有该项配置,整个devtools不会起作用--> <fork>true</fork> </configuration> </plugin> </plugins> </build>
5.3 运行测试热部署效果
1)运行HelloWorld项目,浏览器输入 http://localhost:8999/hello/say 可以看到输出 HelloWorld
2)修改com.fishpro.springstudy.controller.HelloWorldController.java 如下,增加输出字符串
public class HelloWorldController { @RequestMapping("/say") public String say(){ return "HelloWorld"+" 热部署测试"; } }
3)浏览器输入 http://localhost:8999/hello/say 可以看到输出 HelloWorld 热部署测试。同时我们可以看到IDEA的Console(IDEA下面的窗口)系统自动重启了项目。效果如下图:
6 问题
1.经常我们部署了热部署,在Pom中加入了依赖xml描述,但是没有什么效果,问题在于,我们未能配置IDEA的环境,注意是两个环境设置(见5.1)。
参考
-
http://www.runoob.com/maven/maven-pom.html (maven)
-
https://blog.csdn.net/weixin_40753536/article/details/81285046 (注解)
-
https://blog.csdn.net/qq_42685050/article/details/81588584 (热部署)
- 点赞
- 收藏
- 分享
- 文章举报
- springboot快速入门(一)——HelloWorld搭建
- JAVAEE——SpringBoot入门:简介、微服务、环境准备、helloworld与探究、快速构建项目
- SPRING.NET 1.3.2 学习2--快速入门应用示例说明
- Spring基础:快速入门spring boot(1):CLI方式的Helloworld
- Spring boot教程之Spring boot简单快速入门从HelloWorld开始
- SPRING.NET 1.3.2 学习2--快速入门应用示例说明
- springboot项目中websocket入门示例
- springboot+hibernate+Mysql快速入门
- Spring Boot 2.x基础教程:快速入门
- Spring Boot 快速入门必看手册
- Spring Boot快速入门(一): Hello Spring Boot
- gradle+springBoot入门示例
- Spring Boot 2 快速教程:WebFlux 快速入门(二)
- springBoot快速入门
- Spring Boot 快速入门
- SpringBoot快速入门
- Spring Boot:(一)快速入门
- spring boot框架学习1-认识spring boot和快速入门
- spring boot快速入门
- SpringBoot快速入门