java 如何解决“示例化WebApplicationInitializer类失败”

nom7f22z  于 2023-06-20  发布在  Java
关注(0)|答案(3)|浏览(189)

我在GitHub repository上找到了一个从MySQL生成odata的好例子
我可以在eclipse中使用mvn spring-boot:run -P jar命令来运行这个项目。与原始代码相比,我唯一改变的部分是application.properties文件中与本地MySQL的连接细节:

# WEB SERVER 
server.port=9090

# MYSQL DATA SOURCE
spring.datasource.url = jdbc:mysql://localhost:3306/olingo
spring.datasource.username = admin
spring.datasource.password = 123456
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1

# HANA (CLOUD) DB
#spring.datasource.jndi-name = java:comp/env/jdbc/DefaultDB
#spring.jpa.hibernate.ddl-auto = update
#spring.datasource.testWhileIdle = true
#spring.datasource.validationQuery = SELECT 1

# HSQL DB
#spring.datasource.platform=hsqldb

# JPA / HIBERNATE
spring.jpa.show-sql = false
spring.jpa.hibernate.ddl-auto = update
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy

# AZURE 
#logging.path = D://home//LogFiles

下面是pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.penninkhof</groupId>
    <artifactId>odata</artifactId>
    <version>0.1.0</version>
    <packaging>${packaging.type}</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.2.RELEASE</version>
    </parent>

    <properties>
        <java.version>1.7</java.version>
        <cxf.version>3.1.5</cxf.version>
        <olingo.version>2.0.6</olingo.version>
    </properties>

    <profiles>

        <!-- HANA Cloud Platform -->
        <profile>
            <id>hcp</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <packaging.type>war</packaging.type>
                <tomcat.version>7.0.59</tomcat.version>
            </properties>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-web</artifactId>
                    <!-- Some exclusions are necessary to make Spring Boot run on HCP. -->
                    <exclusions>
                        <exclusion>
                            <groupId>org.springframework.boot</groupId>
                            <artifactId>spring-boot-starter-tomcat</artifactId>
                        </exclusion>
                        <exclusion>
                            <groupId>org.apache.tomcat.embed</groupId>
                            <artifactId>tomcat-embed-el</artifactId>
                        </exclusion>
                        <exclusion>
                            <artifactId>logback-classic</artifactId>
                            <groupId>ch.qos.logback</groupId>
                        </exclusion>
                    </exclusions>
                </dependency>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-data-jpa</artifactId>
                    <!-- Without this exclusions, the HCP runtime will not be so happy -->
                    <exclusions>
                        <exclusion>
                            <artifactId>logback-classic</artifactId>
                            <groupId>ch.qos.logback</groupId>
                        </exclusion>
                    </exclusions>
                </dependency>
                <dependency>
                    <groupId>org.apache.olingo</groupId>
                    <artifactId>olingo-odata2-jpa-processor-api</artifactId>
                    <version>${olingo.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.apache.olingo</groupId>
                    <artifactId>olingo-odata2-jpa-processor-core</artifactId>
                    <version>${olingo.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-api</artifactId>
                    <scope>provided</scope>
                </dependency>
                <dependency>
                    <groupId>javax.servlet</groupId>
                    <artifactId>javax.servlet-api</artifactId>
                    <version>3.0.1</version>
                    <scope>provided</scope>
                </dependency>
            </dependencies>
        </profile>

        <!-- Self-contained JAR -->
        <profile>
            <id>jar</id>
            <properties>
                <packaging.type>jar</packaging.type>
            </properties>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-web</artifactId>
                </dependency>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-data-jpa</artifactId>
                </dependency>
                <dependency>
                    <groupId>org.apache.olingo</groupId>
                    <artifactId>olingo-odata2-jpa-processor-api</artifactId>
                    <version>${olingo.version}</version>
                    <exclusions>
                        <exclusion>
                            <artifactId>javax.persistence</artifactId>
                            <groupId>org.eclipse.persistence</groupId>
                        </exclusion>
                    </exclusions>
                </dependency>
                <dependency>
                    <groupId>org.apache.olingo</groupId>
                    <artifactId>olingo-odata2-jpa-processor-core</artifactId>
                    <version>${olingo.version}</version>
                    <exclusions>
                        <exclusion>
                            <artifactId>javax.persistence</artifactId>
                            <groupId>org.eclipse.persistence</groupId>
                        </exclusion>
                    </exclusions>
                </dependency>
            </dependencies>
            <build>
                <plugins>
                    <plugin>
                        <artifactId>maven-resources-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>copy-resources</id>
                                <phase>validate</phase>
                                <goals>
                                    <goal>copy-resources</goal>
                                </goals>
                                <configuration>
                                    <outputDirectory>${basedir}/target/classes/static</outputDirectory>
                                    <resources>
                                        <resource>
                                            <directory>src/main/webapp</directory>
                                            <filtering>true</filtering>
                                        </resource>
                                    </resources>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>

    </profiles>

    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hsqldb</groupId>
            <artifactId>hsqldb</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxrs</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.olingo</groupId>
            <artifactId>olingo-odata2-api</artifactId>
            <version>${olingo.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.olingo</groupId>
            <artifactId>olingo-odata2-core</artifactId>
            <version>${olingo.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.olingo</groupId>
            <artifactId>olingo-odata2-api-annotation</artifactId>
            <version>${olingo.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <pluginRepositories>
        <pluginRepository>
            <id>spring-releases</id>
            <name>Spring Releases</name>
            <url>https://repo.spring.io/libs-release</url>
        </pluginRepository>
    </pluginRepositories>

</project>

当我尝试在使用Tomcat 9时以Run on a Server运行项目时,将显示以下错误:
严重:ServletContainerInitializer处理javax. servlet时出错。ServletException:无法在组织上示例化WebApplicationInitializer类。SpringFramework Web.SpringServletContainerInitializer。onStartup(SpringServletContainerInitializer.java:158)at org.Apachecatalina核心StandardContext。startInternal(StandardContext.java:5140)at org.Apachecatalina效用LifecycleBase. start(LifecycleBase.java:183)at org.Apachecatalina核心ContainerBase $www.example.com(ContainerBase.java:1384)at org.Apachecatalina核心ContainerBase $www.example.com(ContainerBase.java:1374)at www.example.com(Unknown Source)at org.Apache雄猫效用线程。InlineExecutorService。在java中执行(InlineExecutorService.java:75)。效用并发AbstractExecutorService.在org上提交(未知来源)。Apachecatalina核心ContainerBase startInternal(ContainerBase.java:909)at org.Apachecatalina核心标准主机。startInternal(StandardHost.java:841)at org.Apachecatalina效用LifecycleBase. start(LifecycleBase.java:183)at org.Apachecatalina核心ContainerBase $StartChild.calljava.lang.NoClassDefFoundError: StartChild.call 29更多Caused by:java.util.concurrent.FutureTask.run Copyright © 2018 - 2019 www.lf4j.loggerFactory All Rights Reserved.粤ICP备16049566号-1 www.example.com (ContainerBase.java:1384) at org.apache.catalina.core.ContainerBase$StartChild.call (ContainerBase.java:1374) at java.util.concurrent.FutureTask.run (Unknown Source) at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75) at java.util.concurrent.AbstractExecutorService.submit(Unknown Source) 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 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:343) at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:474) Caused by: java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory at com.penninkhof.odata.Application.(Application.java:18) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:155) ... 29 more Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1365) at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1188) ... 35 more
我可以用mvn clean install构建项目,但当我在Tomcat中部署war文件时,它无法运行应用程序。看来同样的问题是问题的根源!
任何帮助解决这个问题是赞赏。

ldfqzlk8

ldfqzlk81#

我觉得你没让斯普林知道豆子课的事。查看LoggerFactory的xml或配置文件在上面的异常堆栈中,它说发生了java.lang.ClassNotFoundException。Spring无法找到类文件,即使你在java src包中有它们。如果我的回答不能解决你的问题,你需要展示你的xml或源代码来确定问题是什么。

cnh2zyt3

cnh2zyt32#

我更改了pom.xml并删除了配置文件,然后我可以使用mvn clean install构建项目,这将导致可以在Tomcat中部署的war文件。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.penninkhof</groupId>
    <artifactId>odata</artifactId>
    <version>0.1.0</version>
    <packaging>war</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.2.RELEASE</version>
    </parent>

    <properties>
        <java.version>1.7</java.version>
        <cxf.version>3.1.5</cxf.version>
        <olingo.version>2.0.6</olingo.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.olingo</groupId>
            <artifactId>olingo-odata2-jpa-processor-core</artifactId>
            <version>${olingo.version}</version>
        </dependency>       
        <dependency>
            <groupId>org.apache.olingo</groupId>
            <artifactId>olingo-odata2-jpa-processor-api</artifactId>
            <version>${olingo.version}</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hsqldb</groupId>
            <artifactId>hsqldb</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxrs</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.olingo</groupId>
            <artifactId>olingo-odata2-api</artifactId>
            <version>${olingo.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.olingo</groupId>
            <artifactId>olingo-odata2-core</artifactId>
            <version>${olingo.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.olingo</groupId>
            <artifactId>olingo-odata2-api-annotation</artifactId>
            <version>${olingo.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${basedir}/target/classes/static</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>src/main/webapp</directory>
                                    <filtering>true</filtering>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <pluginRepositories>
        <pluginRepository>
            <id>spring-releases</id>
            <name>Spring Releases</name>
            <url>https://repo.spring.io/libs-release</url>
        </pluginRepository>
    </pluginRepositories>

</project>

然而,当我在Tomcat中部署它时,它可以访问数据库几个小时,几个小时后,它就不能再从MySQL中获取数据了!

当我们想在Eclipse中使用Run on Server选项运行应用程序时,它需要在像这样的http://localhost:9090/<artifactName>/成功运行后访问应用程序。http://localhost:9090/odata/在这种情况下!

jucafojl

jucafojl3#

对于仍然收到这种类型的错误的人来说,这可能是因为您的主SpringBootApplication中的NoArgs构造函数不可用。因此tomcat无法初始化Servlet。在你的主类中添加一个NoArgs构造函数,以防你错过了它。希望这能帮上忙。

相关问题