2016-12-31 203 views
0

我一直在關注這個tutorial來實現Hibernate到我的應用程序中。但是,我不知道如何閱讀其中提到的application.properties。我也讀過doc,提到:如何使用application.properties配置spring boot來設置hibernate的配置

  • 當前目錄的A/config子目錄。
  • 當前目錄
  • 類路徑/配置包
  • 類路徑根

但是,什麼是當前目錄春季啓動?

我也是而不是使用以下依賴項(比較教程),我的問題可以通過使用它們解決嗎?這是造成依賴衝突:

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

此外,乾脆把放在src /主/資源applications.properties作爲教程不起作用詳細說明。 如果我手動設置如下,它工作(這是我使用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。我剛剛將所有內容都重新命名爲'測試',例如TestDAO.java

編輯2:我application.properties文件是相同的一個教程,除了以下行:

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

+0

「簡單地將applications.properties放入src/main/resources中,詳情請參閱教程不起作用。」沒有幫助。該教程的作品,所以你應該解釋你做了什麼,以便我們明白什麼錯了。 –

+0

我編輯了我的問題以添加更多信息。總結主要的區別是我的'WebSecurityConfig'和我的pom.xml。 – Chuck

+0

編輯沒有幫助,我很害怕。你說的是你已經使用了「application.properties」,如教程中詳細描述的那樣,但這是行不通的。我們仍然不知道你在application.properties中放置了什麼。你也可以在調試模式下運行你的應用程序('--debug')來獲得自動配置報告:這應該告訴你爲什麼'DataSource'沒有配置。 –

回答

0

這就是你需要指定數據庫配置特性文件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

+0

感謝您的回答。 config.xml在哪裏?我無法在我的項目中找到它。 – Chuck

+0

Ohkay,你正在使用註釋。您的財產文件是否與您提到的教程中顯示的完全相同? –

+0

是的,它是一樣的,除了我改變'spring.datasource.url'指向我的分貝。 – Chuck

相關問題