一、该问题的重现步骤是什么?
maven 依赖
org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-undertow org.springframework.boot spring-boot-starter-tomcat provided
2. 打包类型
war
3. 入口类修改
Application SpringBootServletInitializer{ SpringApplicationBuilder (SpringApplicationBuilder builder) { BladeApplication.(CommonConstant.Application.).sources(Application.)} (String[] args) { BladeApplication.(CommonConstant.Application.args)} }
二、你期待的结果是什么?实际看到的又是什么?
打包成功后的war包,放到tomcat中可以运行。
实际看到
IDEA里可以正常运行,也可以打war包,就是放入tomcat时无法使用
三、你正在使用的是什么产品,什么版本?在什么操作系统上?
spring-boot版本 win10系统 idea jdk8 tomcat8.5
四、请提供详细的错误堆栈信息,这很重要。
021-11-17 17:49:59.927 WARN 8652 --- [ost-startStop-1] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is java.lang.NoSuchMethodError: org.apache.tomcat.util.modeler.Registry.disableRegistry()V
2021-11-17 17:49:59.936 INFO 8652 --- [ost-startStop-1] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-11-17 17:49:59.977 ERROR 8652 --- [ost-startStop-1] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
An attempt was made to call a method that does not exist. The attempt was made from the following location:
org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.getWebServer(TomcatServletWebServerFactory.java:177)
The following method did not exist:
org.apache.tomcat.util.modeler.Registry.disableRegistry()V
The method's class, org.apache.tomcat.util.modeler.Registry, is available from the following locations:
jar:file:/D:/devsoft/java/tomcat/apache-tomcat-8.5.72/lib/tomcat-coyote.jar!/org/apache/tomcat/util/modeler/Registry.class
The class hierarchy was loaded from the following locations:
org.apache.tomcat.util.modeler.Registry: file:/D:/devsoft/java/tomcat/apache-tomcat-8.5.72/lib/tomcat-coyote.jar
Action:
Correct the classpath of your application so that it contains a single, compatible version of org.apache.tomcat.util.modeler.Registry
17-Nov-2021 17:49:59.978 涓ラ噸 [localhost-startStop-1] org.apache.catalina.core.ContainerBase.addChildInternal ContainerBase.addChild: start:
org.apache.catalina.LifecycleException: 鏃犳硶鍚姩缁勪欢[StandardEngine[Catalina].StandardHost[localhost].StandardContext[/blade-api]]
at org.apache.catalina.util.LifecycleBase.handleSubClassException(LifecycleBase.java:440)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:198)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:753)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:727)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:695)
at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:1016)
at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1903)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is java.lang.NoSuchMethodError: org.apache.tomcat.util.modeler.Registry.disableRegistry()V
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:161)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:545)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:755)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:402)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:312)
at org.springframework.boot.web.servlet.support.SpringBootServletInitializer.run(SpringBootServletInitializer.java:173)
at org.springframework.boot.web.servlet.support.SpringBootServletInitializer.createRootApplicationContext(SpringBootServletInitializer.java:153)
at org.springframework.boot.web.servlet.support.SpringBootServletInitializer.onStartup(SpringBootServletInitializer.java:95)
at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:172)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5221)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
... 10 more
Caused by: java.lang.NoSuchMethodError: org.apache.tomcat.util.modeler.Registry.disableRegistry()V
at org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.getWebServer(TomcatServletWebServerFactory.java:177)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:178)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:158)
... 22 more
17-Nov-2021 17:49:59.980 涓ラ噸 [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployWAR 閮ㄧ讲 Web 搴旂敤绋嬪簭 archive [D:\devsoft\java\tomcat\apache-tomcat-8.5.72\webapps\blade-api.war] 鏃跺嚭閿?
java.lang.IllegalStateException: 鍚姩瀛愮骇鏃跺嚭閿?
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:757)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:727)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:695)
at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:1016)
at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1903)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: org.apache.catalina.LifecycleException: 鏃犳硶鍚姩缁勪欢[StandardEngine[Catalina].StandardHost[localhost].StandardContext[/blade-api]]
at org.apache.catalina.util.LifecycleBase.handleSubClassException(LifecycleBase.java:440)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:198)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:753)
... 9 more
Caused by: org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is java.lang.NoSuchMethodError: org.apache.tomcat.util.modeler.Registry.disableRegistry()V
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:161)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:545)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:755)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:402)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:312)
at org.springframework.boot.web.servlet.support.SpringBootServletInitializer.run(SpringBootServletInitializer.java:173)
at org.springframework.boot.web.servlet.support.SpringBootServletInitializer.createRootApplicationContext(SpringBootServletInitializer.java:153)
at org.springframework.boot.web.servlet.support.SpringBootServletInitializer.onStartup(SpringBootServletInitializer.java:95)
at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:172)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5221)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
... 10 more
Caused by: java.lang.NoSuchMethodError: org.apache.tomcat.util.modeler.Registry.disableRegistry()V
at org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.getWebServer(TomcatServletWebServerFactory.java:177)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:178)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:158)
... 22 more
五、若有更多详细信息,请在下面提供。
希望作者能能重视下这个问题实践下。论坛上的几个方法都试了。在群里也没有回复。
按照如下步骤尝试,如果还不行,麻烦发一个邮件到bladejava@qq.com附上帖子地址并说明使用的bladex版本
1. 添加tomcat依赖
org.springframework.boot
spring-boot-starter-tomcat
provided
2. blade-core-boot排除undertow依赖
org.springblade
blade-core-boot
org.springblade
blade-core-cloud
org.springframework.boot
spring-boot-starter-undertow
3.pom文件修改打包类型为war
org.springblade
BladeX-Boot
war
2.8.2.RELEASE
4.添加war包插件
org.apache.maven.plugins
maven-war-plugin
false
5.application.yaml添加修改
#服务器配置
server:
port: 80
spring:
application:
name: blade-api
profiles:
active: dev
main:
allow-bean-definition-overriding: true
6.Application类修改,最重要的一步(blade 4.x以上版本)
@EnableScheduling @SpringBootApplication(exclude= {DataSourceAutoConfiguration.class}) public class Application extends SpringBootServletInitializer { public static void main(String[] args) { BladeApplication.run(CommonConstant.APPLICATION_NAME, Application.class, args); } @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return BladeApplication.createSpringApplicationBuilder(CommonConstant.APPLICATION_NAME, Application.class, application).sources(Application.class); } }
7.打包
mvn package -Dmaven.test.skip=true
======补充说明=====
8. 如果是blade 4.x以前的版本,需要新建一个启动类 BladeXApplication,原版代码如下:https://gitee.com/smallc/blade-tool/blob/master/blade-core-launch/src/main/java/org/springblade/core/launch/BladeApplication.java
比如创建一个BladeXApplication到业务工程,然后拷贝如下代码,之后把启动类的BladeApplication改成BladeXApplication即可:
package org.springblade.core.launch;
import org.springblade.core.launch.constant.AppConstant;
import org.springblade.core.launch.constant.NacosConstant;
import org.springblade.core.launch.service.LauncherService;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.*;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* 项目启动器,搞定环境变量问题
*
* @author Chill
*/
public class BladeXApplication {
/**
* Create an application context
* java -jar app.jar --spring.profiles.active=prod --server.port=2333
*
* @param appName application name
* @param source The sources
* @return an application context created from the current state
*/
public static ConfigurableApplicationContext run(String appName, Class source, String... args) {
SpringApplicationBuilder builder = createSpringApplicationBuilder(appName, source, args);
return builder.run(args);
}
public static SpringApplicationBuilder createSpringApplicationBuilder(String appName, Class source, String... args) {
return createSpringApplicationBuilder(appName, source, null, args);
}
public static SpringApplicationBuilder createSpringApplicationBuilder(String appName, Class source, SpringApplicationBuilder builder, String... args) {
Assert.hasText(appName, "[appName]服务名不能为空");
// 读取环境变量,使用spring boot的规则
ConfigurableEnvironment environment = new StandardEnvironment();
MutablePropertySources propertySources = environment.getPropertySources();
propertySources.addFirst(new SimpleCommandLinePropertySource(args));
propertySources.addLast(new MapPropertySource(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME, environment.getSystemProperties()));
propertySources.addLast(new SystemEnvironmentPropertySource(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, environment.getSystemEnvironment()));
// 获取配置的环境变量
String[] activeProfiles = environment.getActiveProfiles();
// 判断环境:dev、test、prod
List profiles = Arrays.asList(activeProfiles);
// 预设的环境
List presetProfiles = new ArrayList<>(Arrays.asList(AppConstant.DEV_CODE, AppConstant.TEST_CODE, AppConstant.PROD_CODE));
// 交集
presetProfiles.retainAll(profiles);
// 当前使用
List activeProfileList = new ArrayList<>(profiles);
Function
@EnableScheduling @SpringBootApplication(exclude= {DataSourceAutoConfiguration.class}) public class Application extends SpringBootServletInitializer { public static void main(String[] args) { BladeXApplication.run(CommonConstant.APPLICATION_NAME, Application.class, args); } @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return BladeXApplication.createSpringApplicationBuilder(CommonConstant.APPLICATION_NAME,Application.class,application).sources(Application.class); } }
扫一扫访问 Blade技术社区 移动端