我一直在关注此tutorial以将Hibernate实现到我的应用程序中。但是,我不知道如何阅读其中提到的application.properties。我也读了doc,提到了:
但春天启动的当前目录是什么?
我也不使用以下依赖项(参见教程),使用它们可以解决我的问题吗?它导致依赖冲突:
<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>
另外,简单地将application.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。我刚将所有内容重命名为&#39;测试&#39;,例如 TestDAO.java 。
编辑2:我的 application.properties 文件与教程中的相同,但以下行除外:
spring.datasource.url = jdbc:mysql://localhost:3306/hibernate
答案 0 :(得分: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