java 在 tomcat7 的类路径上未检测到 Spring WebApplicationInitializer 类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31777090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 19:05:55  来源:igfitidea点击:

No Spring WebApplicationInitializer types detected on classpath on tomcat7

javaspringtomcatwarbuild.gradle

提问by BAE

I am new to tomcat. I run "gradle tomcatRunWar". This web application can run locally and WebApplicationInitializerwas called. Then, I used "gradle war" to get war file. I deleted everything in /var/lib/tomcat7/webapps/. I copied myproject.war to /var/lib/tomcat7/webapps/ directory. Renamed it as ROOT.war. Re-started tomcat7. The server started to run. But I can not access my webpage on my_ip:8080/login OR my_ip:8080/myproject/login. The error is "HTTP Status 404 - /login". In the localhost.log, there is an info: “INFO: No Spring WebApplicationInitializer types detected on classpath”. I searched this problem by Google, some people had the same problems because of the lib version, jdk version. I have no idea how to debug.

我是 tomcat 的新手。我运行“gradle tomcatRunWar”。此 Web 应用程序可以在本地运行,并调用了WebApplicationInitializer。然后,我使用“gradle war”来获取war文件。我删除了 /var/lib/tomcat7/webapps/ 中的所有内容。我将 myproject.war 复制到 /var/lib/tomcat7/webapps/ 目录。将其重命名为 ROOT.war。重新启动tomcat7。服务器开始运行。但是我无法通过 my_ip:8080/login 或 my_ip:8080/myproject/login 访问我的网页。错误是“HTTP 状态 404 - /login”。在 localhost.log 中,有一个信息:“ INFO: No Spring WebApplicationInitializer types detection on classpath”。我用谷歌搜索了这个问题,因为lib版本,jdk版本,有人遇到了同样的问题。我不知道如何调试。

Apache Tomcat/7.0.52, java version "1.8.0_45" Java(TM) SE Runtime Environment (build 1.8.0_45-b14) Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)

Apache Tomcat/7.0.52,Java 版本“1.8.0_45” Java(TM) SE 运行时环境(构建 1.8.0_45-b14)Java HotSpot(TM) 64 位服务器 VM(构建 25.45-b02,混合模式)

build.gradle is:

build.gradle 是:

    buildscript {
    repositories {
        maven { url "http://repo.spring.io/libs-release" }
        mavenLocal()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.0.1.RELEASE")
        classpath 'org.gradle.api.plugins:gradle-tomcat-plugin:1.2.4'
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'spring-boot'
apply plugin: 'war'
apply plugin: 'groovy'
apply plugin: 'tomcat'


eclipse {
    jdt {
        sourceCompatibility = 1.6
        targetCompatibility = 1.7
    }
}

jar {
    baseName = 'myproject'
    version =  '0.1.0'
}

repositories {
    mavenCentral()
    maven { url "http://repo.spring.io/libs-release" }
}


dependencies {
    compile('commons-fileupload:commons-fileupload:1.3.1')
    compile("org.codehaus.Hymanson:Hymanson-mapper-asl:1.9.0")
    compile("org.springframework.boot:spring-boot-starter-web") {
        exclude group: 'ch.qos.logback', module: 'logback-classic'
        exclude group: 'org.slf4j'
    }
    compile("javax.servlet:javax.servlet-api:3.0.1")
    providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
    compile("org.springframework.data:spring-data-mongodb")  {
        exclude group: 'org.slf4j'
    }
    compile("org.springframework.security:spring-security-crypto:3.2.3.RELEASE")
    compile("com.fasterxml.Hymanson.core:Hymanson-databind")
    compile("joda-time:joda-time:2.2")
    compile("org.apache.directory.studio:org.apache.commons.codec:1.6")
    compile('com.amazonaws:aws-java-sdk:1.2.1')
    testCompile("junit:junit")
        runtime 'javax.servlet:jstl:1.2'
    providedCompile group:"org.apache.tomcat", name:"tomcat-catalina", version:"7.0.52" 
}

dependencies { 
    def tomcatVersion = '7.0.52' 
    tomcat "org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}", 
    "org.apache.tomcat.embed:tomcat-embed-logging-log4j:${tomcatVersion}" 
    tomcat("org.apache.tomcat.embed:tomcat-embed-jasper:${tomcatVersion}") { 
        exclude group: 'org.eclipse.jdt.core.compiler', module: 'ecj' 
    } 
}

tomcatRun { 
    daemon = true 
    httpPort = 8080 
    outputFile = file('/logs/tomcat.log') 
    stopKey = 'ctl-d'  
}

tomcatRunWar { 
    outputFile = file('/logs/tomcat.log') 
}

tomcatStop { 
    stopKey = 'ctl-d' 
}

The /var/lib/tomcat7/logs/catalina.out is:

/var/lib/tomcat7/logs/catalina.out 是:

Aug 03, 2015 12:41:30 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Aug 03, 2015 12:41:30 AM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 1009 ms
Aug 03, 2015 12:41:30 AM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Aug 03, 2015 12:41:30 AM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.52 (Ubuntu)
Aug 03, 2015 12:41:30 AM org.apache.catalina.startup.HostConfig deployDescriptor
INFO: Deploying configuration descriptor /etc/tomcat7/Catalina/localhost/examples.xml
Aug 03, 2015 12:41:31 AM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jstl/core_rt is already defined
Aug 03, 2015 12:41:31 AM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jstl/core is already defined
Aug 03, 2015 12:41:31 AM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jsp/jstl/core is already defined
Aug 03, 2015 12:41:31 AM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jstl/fmt_rt is already defined
Aug 03, 2015 12:41:31 AM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jstl/fmt is already defined
Aug 03, 2015 12:41:31 AM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jsp/jstl/fmt is already defined
Aug 03, 2015 12:41:31 AM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jsp/jstl/functions is already defined
Aug 03, 2015 12:41:31 AM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://jakarta.apache.org/taglibs/standard/permittedTaglibs is already defined
Aug 03, 2015 12:41:31 AM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://jakarta.apache.org/taglibs/standard/scriptfree is already defined
Aug 03, 2015 12:41:31 AM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jstl/sql_rt is already defined
Aug 03, 2015 12:41:31 AM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jstl/sql is already defined
Aug 03, 2015 12:41:31 AM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jsp/jstl/sql is already defined
Aug 03, 2015 12:41:31 AM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jstl/xml_rt is already defined
Aug 03, 2015 12:41:31 AM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jstl/xml is already defined
Aug 03, 2015 12:41:31 AM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jsp/jstl/xml is already defined
Aug 03, 2015 12:41:31 AM org.apache.catalina.startup.HostConfig deployDescriptor
INFO: Deploying configuration descriptor /etc/tomcat7/Catalina/localhost/docs.xml
Aug 03, 2015 12:41:31 AM org.apache.catalina.startup.HostConfig deployDescriptor
INFO: Deploying configuration descriptor /etc/tomcat7/Catalina/localhost/manager.xml
Aug 03, 2015 12:41:31 AM org.apache.catalina.startup.HostConfig deployDescriptor
INFO: Deploying configuration descriptor /etc/tomcat7/Catalina/localhost/host-manager.xml
Aug 03, 2015 12:41:31 AM org.apache.catalina.startup.HostConfig deployWAR
INFO: Deploying web application archive /var/lib/tomcat7/webapps/ROOT.war

Aug 03, 2015 12:41:37 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Aug 03, 2015 12:41:37 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 7788 ms

In the localhost.log, there is an info: “INFO: No Spring WebApplicationInitializer types detected on classpath

在 localhost.log 中,有一个信息:“ INFO: No Spring WebApplicationInitializer types detection on classpath

My Application.java is:

我的 Application.java 是:

    @Configuration
@EnableAutoConfiguration
public class Application extends SpringBootServletInitializer {
      @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
            return application.sources(Application.class);
        }

        public static void main(String[] args) throws Exception {
            SpringApplication.run(Application.class, args);
        }

}

My WebAppInitializer.java is:

我的 WebAppInitializer.java 是:

@Configuration
public class WebAppInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext)
            throws ServletException {

        AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
        appContext.register(Application.class);
        ServletRegistration.Dynamic dispatcher = servletContext.addServlet(
                "SpringDispatcher", new DispatcherServlet(appContext));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");
    }
}

UPDATE

更新

I used web.xml instead of WebApplicationInitializer. Now it works.

我使用 web.xml 而不是 WebApplicationInitializer。现在它起作用了。

回答by Andres Cespedes Morales

The problem is about your access directory, you need to verify what is your CATALINA_HOME environment variable, the log is clear "directory don't exists".

问题在于您的访问目录,您需要验证您的 CATALINA_HOME 环境变量是什么,日志明确“目录不存在”。

Can't to access from the execution location.

无法从执行位置访问。

SOLUTION: 1. First, try to start tomcat alone without using Gradle. 2. Deploy any hello world web application in your tomcat 3. Try to use Gradle Step by step 4. To learn more about Gradle look at here: http://examples.javacodegeeks.com/core-java/gradle/gradle-hello-world-tutorial/

解决方案: 1. 首先,尝试不使用Gradle 单独启动tomcat。2. 在您的 tomcat 中部署任何 hello world Web 应用程序 3. 尝试使用 Gradle 一步一步 4. 要了解有关 Gradle 的更多信息,请查看此处:http: //examples.javacodegeeks.com/core-java/gradle/gradle-hello -世界教程/

Regards.

问候。