【问题标题】:How to configure spring boot to set hibernate's config using application.properties如何配置spring boot以使用application.properties设置hibernate的配置
【发布时间】:2016-12-31 16:40:37
【问题描述】:

我一直在关注这个tutorial 在我的应用程序中实现 Hibernate。但是,我不知道如何阅读其中提到的 application.properties。我也读过doc,有人提到:

  • 当前目录的 /config 子目录。
  • 当前目录
  • 一个类路径 /config 包
  • 类路径根

但是spring boot的当前目录是什么?

我也使用以下依赖项(参见教程),我的问题可以通过使用它们来解决吗?它导致了依赖冲突:

<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>

此外,简单地将 applications.properties 放在 src/main/resources 中(如教程中所述)是行不通的。 如果我手动设置以下内容(这是我在 Spring Security 中使用的),它确实有效:

  @Bean(name = "dataSource")
  public DriverManagerDataSource dataSource() {
      DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
      driverManagerDataSource.setDriverClassName("com.mysql.jdbc.Driver");
      driverManagerDataSource.setUrl("jdbc:mysql://localhost:3306/hibernate");
      driverManagerDataSource.setUsername("root");
      driverManagerDataSource.setPassword("root");
      return driverManagerDataSource;
  }

但是如果我去掉之前的代码,肯定是找不到applications.properties:

Parameter 0 of constructor in org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration required a bean of type 'javax.sql.DataSource' that could not be found. - Bean method 'dataSource' not loaded because @ConditionalOnProperty (spring.datasource.jndi-name) did not find property 'jndi-name' - Bean method 'dataSource' not loaded because @ConditionalOnBean (types: org.springframework.boot.jta.XADataSourceWrapper; SearchStrategy: all) did not find any beans

有人知道如何解决这个问题,或者有线索吗?谢谢!

编辑:

所以对我所做的事情进行了更多解释。这是我项目的架构:http://imgur.com/a/V3f7p

正如我所说,我的 pom 不同,这里是:http://pastebin.com/fjrfKcVX

我有一个 WebMvcConfigurerAdapter,它不应该干扰任何东西:

@Configuration
@EnableWebMvc
public class MvcConfiguration extends WebMvcConfigurerAdapter {
    @Bean(name = "simpleMappingExceptionResolver")
    public SimpleMappingExceptionResolver createSimpleMappingExceptionResolver() {
        SimpleMappingExceptionResolver r = new SimpleMappingExceptionResolver();

        Properties mappings = new Properties();
       mappings.setProperty("ClassNotFoundException", "greeting");
       mappings.setProperty("SQLException", "greeting");
       mappings.setProperty("IOException", "greeting");

        r.setExceptionMappings(mappings); // None by default
        r.setDefaultErrorView("error"); // No default
        r.setExceptionAttribute("ex"); // Default is "exception"
        r.setWarnLogCategory("example.MvcLogger"); // No default
        return r;
    }

      @Override
        public void addViewControllers(ViewControllerRegistry registry) {
         /*   registry.addViewController("/accueil").setViewName("accueil");
            registry.addViewController("/").setViewName("accueil");
            registry.addViewController("/hello").setViewName("hello");
            registry.addViewController("/login").setViewName("login");*/
          registry.addViewController("/accessdenied").setViewName("accessdenied");
        }

    /*  @Bean(name = "dataSource")
      public DriverManagerDataSource dataSource() {
          DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
          driverManagerDataSource.setDriverClassName("com.mysql.jdbc.Driver");
          driverManagerDataSource.setUrl("jdbc:mysql://localhost:3306/ai_15");
          driverManagerDataSource.setUsername("root");
          driverManagerDataSource.setPassword("root");
          return driverManagerDataSource;
      }*/


}

除此之外,它是相同的,没有任何东西干扰 Hibernate。我刚刚将所有内容重命名为“Test”,例如 TestDAO.java

编辑 2:我的 application.properties 文件与教程中的相同,除了以下行:

spring.datasource.url = jdbc:mysql://localhost:3306/hibernate

【问题讨论】:

  • "按照教程中的详细说明将 applications.properties 放在 src/main/resources 中是行不通的。"没有帮助。该教程有效,因此您应该解释您所做的事情,以便我们了解出了什么问题。
  • 我已经编辑了我的问题以添加更多信息。总结主要区别是我的WebSecurityConfig 和我的 pom.xml。
  • 恐怕编辑没有帮助。您说您已使用教程中详述的“application.properties”,但这不起作用。我们仍然不知道您在 application.properties 中添加了什么。您还可以在调试模式 (--debug) 下运行您的应用程序以获取自动配置报告:这应该会告诉您为什么未配置 DataSource
  • 我使用了相同的 application.properties,只是我更改了 spring.datasource.url 以使其适应我的数据库。我将看看如何使用 eclipse 启用调试模式。

标签: hibernate maven spring-boot


【解决方案1】:

这是您需要在数据库配置 xml 中指定属性文件的方式。

<context:property-placeholder location="classpath:jdbc.properties" />

jdbc.properties 是放置在资源下的属性文件的名称。然后您可以访问如下属性:

  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"  destroy-method="close" p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.databaseurl}" p:username="${jdbc.username}" p:password="${jdbc.password}" />


 <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="entityInterceptor" ref="entityInterceptor"/>
    <property name="configLocation">
        <value>classpath:hibernate.cfg.xml</value>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${jdbc.dialect}</prop>
            <prop key="hibernate.show_sql">${jdbc.showsql}</prop>
            <prop key="hibernate.default_schema">${jdbc.schema}</prop>
        </props>
    </property>
</bean>

注意:我所有的属性名称都以 jdbc 开头。所以这就是为什么它们被称为 jdbc.name

【讨论】:

  • 感谢您的回答。 config.xml 在哪里?我在项目中的任何地方都找不到它。
  • 哦,您正在使用注释。您的属性文件与您提到的教程中显示的完全相同吗?
  • 是的,都是一样的,只是我把spring.datasource.url改成了指向我的数据库。