【发布时间】:2017-01-04 13:44:02
【问题描述】:
在 Bluemix 上运行时,如何在 MySQL 中为云创建数据源?如果有任何可用的 Java 配置示例,请分享。如何让 Hibernate 创建表?为什么会出现此错误?
创建名称为“entityManagerFactory”的bean时出错 com.covenant.app.config.root.DatabaseConfig:不满足的依赖关系 通过类型为索引 0 的构造函数参数表示 [org.springframework.jdbc.datasource.DriverManagerDataSource]::否 符合条件的 bean 类型 [org.springframework.jdbc.datasource.DriverManagerDataSource] 找到 对于依赖项:预计至少有 1 个符合自动装配条件的 bean 这种依赖的候选人。依赖注解:{};嵌套的 例外是 org.springframework.beans.factory.NoSuchBeanDefinitionException: 否 符合条件的 bean 类型 [org.springframework.jdbc.datasource.DriverManagerDataSource] 找到 对于依赖项:预计至少有 1 个符合自动装配条件的 bean 这种依赖的候选人。依赖注释:{}
我的数据库配置类
@Configuration
@Profile("cloud")
@EnableTransactionManagement
public class CloudDatabaseConfig extends AbstractCloudConfig {
@Bean
public DataSource inventoryDataSource() {
return connectionFactory().dataSource("mysql");
}
@Bean(name = "namingStrategy")
public ImprovedNamingStrategy getNamingStrategy(){
ImprovedNamingStrategy namingStrategy = new CDCustomNamingStrategy();
return namingStrategy;
}
@Bean(name="dataSource")
public BasicDataSource dataSource() throws PropertyVetoException {
BasicDataSource bean = new BasicDataSource();
bean.setDriverClassName("com.mysql.jdbc.Driver");
bean.setUrl("jdbc:mysql://localhost:3306/bluemix?useUnicode=true&characterEncoding=UTF-8");
bean.setUsername("root");
bean.setPassword("root");
return bean;
}
@Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, ImprovedNamingStrategy ins) {
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
entityManagerFactoryBean.setDataSource(dataSource);
entityManagerFactoryBean.setPackagesToScan(new String[]{"com.covenant.app.model"});
entityManagerFactoryBean.setLoadTimeWeaver(new InstrumentationLoadTimeWeaver());
entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
Map<String, Object> jpaProperties = new HashMap<String, Object>();
jpaProperties.put("database", "mysql");
jpaProperties.put("hibernate.hbm2ddl.auto", "update");
jpaProperties.put("hibernate.show_sql", "true");
jpaProperties.put("hibernate.format_sql", "true");
jpaProperties.put("hibernate.use_sql_comments", "true");
jpaProperties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
jpaProperties.put("hibernate.ejb.naming_strategy", ins);
entityManagerFactoryBean.setJpaPropertyMap(jpaProperties);
return entityManagerFactoryBean;
}
}
我在 Bluemix 上的 manifest.yml 文件:
---
applications:
- name: lordthankyou
path: target/ideals.war
services:
- mysql
env:
SPRING_PROFILES_ACTIVE: cloud
我收到以下错误:
创建名为“userService”的 bean 时出错:注入 autowired 依赖失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:不能 自动装配字段:私有 com.covenant.app.dao.UserRepository com.covenant.app.services.UserService.userRepository;嵌套异常 是 org.springframework.beans.factory.BeanCreationException: 错误 创建名为“userRepository”的bean:注入持久性 依赖失败;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException: 否 [javax.persistence.EntityManagerFactory] 类型的限定 bean 是 定义
【问题讨论】:
标签: mysql hibernate spring-mvc ibm-cloud spring-cloud