美文网首页
spring boot

spring boot

作者: 懦弱的me | 来源:发表于2018-03-16 15:31 被阅读0次

一、前言

Spring Boot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化新 Spring 应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。

本系列以快速入门为主,可当作工具小手册阅读

二、环境搭建

创建一个 maven 工程,目录结构如下图:

2.1 添加依赖

创建 maven 工程,在 pom.xml 文件中添加如下依赖:

org.springframework.bootspring-boot-starter-parent1.5.6.RELEASEUTF-8UTF-81.8org.springframework.bootspring-boot-starter-weborg.springframework.bootspring-boot-maven-plugin

2.2 创建目录和配置文件

创建 src/main/resources 源文件目录,并在该目录下创建 application.properties 文件、static 和 templates 的文件夹。

application.properties:用于配置项目运行所需的配置数据。

static:用于存放静态资源,如:css、js、图片等。

templates:用于存放模板文件。

目录结构如下:

2.3 创建启动类

在 com.light.springboot 包下创建启动类,如下:

/** 该注解指定项目为springboot,由此类当作程序入口 自动装配 web 依赖的环境 **/@SpringBootApplicationpublicclassSpringbootApplication{public static void main(String[] args){        SpringApplication.run(SpringbootApplication.class, args);    }}

2.4 案例演示

创建 com.light.springboot.controller 包,在该包下创建一个 Controller 类,如下:

@RestControllerpublicclassTestController{@GetMapping("/helloworld")public String helloworld(){return"helloworld";    }}

在 SpringbootApplication 文件中右键 Run as -> Java Application。当看到 “Tomcat started on port(s): 8080 (http)” 字样说明启动成功。

打开浏览器访问 http://localhost:8080/helloworld,结果如下:

读者可以使用 STS 开发工具,里边集成了插件,可以直接创建 Spingboot 项目,它会自动生成必要的目录结构。

三、热部署

当我们修改文件和创建文件时,都需要重新启动项目。这样频繁的操作很浪费时间,配置热部署可以让项目自动加载变化的文件,省去的手动操作。

在 pom.xml 文件中添加如下配置:

org.springframework.bootspring-boot-devtoolstruetrue

org.springframework.bootspring-boot-maven-plugintrue

配置好 pom.xml 文件后,我们启动项目,随便创建/修改一个文件并保存,会发现控制台打印 springboot 重新加载文件的信息。

演示图如下:

四、多环境切换

application.properties 是 springboot 在运行中所需要的配置信息。

当我们在开发阶段,使用自己的机器开发,测试的时候需要用的测试服务器测试,上线时使用正式环境的服务器。

这三种环境需要的配置信息都不一样,当我们切换环境运行项目时,需要手动的修改多出配置信息,非常容易出错。

为了解决上述问题,springboot 提供多环境配置的机制,让开发者非常容易的根据需求而切换不同的配置环境。

在 src/main/resources 目录下创建三个配置文件:

application-dev.properties:用于开发环境application-test.properties:用于测试环境application-prod.properties:用于生产环境

我们可以在这个三个配置文件中设置不同的信息,application.properties 配置公共的信息。

在 application.properties 中配置:

spring.profiles.active=dev

表示激活 application-dev.properties 文件配置, springboot 会加载使用 application.properties 和 application-dev.properties 配置文件的信息。

同理,可将 spring.profiles.active 的值修改成 test 或 prod 达到切换环境的目的。

演示图如下:

五、配置日志

5.1 配置 logback(官方推荐使用)

5.1.1 配置日志文件

spring boot 默认会加载 classpath:logback-spring.xml 或者 classpath:logback-spring.groovy。

如需要自定义文件名称,在 application.properties 中配置 logging.config 选项即可。

在 src/main/resources 下创建 logback-spring.xml 文件,内容如下:

${PATTERN}${TEST_FILE_PATH}${TEST_FILE_PATH}/info.%d{yyyy-MM-dd}.log100${PATTERN}${PRO_FILE_PATH}${PRO_FILE_PATH}/warn.%d{yyyy-MM-dd}.log100${PATTERN}

其中,springProfile 标签的 name 属性对应 application.properties 中的 spring.profiles.active 的配置。

即 spring.profiles.active 的值可以看作是日志配置文件中对应的 springProfile 是否生效的开关。

5.2 配置 log4j2

5.2.1 添加依赖

org.springframework.bootspring-boot-starter-log4j2

5.2.2 配置日志文件

spring boot 默认会加载 classpath:log4j2.xml 或者 classpath:log4j2-spring.xml。

如需要自定义文件名称,在 application.properties 中配置 logging.config 选项即可。

log4j2.xml 文件内容如下:

%d{yyyy-MM-dd HH:mm:ss.SSS} |-%-5level [%thread] %c [%L] -| %msg%n

log4j2 不能像 logback 那样在一个文件中设置多个环境的配置数据,只能命名 3 个不同名的日志文件,分别在 application-dev,application-test 和 application-prod 中配置 logging.config 选项。

除了在日志配置文件中设置参数之外,还可以在 application-*.properties 中设置,日志相关的配置:

logging.config# 日志配置文件路径,如 classpath:logback-spring.xmllogging.exception-conversion-word# 记录异常时使用的转换词logging.file# 记录日志的文件名称,如:test.loglogging.level.*# 日志映射,如:logging.level.root=WARN,logging.level.org.springframework.web=DEBUGlogging.path# 记录日志的文件路径,如:d:/logging.pattern.console# 向控制台输出的日志格式,只支持默认的 logback 设置。logging.pattern.file# 向记录日志文件输出的日志格式,只支持默认的 logback 设置。logging.pattern.level# 用于呈现日志级别的格式,只支持默认的 logback 设置。logging.register-shutdown-hook# 初始化时为日志系统注册一个关闭钩子

六、打包运行

打包的形式有两种:jar 和 war。

6.1 打包成可执行的 jar 包

默认情况下,通过 maven 执行 package 命令后,会生成 jar 包,且该 jar 包会内置了 tomcat 容器,因此我们可以通过 java -jar 就可以运行项目,如下图:

6.2 打包成部署的 war 包

让 SpringbootApplication 类继承 SpringBootServletInitializer 并重写 configure 方法,如下:

@SpringBootApplicationpublicclassSpringbootApplicationextendsSpringBootServletInitializer{@Overrideprotected SpringApplicationBuilder configure(SpringApplicationBuilder application){returnapplication.sources(SpringbootApplication.class);    }public static void main(String[] args){        SpringApplication.run(SpringbootApplication.class, args);    }}

修改 pom.xml 文件,将 jar 改成 war,如下:

war

打包成功后,将 war 包部署到 tomcat 容器中运行即可。

七、参考资料

https://docs.spring.io/spring-boot/docs/1.5.8.RELEASE/reference/html/ 官方文档

相关文章

网友评论

      本文标题:spring boot

      本文链接:https://www.haomeiwen.com/subject/cqqrqftx.html