关于Spring Boot - 1的注意事项
这是一篇关于Spring Boot和JAR部署的笔记。
↵
背景
与使用 Spring MVC 构建的 Web 应用程序相比,Spring Boot 应用程序存在许多差异。但最明显的区别是,Spring Boot 允许一个包包含一个嵌入式容器,用于处理来自 JAR 文件的 Web 请求。本笔记旨在回答以下问题。
- 构建 Spring Boot 应用程序的 JAR 文件所需的最少 Maven 依赖是什么?
- 如何启动 Spring Boot 应用程序?
- 如何在 Spring Boot 应用程序中添加控制器?
- 如何在 Spring Boot 应用程序中添加配置?
- 如何提供静态内容并将静态内容打包到 JAR 文件中?
附带的是一个 Maven 项目。我在 Eclipse Java EE IDE for web developers 中测试过它,请随时自己尝试。
最少的 Maven 依赖
附带的 Maven 项目具有以下 POM.xml 文件,我认为它包含了构建和打包 Spring Boot 应用程序所需的最少 Maven 依赖。
<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>
<groupId>com.song.example</groupId>
<artifactId>spring-boot-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<spring-boot.version>1.5.7.RELEASE</spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring-boot.version}</version>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration><source>1.8</source><target>1.8</target></configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<executions>
<execution>
<goals><goal>repackage</goal></goals>
<configuration>
<finalName>${artifactId}-${version}</finalName>
<mainClass>${start-class}</mainClass>
<addResources>true</addResources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
- 编译依赖 spring-boot-starter-web 是编译最小 Spring Boot 应用程序所需的唯一 Maven 依赖;
- Maven 需要构建插件 spring-boot-maven-plugin 来打包 Spring Boot 应用程序。
一些教程建议将 Spring Boot 应用程序设置为父 POM spring-boot-starter-parent 的子应用程序。但是,Maven 项目只能支持一个父 POM,我可能希望为该应用程序提供我自己的父 POM 以便用于其他目的,因此我选择让示例应用程序成为一个独立的应用程序,而不是带有父 POM spring-boot-starter-parent。至少对于本笔记涉及的功能,我没有发现任何问题。
Spring Boot 应用程序中的静态主类
要启动 Spring Boot 应用程序,您可以创建一个扩展 SpringBootServletInitializer 并使用 @SpringBootApplication 注释的类。
package com.song.web.boot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan({"com.song.web.boot", "com.song.web.controller"})
public class ApplicationStart extends SpringBootServletInitializer {
public static void main (String[] args) {
SpringApplication.run(ApplicationStart.class, args);
}
}
@ComponentScan 注释告诉 Spring Boot 要扫描哪些包以查找 Spring 组件和控制器。
如何添加控制器
典型的 Spring MVC 控制器在 Spring Boot 中运行良好。控制器类需要位于 @ComponentScan 路径内。如果您查看 ApplicationStart
类,您会注意到我们将 com.song.web.controller
包添加到了 @ComponentScan 路径中。
package com.song.web.controller;
import java.util.HashMap;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class ExampleController {
@RequestMapping(value = "/getAJson", method=RequestMethod.GET)
@ResponseBody
public HashMap<String, String> getAJson() {
HashMap<String, String> map = new HashMap<String, String>();
map.put("Sprint", "boot");
return map;
}
}
在此示例控制器中,我们实现了一个简单的 REST 服务,它将响应 GET
请求中的 HashMap
。
如何添加配置
package com.song.web.boot;
import javax.servlet.Filter;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.song.web.filter.NocacheFilter;
@Configuration
public class ServletConfig {
@Bean
public EmbeddedServletContainerCustomizer portCustomizer() {
return (container -> { container.setPort(8090); });
}
@Bean
public FilterRegistrationBean noCacheFilter() {
Filter filter = new NocacheFilter();
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(filter);
registration.setOrder(1);
return registration;
}
}
要配置 Spring Boot 应用程序,我们可以创建一个用 @Configuration 注释的类,并且该类中的每个 @Bean 方法都提供一个配置条目。
portCustomizer()
告诉应用程序监听8090
端口;noCacheFilter()
告诉应用程序将过滤器应用于 Web 请求。
NocacheFilter
类实现如下,以禁用所有 Web 请求的缓存。
package com.song.web.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletResponse;
@WebFilter(urlPatterns = {"/*" })
public class NocacheFilter implements Filter {
public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletResponse httpResponse = (HttpServletResponse)response;
httpResponse.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
httpResponse.setHeader("Pragma", "no-cache");
httpResponse.setDateHeader("Expires", 0);
chain.doFilter(request, response);
}
public void destroy() {}
public void init(FilterConfig fConfig) throws ServletException {}
}
如何提供静态内容
要从 Spring Boot 应用程序提供静态内容,您可以将静态内容添加到 src/main/resources/static 文件夹。在此示例中,我从我早期的一篇文章中借用了所有 HTML/CSS/JAVASCRIPT 文件,以节省我的时间。
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<executions>
<execution>
<goals><goal>repackage</goal></goals>
<configuration>
<finalName>${artifactId}-${version}</finalName>
<mainClass>${start-class}</mainClass>
<addResources>true</addResources>
</configuration>
</execution>
</executions>
</plugin>
为了将静态内容打包到 JAR 文件中,我们需要确保在 POM.xml 的 spring-boot-maven-plugin
中添加了 <addResources>true</addResources>
。
构建并运行
要构建应用程序,您可以发出以下命令
mvn clean install
您实际上可以直接通过以下命令启动应用程序
mvn spring-boot:run
如果您将 Maven 项目导入 Eclipse,您可以在 Eclipse 中像 Java 应用程序一样运行或调试它。
您也可以通过以下命令运行 JAR 文件
java -jar target/spring-boot-example-0.0.1-SNAPSHOT.jar
无论您如何启动应用程序,如果它成功启动,您都可以通过浏览器进行测试。
关注点
- 这是关于 Spring Boot 和 JAR 部署的笔记;
- 希望您喜欢我的帖子,并希望这篇说明能对您有所帮助。
历史
- 2017 年 11 月 25 日:第一次修订