如何在 Bluemix 上为 MySQL 配置云数据源?
How to configure cloud datasource for MySQL on Bluemix?
当在 Bluemix 上 运行 时,如何在 MySQL 中为云创建数据源?如果有可用的 Java 配置示例,请分享。如何让 Hibernate 创建表以及为什么会出现此错误?
Error creating bean with name 'entityManagerFactory' defined in
com.covenant.app.config.root.DatabaseConfig: Unsatisfied dependency
expressed through constructor argument with index 0 of type
[org.springframework.jdbc.datasource.DriverManagerDataSource]: : No
qualifying bean of type
[org.springframework.jdbc.datasource.DriverManagerDataSource] found
for dependency: expected at least 1 bean which qualifies as autowire
candidate for this dependency. Dependency annotations: {}; nested
exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
qualifying bean of type
[org.springframework.jdbc.datasource.DriverManagerDataSource] found
for dependency: expected at least 1 bean which qualifies as autowire
candidate for this dependency. Dependency annotations: {}
我的数据库配置class
@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
我收到以下错误:
Error creating bean with name 'userService': Injection of autowired
dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Could not
autowire field: private com.covenant.app.dao.UserRepository
com.covenant.app.services.UserService.userRepository; nested exception
is org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'userRepository': Injection of persistence
dependencies failed; nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
qualifying bean of type [javax.persistence.EntityManagerFactory] is
defined
终于我开始工作了 我只是在 manifest.yml 中添加了一个环境变量来激活云配置文件并删除了 extends AbstractCloudConfig 因为它也在搜索 mongodb 。在这些更改之后它开始工作现在我可以 运行 spring mvc on blue mix。
当在 Bluemix 上 运行 时,如何在 MySQL 中为云创建数据源?如果有可用的 Java 配置示例,请分享。如何让 Hibernate 创建表以及为什么会出现此错误?
Error creating bean with name 'entityManagerFactory' defined in com.covenant.app.config.root.DatabaseConfig: Unsatisfied dependency expressed through constructor argument with index 0 of type [org.springframework.jdbc.datasource.DriverManagerDataSource]: : No qualifying bean of type [org.springframework.jdbc.datasource.DriverManagerDataSource] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.jdbc.datasource.DriverManagerDataSource] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
我的数据库配置class
@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
我收到以下错误:
Error creating bean with name 'userService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.covenant.app.dao.UserRepository com.covenant.app.services.UserService.userRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined
终于我开始工作了 我只是在 manifest.yml 中添加了一个环境变量来激活云配置文件并删除了 extends AbstractCloudConfig 因为它也在搜索 mongodb 。在这些更改之后它开始工作现在我可以 运行 spring mvc on blue mix。