上篇 Spring Boot 快速入门(上)HelloWorld示例 进行了 Sprint Boot
的快速入门,以实际的示例代码来练手,总比光看书要强很多嘛,最好的就是边看、边写、边记、边展示。就是把看到的使用IDE工具进行实际的代码演练,在把演练过程写出来,最后发表在 blog
或其他自媒体平台。
本篇主要以上篇的代码为准,说明 HelloWorld
示例代码中的注意事项。相对于微软的 Net Core
,Spring Boot
功能点更加清晰、易于记忆与应用。本篇主要涉及到
注意,本示例系统环境为 mac
,windows
中所有本示例的 Preferences
菜单 改为 Setting
其他没有差别。
除了全宇宙无敌的 Visual Studio
外,推荐 Intellij IDEA
,之前写过一篇 Spring Boot 2.x 入门前的准备-IntelliJ IDEA 开发工具的安装与使用 ,Intellij IDEA可能是Java中最好的开发者工具。可选Intellij IDEA是收费的,而且还小贵。大家学习用,可以用试用版本。也可以选择Eclipse和Visual Studio Code来替代。本示例HelloWorld使用的IDEA来开发。
最新更新:当然你也可以通过github 的开源项目来更新,但对于初学者来说,这恐怕比较勉强。
IDEA如何运行Java项目,如何创建和编译运行Spring Boot项目在上一篇( Spring Boot 2.x 快速入门(上)HelloWorld示例)已经演示过。
开源,但是要自己设置的比较多,我之前是编写NET代码,有的时候也用,当初Android Studio也是选择的Eclipse,但是后来转战Intellij IDEA了,说明啥,对用户友好的还是Intellij IDEA
近年来,vs code逐渐流行,并且有迅速扩大的态势。高效、简单、使用,是他的基本特点。我比较看好VS Code,简单精致,有无限可能。
在 https://start.spring.io/
的网站上可以看到,除了使用 Maven
外,我们还可以使用 Gradle Project
来管理 SpringBoot
项目。我目前使用Maven来管理 Spring Boot
,Android Studio
则默认使用 Gradle Project
来管理项目。
如果你想详细了解 Maven,我之前也写过一篇文章介绍 Spring Boot 学习前你应该知道的 Maven 知识
第一次使用Maven,感觉跟Net的Nuget包管理器是一样的。是对第三方组件(类似Dll)Java中是Jar包的管理,可以自动管理他们的加载与卸载。
在没有 Maven
的时候,我们可以通过 添加第三方包来向项目增加Jar包,这种方式jar很少的时候,非常方便,但多了之后非常的麻烦,特别是团队作业的时候。
所以我的理解是,Maven
感觉就是一个智能机器人,我们把对Jar包的管理托管给他,由他来管理包的加载、匹配、卸载等工作。
Pom.xml
是 Maven
的配置文件,这个类似 NodeJs
使用 json 文件来管理、ASP.NET
来使用 web.config
来管理一样,Pom.xml
是 Maven
管理第三方Jar包的可描述的文件。
实际操作过程中,我们只要对 Pom.xml
进行变更,系统就会自动去管理jar包。
上一张的 Pom.xml
如下描述:
<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.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.1.4.RELEASEversion>
<relativePath/>
parent>
<groupId>com.fishprogroupId>
<artifactId>springstudyartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>springstudyname>
<description>Demo project for Spring Bootdescription>
<properties>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
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
操作。
在Spring Boot中正是靠注解来大大简化编程代码,在 Spring Boot 代码中随处可见注解,包括 @SpringBootApplication
、@Autowired
、@Bean
、@Component
等等,下面列出 HelloWorld 中的常见注解
申明让 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 {};
}
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 "";
}
提供路由信息,负责URL到Controller中的具体函数的映射。类似C#中的[Route(“hello/[controller]/[action]”)]
@RequestMapping是@Mapping的扩展
有了这些注解,HelloWorld示例程序就可以运行了。不仅仅在简单的RestApi中可以使用注解,在SpringBoot所有的特征功能中,都使用了注解。
总的来说,注解是一种AOP形态,类似于C#中的特性。类似一种标记,大大简化了代码的编写量。在后面会有单独一章讲解注解。
在HelloWorld示例中,我们使用了内置的Tomcat来打包Spring Boot程序。这也是官方默认的打包方式,用起来也是非常方便。
无须其他的配置,直接编译,项目中的mvnw, mvnw.cmd文件是对mvn命令的封装。通常使用以下命令来编译
mvn clean
mvn install
或者写成一个命令
mvn clean install
war可以在独立的Tomcat服务器中部署。
修改Pom.xml
<groupId>com.fishprogroupId>
<artifactId>springstudyartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>springstudyname>
<packaging>warpackaging>
使用命令
mvn clean
mvn install
打包成war
把 war
放到 tomcat
服务器的 webapps
文件夹下,在 tomcat
运行时会自动将 war
解压的 webapps
下
热部署,就是当我们修改源代码或资源文件的时候,委托系统自动重启tomcat应用,这样,我们开发人员就不需要频发的手动操作。
下面以IDEA环境为例子说明
点击 Preferences>Build,Execution,Deployment>Compiler ,勾选 Build project automatically
并按住 ctrl+shift+alt+/ 组合键打开面板 Maintenance
,点击Registry…
勾选 Compiler autoMake allow when app running
引入插件
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<optional>trueoptional>
<scope>truescope>
dependency>
修改build>plugins节点
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<configuration>
<fork>truefork>
configuration>
plugin>
plugins>
build>
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下面的窗口)系统自动重启了项目。效果如下图:
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 (热部署)