java.lang.NoSuchMethodException 在 tomcat 上部署时 Spring-boot web 应用程序出错

java.lang.NoSuchMethodException error in Spring-boot web-application when deploying on tomcat

我正在 'tomcat 9' 上部署一个 Spring-boot 网络应用程序,我遇到了 NoSuchMethodException。我在本地主机中成功编译并 运行 这段代码;但是当我将它部署到服务器上时,出现了这个错误。这是我的例外:

javax.servlet.ServletException: Failed to instantiate WebApplicationInitializer class
                at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:158)
                at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5135)
                at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
                at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:717)
                at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:690)
                at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:705)
                at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:978)
                at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1849)
                at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
                at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
                at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
                at java.base/java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:118)
                at org.apache.catalina.startup.HostConfig.deployWARs(HostConfig.java:773)
                at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:427)
                at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1576)
                at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:309)
                at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:123)
                at org.apache.catalina.util.LifecycleBase.setStateInternal(LifecycleBase.java:423)
                at org.apache.catalina.util.LifecycleBase.setState(LifecycleBase.java:366)
                at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:936)
                at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:841)
                at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
                at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1384)
                at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1374)
                at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
                at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
                at java.base/java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:140)
                at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:909)
                at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:262)
                at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
                at org.apache.catalina.core.StandardService.startInternal(StandardService.java:421)
                at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
                at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:930)
                at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
                at org.apache.catalina.startup.Catalina.start(Catalina.java:633)
                at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
                at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
                at java.base/java.lang.reflect.Method.invoke(Method.java:566)
                at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:343)
                at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:474)
        Caused by: java.lang.NoSuchMethodException: XXX.<init>()
                at java.base/java.lang.Class.getConstructor0(Class.java:3349)
                at java.base/java.lang.Class.getDeclaredConstructor(Class.java:2553)
                at org.springframework.util.ReflectionUtils.accessibleConstructor(ReflectionUtils.java:185)
                at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:155)
                ... 40 more

这是我的主要 class:

public class XXX extends SpringBootServletInitializer implements CommandLineRunner {
  private static final Logger logger = LogManager.getLogger("System");
  @Autowired
  FilesStorageService storageService;
  @Autowired
  CasUserDetailService casUserDetailService;

  @Value("${cas.url.logout.path}")
  private String casLogout;
  @Value("${cas.url.validator}")
  private String ticketValidator;
  @Value("${base.url}")
  private String baseurl;

  public XXX() {
  }


  public static void main(String[] args) {

    try {
      SpringApplication.run(XXX.class, args);
    } catch (Exception e) {
      e.printStackTrace();
    }
    logger.warn("Started!");
  }

  @Override
  public void run(String... arg) {
    // storageService.deleteAll();
    storageService.init();
  }

  @Bean
  public CasAuthenticationFilter casAuthenticationFilter(
      AuthenticationManager authenticationManager,
      ServiceProperties serviceProperties) {
    CasAuthenticationFilter filter = new CasAuthenticationFilter();
    filter.setAuthenticationManager(authenticationManager);
    filter.setServiceProperties(serviceProperties);
    return filter;
  }

  @Bean
  public ServiceProperties serviceProperties() {
    ServiceProperties serviceProperties = new ServiceProperties();
    serviceProperties.setService(baseurl + "/login/cas");
    serviceProperties.setSendRenew(false);
    return serviceProperties;
  }

  @Bean
  public TicketValidator ticketValidator() {
    return new Cas30ServiceTicketValidator(ticketValidator);
  }

  @Bean
  public CasAuthenticationProvider casAuthenticationProvider() {
    CasAuthenticationProvider provider = new CasAuthenticationProvider();
    provider.setServiceProperties(serviceProperties());
    provider.setTicketValidator(ticketValidator());

    provider.setAuthenticationUserDetailsService(casUserDetailService);
    provider.setKey("CAS_PROVIDER_LOCALHOST_8900");
    return provider;
  }

  @Bean
  public SecurityContextLogoutHandler securityContextLogoutHandler() {
    return new SecurityContextLogoutHandler();
  }

  @Bean
  public LogoutFilter logoutFilter() {
    LogoutFilter logoutFilter = new LogoutFilter(casLogout, securityContextLogoutHandler());
    logoutFilter.setFilterProcessesUrl("/logout/cas");
    return logoutFilter;
  }

  @Bean
  public SingleSignOutFilter singleSignOutFilter() {
    SingleSignOutFilter singleSignOutFilter = new SingleSignOutFilter();
    // singleSignOutFilter.setCasServerUrlPrefix(casLogout);
    singleSignOutFilter.setLogoutCallbackPath("/exit/cas");
    singleSignOutFilter.setIgnoreInitConfiguration(true);
    return singleSignOutFilter;
  }

}

这是我的 gradle.build 文件

plugins {
    id 'org.springframework.boot' version '2.3.0.RELEASE'
    id 'io.spring.dependency-management' version '1.0.9.RELEASE'
    id 'java'
    id 'war'
}

dependencyManagement {
    imports {
        mavenBom 'org.apache.logging.log4j:log4j-bom:2.14.1'
    }
}

group = 'xxx'
version = 'xxx'
sourceCompatibility = '1.8'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
    maven {
        url "https://git.captcha.com/botdetect-java-captcha.git/blob_plain/HEAD:/"
    }
    jcenter()
}

dependencies {
    implementation 'org.json:json:20201115'
    implementation 'org.springframework.boot:spring-boot-starter-security'
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-data-ldap'
    implementation 'org.springframework.boot:spring-boot-starter-data-rest'
    implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity5'
    implementation 'org.thymeleaf:thymeleaf-spring5'
    implementation 'org.springframework.security:spring-security-web'
    implementation 'jakarta.xml.bind:jakarta.xml.bind-api:2.3.3'
    implementation 'com.sun.xml.ws:jaxws-ri:2.3.2'
    compile 'io.jsonwebtoken:jjwt-api:0.11.1'

    compile 'com.captcha:botdetect-jsp20:4.0.beta3.7'

    // https://mvnrepository.com/artifact/commons-io/commons-io
    compile group: 'commons-io', name: 'commons-io', version: '2.6'

    runtime 'io.jsonwebtoken:jjwt-impl:0.11.1',
            'io.jsonwebtoken:jjwt-jackson:0.11.1'
    implementation 'org.springframework.security:spring-security-config'
    implementation 'org.springframework.ldap:spring-ldap-core'
    implementation 'org.springframework.security:spring-security-ldap'
    implementation 'com.unboundid:unboundid-ldapsdk'
    implementation 'org.springframework.data:spring-data-redis'
    implementation 'redis.clients:jedis'
    compile 'com.googlecode.json-simple:json-simple:1.1.1'
    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'
    providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
    testImplementation 'org.springframework.security:spring-security-test'
    compile group: 'org.springframework', name: 'spring-context-support', version: '5.2.6.RELEASE'

    // https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient
    compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.12'

    // https://mvnrepository.com/artifact/com.google.code.gson/gson
    compile group: 'com.google.code.gson', name: 'gson', version: '2.8.6'
    
    // https://mvnrepository.com/artifact/io.springfox/springfox-swagger2
    compile group: 'io.springfox', name: 'springfox-swagger2', version: '2.9.2'

    // https://mvnrepository.com/artifact/com.google.zxing/core
    compile group: 'com.google.zxing', name: 'core', version: '3.4.0'

    // https://mvnrepository.com/artifact/com.google.zxing/javase
    compile group: 'com.google.zxing', name: 'javase', version: '3.4.0'

    compile group: 'com.sun.mail', name: 'javax.mail', version: '1.5.1'
    // https://mvnrepository.com/artifact/org.springframework.security/spring-security-cas
    compile group: 'org.springframework.security', name: 'spring-security-cas', version: '5.3.3.RELEASE'

    // https://mvnrepository.com/artifact/org.apache.poi/poi
    compile group: 'org.apache.poi', name: 'poi', version: '4.1.2'
    implementation 'org.apache.poi:poi-ooxml:4.1.2'

    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }



    // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-mongodb
    implementation group: 'org.springframework.boot', name: 'spring-boot-starter-data-mongodb', version: '2.4.5'

    // https://mvnrepository.com/artifact/com.github.ua-parser/uap-java
    compile group: 'com.github.ua-parser', name: 'uap-java', version: '1.4.0'

    // https://mvnrepository.com/artifact/org.springframework/spring-websocket
    compile group: 'org.springframework', name: 'spring-websocket', version: '4.0.0.RELEASE'

    // https://mvnrepository.com/artifact/io.jsonwebtoken/jjwt
    compile group: 'io.jsonwebtoken', name: 'jjwt', version: '0.9.1'

    // https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api
    compile group: 'javax.xml.bind', name: 'jaxb-api', version: '2.4.0-b180830.0359'

    // https://mvnrepository.com/artifact/org.jboss.aerogear/aerogear-otp-java
    compile group: 'org.jboss.aerogear', name: 'aerogear-otp-java', version: '1.0.0'

    // https://mvnrepository.com/artifact/org.springframework/spring-websocket
    compile group: 'org.springframework', name: 'spring-websocket', version: '5.2.9.RELEASE'

    // https://mvnrepository.com/artifact/io.jsonwebtoken/jjwt
    compile group: 'io.jsonwebtoken', name: 'jjwt', version: '0.9.1'

    // https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api
    compile group: 'javax.xml.bind', name: 'jaxb-api', version: '2.4.0-b180830.0359'

    // https://mvnrepository.com/artifact/org.jboss.aerogear/aerogear-otp-java
    compile group: 'org.jboss.aerogear', name: 'aerogear-otp-java', version: '1.0.0'

    // https://mvnrepository.com/artifact/org.springframework/spring-websocket
    compile group: 'org.springframework', name: 'spring-websocket', version: '5.2.9.RELEASE'

    // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-websocket
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-websocket', version: '2.3.4.RELEASE'

    // https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core
    implementation group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.14.1'

    // https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api
    implementation group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.14.1'

    implementation 'org.apache.logging.log4j:log4j-mongodb4:2.14.1'

    // https://mvnrepository.com/artifact/commons-codec/commons-codec
    implementation group: 'commons-codec', name: 'commons-codec', version: '1.15'

    // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-mail
    implementation group: 'org.springframework.boot', name: 'spring-boot-starter-mail', version: '2.5.1'

    // https://mvnrepository.com/artifact/org.javers/javers-spring
    implementation group: 'org.javers', name: 'javers-spring', version: '6.2.4'

    // https://mvnrepository.com/artifact/org.javers/javers-core
    implementation group: 'org.javers', name: 'javers-core', version: '6.2.4'

    // https://mvnrepository.com/artifact/org.apache.commons/commons-lang3
    implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.12.0'

    // https://mvnrepository.com/artifact/org.bitbucket.b_c/jose4j
    implementation group: 'org.bitbucket.b_c', name: 'jose4j', version: '0.7.9'



    configurations.all {
        resolutionStrategy.dependencySubstitution.all { dependency ->
            if (dependency.requested instanceof ModuleComponentSelector && dependency.requested.module == 'spring-boot-starter-logging') {
                dependency.useTarget("org.springframework.boot:spring-boot-starter-log4j2:$dependency.requested.version", 'Use Log4j2 instead of Logback')
            }
        }
    }
}

test {
    useJUnitPlatform()
}

这是我的代码,正如它所说,tomcat 找不到 main class 的默认构造函数。但是我已经添加了它,我仍然得到这个错误。

我的问题是 git 存储库问题