Java SpringBoot - 无法启动嵌入式 TomCat

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36859646/
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-08-11 18:45:02  来源:igfitidea点击:

SpringBoot - Unable to Start embedded TomCat

javaspringtomcat

提问by besmart

I have a Spring Boot application. It runs on 2 servers. The servers have the same configuration. One one of this it works... on the other i'm getting this exception when starting it

我有一个 Spring Boot 应用程序。它在 2 台服务器上运行。服务器具有相同的配置。其中一个它有效......另一方面,我在启动它时遇到了这个异常

2016-04-26 08:24:17.633 ERROR [localhost-startStop-1]: Error starting Tomcat context: org.springframework.beans.factory.BeanCreationException
2016-04-26 08:24:17.903 ERROR [main]: Application startup failed
org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to start embedded Tomcat
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:133)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:532)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766)
    at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:307)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180)
    at it.besmart.parkserver.StartServer.main(StartServer.java:13)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:54)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to start embedded Tomcat
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.initialize(TomcatEmbeddedServletContainer.java:99)
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.<init>(TomcatEmbeddedServletContainer.java:76)
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.getTomcatEmbeddedServletContainer(TomcatEmbeddedServletContainerFactory.java:457)
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.getEmbeddedServletContainer(TomcatEmbeddedServletContainerFactory.java:168)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:160)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:130)
    ... 14 more

The stack trace continues for a lot of rows, mostly I get problems on auto wiring and injecting activities, with root cause

堆栈跟踪持续了很多行,主要是我在自动连接和注入活动方面遇到问题,根本原因是

Invocation of init method failed; nested exception is org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set

But my db.properties file is:

但我的 db.properties 文件是:

jdbc.driverClassName = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://192.168.3.240:3306/SMARTPARK?useSSL=false
jdbc.username = parkuser
jdbc.password = xxxxxxxxxxxxxxxx
hibernate.dialect = org.hibernate.dialect.MySQLDialect
hibernate.show_sql = false
hibernate.format_sql = false

The DB is running (second server connects to it regularly..) and all privileges to users and hosts are correct

数据库正在运行(第二台服务器定期连接到它......)并且用户和主机的所有权限都是正确的

This is my pom.xml

这是我的 pom.xml

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

    <groupId>it.besmart</groupId>
    <artifactId>eparkserver</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>

    <name>eparkserver</name>
    <description>ePark server</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.3.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
        <start-class>it.besmart.parkserver.StartServer</start-class>
        <!-- 
        <tomcat.version>8.0.29</tomcat.version>
         -->
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>com.pi4j</groupId>
            <artifactId>pi4j-core</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.Hymanson.core</groupId>
            <artifactId>Hymanson-core</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.Hymanson.core</groupId>
            <artifactId>Hymanson-databind</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <executable>true</executable>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <id>server-copy</id>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <phase>package</phase>
                        <configuration>
                            <target>
                                <echo message="Push to server /home/pi/park/" />
                                <scp trust="yes" todir="pi:[email protected]:/home/pi/park/">
                                    <fileset dir="${basedir}/target">
                                    </fileset>
                                </scp>
                            </target>
                        </configuration>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.apache.ant</groupId>
                        <artifactId>ant-jsch</artifactId>
                        <version>1.9.6</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
</project>

All my hibernate settings are in HibernateConfiguration.class (i migrated this app from Spring MVC to Spring Boot)

我所有的休眠设置都在 HibernateConfiguration.class 中(我将此应用程序从 Spring MVC 迁移到 Spring Boot)

@Configuration
@EnableTransactionManagement
@ComponentScan({ "it.besmart" })
@PropertySource(value = { "classpath:db.properties" })
public class HibernateConfiguration {

@Autowired
private Environment environment;

@Bean
public LocalSessionFactoryBean sessionfactory(){
    LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
    sessionFactory.setDataSource(dataSource());
    sessionFactory.setPackagesToScan(new String[] {"it.besmart.models"});
    sessionFactory.setHibernateProperties(hibernateProperties());
    return sessionFactory;
}

@Bean
public DataSource dataSource() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName"));
    dataSource.setUrl(environment.getRequiredProperty("jdbc.url"));
    dataSource.setUsername(environment.getRequiredProperty("jdbc.username"));
    dataSource.setPassword(environment.getRequiredProperty("jdbc.password"));
    return dataSource;
}

private Properties hibernateProperties() {
    Properties properties = new Properties();
    properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect"));
    properties.put("hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql"));
    properties.put("hibernate.format_sql", environment.getRequiredProperty("hibernate.format_sql"));
    return properties;        
}

@Bean
@Autowired
public HibernateTransactionManager transactionManager(SessionFactory s) {
   HibernateTransactionManager txManager = new HibernateTransactionManager();
   txManager.setSessionFactory(s);
   return txManager;
}
}

I don't know what is happening

我不知道发生了什么

回答by Abdel

1 ) When using Spring Boot you should not include the other Spring dependencies directly, but rely on Boot's own dependency management. When using the provided "starters" you can be sure that all needed libraries will be included in a matching version. Documentation : https://spring.io/guides/gs/spring-boot/#use-maven

1) 使用Spring Boot时不应该直接包含其他Spring依赖,而是依赖Boot自己的依赖管理。使用提供的“入门”时,您可以确保所有需要的库都包含在匹配的版本中。文档:https: //spring.io/guides/gs/spring-boot/#use-maven

2) The scheduling guide isn't a web app so you probably have some mouldy stuff in your pom.xml from the REST guide?

2)调度指南不是一个网络应用程序,所以你的 pom.xml 中可能有一些来自 REST 指南的发霉的东西?

A "good" pom would have "spring-boot-starter-web" (for convenience) or else all the dependencies included in the starter listed individually. Just check that you have them. Also make sure you include an @EnableAutoConfiguration in your SpringApplication. Add the classes to run in an Object array to SpringApplication's run method : SpringApplication.run(new Object[]{Application.class, ScheduledTasks.class}, args);

“好的”pom 将具有“spring-boot-starter-web”(为方便起见)或单独列出的启动器中包含的所有依赖项。只需检查您是否拥有它们。还要确保在 SpringApplication 中包含 @EnableAutoConfiguration。将在 Object 数组中运行的类添加到 SpringApplication 的 run 方法中: SpringApplication.run(new Object[]{Application.class, ScheduledTasks.class}, args);

Enjoy :)

享受 :)

回答by Jorge Machado

I had the same problem. Add this :

我有同样的问题。添加这个:

<dependency>
<groupId><groupId></groupId>
<artifactId><some dependency></artifactId>
<version><version></version>
<exclusions>
    <exclusion>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
    </exclusion>
</exclusions>

回答by Pratik Shah

Add a new file under src/main/resources with name application.properties

在 src/main/resources 下添加一个名为 application.properties 的新文件

spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://192.168.3.240:3306/SMARTPARK?useSSL=false
spring.datasource.password=xxxxxxxxxxxxxxxx
spring.datasource.username=parkuser
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
spring.jpa.show-sql=true

Remove HibernateConfigurationclass & on your Main class put @EnableTransactionManagementannotation.

删除HibernateConfigurationclass & 在您的 Main 类上放置@EnableTransactionManagement注释。

回答by isuruh

Probably you can avoid this by changing your project sdk.

也许您可以通过更改项目 sdk 来避免这种情况。

In my project I initially used java-11-openjdk-amd64as my JDK and had the same issue.

在我的项目中,我最初使用java-11-openjdk-amd64作为我的 JDK 并且遇到了同样的问题。

After changing JDK into java-8-oracleit worked.

将 JDK 更改为java-8-oracle 后,它起作用了。

回答by Xumn

Probably It's about the servlet.jar confliction between 'starter-web' and embedded tomcat ,use the following instead.

可能是关于 'starter-web' 和嵌入式 tomcat 之间的 servlet.jar 冲突,请改用以下内容。

 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-web</artifactId>
 <version>2.0.4.RELEASE</version>

回答by Dickson the developer

On my case, the project was using Java 8but in my configuration it was set to Java 11. Go to project structure-> Project-> Project SDKand change the java version. I hope it helps.

在我的情况下,该项目正在使用,Java 8但在我的配置中它被设置为Java 11. 转到项目结构->项目->项目 SDK并更改java 版本。我希望它有帮助。

回答by kaya

Suddenly i faced the same error, but for me it was the dependency below, just answering here to save others wasting time

突然我遇到了同样的错误,但对我来说是下面的依赖,只是在这里回答以节省其他人浪费时间

spring-boot-devtools

spring-boot-devtools