如何在Bluemix上为MySQL配置云数据源?

时间:2016-08-28 11:29:37

标签: mysql hibernate spring-mvc ibm-cloud spring-cloud

在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有资格作为autowire   这种依赖的候选人。依赖注释:{};嵌套   例外是   org.springframework.beans.factory.NoSuchBeanDefinitionException:没有   合格的bean类型   找到[org.springframework.jdbc.datasource.DriverManagerDataSource]   对于依赖:预计至少有1个bean有资格作为autowire   这种依赖的候选人。依赖注释:{}

我的数据库配置类

@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时出错:注入自动装配   依赖失败;嵌套异常是   org.springframework.beans.factory.BeanCreationException:不能   autowire字段:private 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是   定义

1 个答案:

答案 0 :(得分:0)

最后我得到它工作我刚刚添加了一个环境变量来激活manifest.yml中的云配置文件并删除了扩展AbstractCloudConfig,因为它也在搜索mongodb。这些更改后它开始工作,现在我可以运行spring mvc on blue mix