java Spring Boot:无法配置

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

Spring Boot: Unable to configure

javaspringspring-boot

提问by Forkmohit

It seems(maybe not) that, SpringBoot is unable to load application.properties file which resides on src/main/resources. What extra config I need to add to boot my app?

似乎(也许不是),SpringBoot 无法加载位于 src/main/resources 上的 application.properties 文件。我需要添加什么额外的配置来启动我的应用程序?

Error Trace:

错误跟踪:

Caused by:

造成的:

org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (no profiles are currently active).

org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException:无法确定数据库类型 NONE 的嵌入式数据库驱动程序类。如果您想要一个嵌入式数据库,请在类路径上放置一个受支持的数据库。如果您有要从特定配置文件加载的数据库设置,您可能需要激活它(当前没有配置文件处于活动状态)。

MembersApplication.java

成员应用程序.java

@SpringBootApplication
public class MembersApplication {

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

DatabaseConfig.java

数据库配置文件

@Configuration
@EnableTransactionManagement
public class DatabaseConfig {

  @Value("${db.driver}")
  private String DB_DRIVER;

  @Value("${db.password}")
  private String DB_PASSWORD;

  @Value("${db.url}")
  private String DB_URL;

  @Value("${db.username}")
  private String DB_USERNAME;

  @Value("${hibernate.dialect}")
  private String HIBERNATE_DIALECT;

  @Value("${hibernate.show_sql}")
  private String HIBERNATE_SHOW_SQL;

  @Value("${hibernate.hbm2ddl.auto}")
  private String HIBERNATE_HBM2DDL_AUTO;

  @Value("${entitymanager.packagesToScan}")
  private String ENTITYMANAGER_PACKAGES_TO_SCAN;

  @Bean
  public DataSource dataSource() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName(DB_DRIVER);
    dataSource.setUrl(DB_URL);
    dataSource.setUsername(DB_USERNAME);
    dataSource.setPassword(DB_PASSWORD);
    return dataSource;
  }

  @Bean
  public LocalSessionFactoryBean sessionFactory() {
    LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
    sessionFactoryBean.setDataSource(dataSource());
    sessionFactoryBean.setPackagesToScan(ENTITYMANAGER_PACKAGES_TO_SCAN);
    Properties hibernateProperties = new Properties();
    hibernateProperties.put("hibernate.dialect", HIBERNATE_DIALECT);
    hibernateProperties.put("hibernate.show_sql", HIBERNATE_SHOW_SQL);
    hibernateProperties.put("hibernate.hbm2ddl.auto", HIBERNATE_HBM2DDL_AUTO);
    sessionFactoryBean.setHibernateProperties(hibernateProperties);

    return sessionFactoryBean;
  }

  @Bean
  public HibernateTransactionManager transactionManager() {
    HibernateTransactionManager transactionManager = 
        new HibernateTransactionManager();
    transactionManager.setSessionFactory(sessionFactory().getObject());
    return transactionManager;
  }

采纳答案by luboskrnac

You are missing DB JAR on your classpath. If you are using Maven/Gradle, just include JDBC driver dependency into your POM/gradle file.

您的类路径中缺少 DB JAR。如果您使用 Maven/Gradle,只需将 JDBC 驱动程序依赖项包含到您的 POM/gradle 文件中。