简体   繁体   中英

Spring boot war file does not work on tomcat

I added spring boot to a existing webapp. When i run the command

java -jar -Denvironment.type=dev myfile.war

Every things goes fine. But if I deploy on tomcat, for some reason a get a very big exception.

Failed to instantiate [javax.sql.DataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath.

I am using mongodb and I do not have any datasource configured on my application context. I also extended SpringBootServletInitializer

@SpringBootApplication
public class AdminApp extends SpringBootServletInitializer {

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
    return builder.sources(AdminApp.class);
}

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

Any clue what can it be?

My properties file

database.url=localhost
database.port=27017
database.name=dbname
database.username=admin
database.password=admin

Update: I also have this class the says which property file should be used.

@Configuration
@PropertySource("classpath:application-${environment.type}.properties")
public class PropertyWithJavaConfig {

@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    return new PropertySourcesPlaceholderConfigurer();
}

}

The error is thrown in DataSourceProperties.getDriverClassName() method. Find below the source code of the same from spring distribution:

if (!StringUtils.hasText(driverClassName)) {
    throw new BeanCreationException(
        "Cannot determine embedded database driver class for database type "
            + this.embeddedDatabaseConnection
            + ". If you want an embedded "
            + "database please put a supported one on the classpath.");
}

Spring throws this error when spring.datasource.driverClassName property is empty. So to fix this error, make sure that the application.properties is in the classpath.

So, after digging a lot on the dependencies I noticed that I had spring-orm and spring-jdbc even though I was not using. I removed them and everything worked fine for embedded and local tomcat.

But I still can not understand why before worked just for embedded tomcat.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM