【发布时间】: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