详解Springboot工程中如何快速判断web应用服务器类型
作者:爱码少年 00fly.online
在 Spring Boot 工程中快速判断 Web 应用服务器类型,可以从运行时环境、Spring Boot 特有 API、底层类加载器、HTTP 响应和外部命令等多个层面入手。下面是几种常见方法的速览表:
| 方法 | 原理简述 | 代码复杂度 | 通用性 | 推荐度 |
|---|---|---|---|---|
| 注入 ServletContext | 直接调用 getServerInfo() 获取服务器信息 | ★☆☆☆☆ | 极高 (适用于所有 Servlet 容器) | ⭐⭐⭐⭐⭐ |
| 检查特定类是否存在 | 通过 Class.forName() 加载特定服务器的关键类 | ★★☆☆☆ | 高 | ⭐⭐⭐⭐ |
| 利用 Spring Boot 条件注解 | 使用 @ConditionalOnClass 注解声明 Bean | ★★☆☆☆ | 一般 (适用于 Spring 环境) | ⭐⭐⭐ |
| 监听容器初始化事件 | 在应用启动时监听 ServletWebServerInitializedEvent 事件 | ★★☆☆☆ | 一般 (适用于 Spring Boot) | ⭐⭐⭐ |
| 查看启动日志 | 直接观察应用启动时的控制台日志输出 | ☆☆☆☆☆ | 极高 | ⭐⭐⭐⭐⭐ |
| 使用 Actuator 端点 | 访问 /actuator/env 端点查看服务器相关配置 | ★★☆☆☆ | 一般 | ⭐⭐⭐ |
| 检查特定系统属性或 JMX | 读取 java.vm.vendor 或查询 JMX MBean | ★★★☆☆ | 中 | ⭐⭐ |
| 通过外部命令或端口号推断 | 使用 netstat 或 ps 命令在操作系统层面查询 | ★★★☆☆ | 低 (仅限手动排查) | ⭐ |
方法一:通过 ServletContext 获取服务器信息(推荐)
这是最直接、最可靠的方式。你可以在任何 Spring Bean 中注入 ServletContext 对象,并调用 getServerInfo() 方法。该方法会返回一个字符串,其中包含了当前正在运行的 Servlet 容器的名称和版本信息。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.servlet.ServletContext;
@Service
public class ServerInfoService {
@Autowired
private ServletContext servletContext;
public void printServerInfo() {
String serverInfo = servletContext.getServerInfo();
System.out.println("服务器信息: " + serverInfo);
// 对于 Tomcat,输出可能是 "Apache Tomcat/9.0.83"
// 对于 Jetty,输出可能是 "Jetty/9.4.53.v20231009"
// 对于 Undertow,输出可能是 "Undertow - 2.2.24.Final"
}
}根据 getServerInfo() 返回的内容进行判断时,注意对版本号部分进行清理,避免因版本差异导致匹配失败。
方法二:检查特定类的存在性
通过 Class.forName() 方法检测特定服务器的关键类是否存在于当前应用的 Classpath 中,从而判断其类型。
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
@Component
public class ServerDetector {
public enum ServerType {
TOMCAT, JETTY, UNDERTOW, JBOSS, WEBLOGIC, WEBSPHERE, UNKNOWN
}
private static ServerType detectedServer;
@PostConstruct
public void detect() {
detectedServer = determineServerType();
System.out.println("检测到服务器类型: " + detectedServer);
}
public static ServerType determineServerType() {
if (isClassPresent("org.apache.catalina.startup.Tomcat")) return ServerType.TOMCAT;
if (isClassPresent("org.eclipse.jetty.server.Server")) return ServerType.JETTY;
if (isClassPresent("io.undertow.Undertow")) return ServerType.UNDERTOW;
if (isClassPresent("org.jboss.Main")) return ServerType.JBOSS;
if (isClassPresent("weblogic.Server")) return ServerType.WEBLOGIC;
if (isClassPresent("com.ibm.websphere.product.VersionInfo")) return ServerType.WEBSPHERE;
return ServerType.UNKNOWN;
}
private static boolean isClassPresent(String className) {
try {
Class.forName(className);
return true;
} catch (ClassNotFoundException e) {
return false;
}
}
}方法三:利用 Spring Boot 的条件注解
这种方式适合用于控制某些特定服务器下 Bean 的加载,是一种声明式的判断方法。
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ServerSpecificConfig {
@Bean
@ConditionalOnClass(name = "org.apache.catalina.startup.Tomcat")
public String tomcatOnlyBean() {
return "仅当应用运行在 Tomcat 服务器上时,此 Bean 才会被创建";
}
}这个例子也可以作为快速判断的依据,当你看到 tomcatOnlyBean 在应用上下文中被创建,就可以认为当前运行的是 Tomcat 容器。
方法四:监听容器初始化事件
通过实现 ApplicationListener<ServletWebServerInitializedEvent> 接口,可以在 Servlet 容器完成初始化后获取 WebServer 实例,从而获取服务器的具体实现类,反推出其类型。
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
@Component
public class WebServerListener implements ApplicationListener<WebServerInitializedEvent> {
@Override
public void onApplicationEvent(WebServerInitializedEvent event) {
// 获取 WebServer 实例,其实现类通常是 TomcatWebServer、JettyWebServer、UndertowWebServer 等
Class<?> webServerClass = event.getWebServer().getClass();
System.out.println("WebServer 实现类: " + webServerClass.getName());
// 可根据实现类名判断服务器类型
if (webServerClass.getName().contains("Tomcat")) {
System.out.println("当前运行在 Tomcat 服务器上");
}
}
}补充说明
通用性与准确性:ServletContext.getServerInfo() 是标准 Java Servlet 规范的一部分,在所有支持 Servlet 规范的容器中均可使用,且直接提供官方定义的服务器名称,是最可靠的方法。
为什么可能有“多种判断”:在某些特殊场景下(例如需要与不支持 Servlet API 的老旧代码集成),可能需要采用其他方式。另外,getServerInfo() 返回的字符串版本号格式因服务器而异,有时需要自行解析。
方法选择建议:对绝大多数 Spring Boot 应用,最推荐 ServletContext.getServerInfo()。若无法获取 ServletContext,可使用“检查类是否存在”的方法作为替代。若要根据服务器类型选择性加载 Bean,应优先使用 Spring 的条件注解。
方法补充
该代码片段通过检查Spring应用上下文中的Bean定义来判断当前使用的是Tomcat还是Jetty容器。使用@PostConstruct注解在初始化时执行检测逻辑,通过扫描Bean名称是否包含EmbeddedTomcat或EmbeddedJetty来设置相应的布尔标志,并输出日志记录检测结果。这种动态识别方式适用于需要针对不同嵌入式容器做差异化处理的场景。
核心源码
boolean isTomcat;
boolean isJetty;
@Autowired
ApplicationContext applicationContext;
@PostConstruct
private void init()
{
isTomcat = Stream.of(applicationContext.getBeanDefinitionNames()).anyMatch(name -> StringUtils.containsIgnoreCase(name, "EmbeddedTomcat"));
isJetty = Stream.of(applicationContext.getBeanDefinitionNames()).anyMatch(name -> StringUtils.containsIgnoreCase(name, "EmbeddedJetty"));
log.info("#### isTomcat: {}", isTomcat);
log.info("#### isJetty: {}", isJetty);
}
典型应用
import java.io.File;
import java.io.IOException;
import java.util.stream.Stream;
import javax.annotation.PostConstruct;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.fly.demo.common.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Api(tags = "文件上传(simple)")
@RestController
@RequestMapping("/simple/file")
public class SimpleFileController
{
boolean isTomcat;
boolean isJetty;
@Autowired
ApplicationContext applicationContext;
@PostConstruct
private void init()
{
isTomcat = Stream.of(applicationContext.getBeanDefinitionNames()).anyMatch(name -> StringUtils.containsIgnoreCase(name, "EmbeddedTomcat"));
isJetty = Stream.of(applicationContext.getBeanDefinitionNames()).anyMatch(name -> StringUtils.containsIgnoreCase(name, "EmbeddedJetty"));
log.info("#### isTomcat: {}", isTomcat);
log.info("#### isJetty: {}", isJetty);
}
@ApiOperation("文件上传")
@PostMapping("/upload")
public JsonResult<?> upload(@RequestParam MultipartFile file)
throws IOException
{
File rootDir = new File("upload");
File dest;
if (isTomcat)
{
if (RandomUtils.nextBoolean())
{
log.info("### transferTo");
dest = new File(rootDir.getCanonicalPath() + File.separator + file.getOriginalFilename());
file.transferTo(dest);
}
else
{
log.info("### copyInputStreamToFile");
dest = new File(rootDir, file.getOriginalFilename());
FileUtils.copyInputStreamToFile(file.getInputStream(), dest);
}
}
else
{
log.info("### copyInputStreamToFile");
dest = new File(rootDir, file.getOriginalFilename());
FileUtils.copyInputStreamToFile(file.getInputStream(), dest);
}
return JsonResult.success(dest.getName());
}
}
到此这篇关于详解Springboot工程中如何快速判断web应用服务器类型的文章就介绍到这了,更多相关Springboot判断web应用服务器类型内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
