我有一个问题是hibernate的配置类,我正在尝试为两个数据库mysql创建连接器。例如,如果我创建了一个实例数据源 - 一切都很好,但在追加另一个实例数据源之后:
说明
方法sessionFactory的参数0 pl.lasota.databaseaccess.ConfigBeanForDatabase需要一个bean, 但发现了2个: - dataSource:由类路径资源[pl / lasota / databaseaccess / ConfigBeanForDatabase.class]中的方法'dataSource'定义 - dataSourceUser:由类路径资源[pl / lasota / databaseaccess / ConfigBeanForDatabase.class]中的方法'dataSourceUser'定义
动作:
考虑将其中一个bean标记为@Primary,更新消费者 接受多个bean,或使用@Qualifier来识别bean 应该消费
豆类标注注释资格。我不明白为什么这不能正常工作。
这是我的配置类:
@Configuration
public class ConfigBeanForDatabase {
@Bean(name = "dataSource")
public DataSource dataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/mysql1");
dataSource.setUsername("****");
dataSource.setPassword("****");
return dataSource;
}
@Autowired
@Bean(name = "sessionFactory")
public SessionFactory sessionFactory(
@Qualifier("dataSource") DataSource dataSource) {
LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);
sessionBuilder.addProperties(getHibernateProperties());
sessionBuilder.addAnnotatedClasses(Comment.class);
return sessionBuilder.buildSessionFactory();
}
@Autowired
@Bean
public HibernateTransactionManager transactionManager(
@Qualifier("sessionFactory") SessionFactory sessionFactory) {
HibernateTransactionManager transactionManager = new HibernateTransactionManager(
sessionFactory);
return transactionManager;
}
@Autowired
@Bean
public MysqlPlaceDaoImpl mysqlPlaceDaoImpl(@Qualifier("sessionFactory") SessionFactory sessionFactory) {
return new MysqlPlaceDaoImpl(sessionFactory);
}
@Autowired
@Bean
public MysqlSiteDaoImpl mysqlSiteDaoImpl(@Qualifier("sessionFactory") SessionFactory sessionFactory) {
return new MysqlSiteDaoImpl(sessionFactory);
}
@Bean(name = "dataSourceUser")
public DataSource dataSourceUser() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/mysql2");
dataSource.setUsername("****");
dataSource.setPassword("****");
return dataSource;
}
@Autowired
@Bean(name = "sessionFactoryUser")
public SessionFactory sessionFactoryUser(@Qualifier("dataSourceUser") DataSource dataSource) {
LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);
sessionBuilder.addProperties(getHibernateProperties());
sessionBuilder.addAnnotatedClasses(User.class);
return sessionBuilder.buildSessionFactory();
}
@Autowired
@Bean
public HibernateTransactionManager transactionManagerUser(
@Qualifier("sessionFactoryUser") SessionFactory sessionFactory) {
HibernateTransactionManager transactionManager = new HibernateTransactionManager(
sessionFactory);
return transactionManager;
}
@Autowired
@Bean
public MysqlUserDaoImpl mysqlUserDaoImpl(@Qualifier("sessionFactoryUser") SessionFactory sessionFactory) {
return new MysqlUserDaoImpl(sessionFactory);
}
private Properties getHibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.show_sql", "true");
properties.put(" hibernate.cache.provider_configuration_file_resource_path", "ehcache.xml");
properties.put("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
properties.put("hibernate.hbm2ddl.auto", "update");
properties.put("hibernate.connection.useUnicode", true);
properties.put("hibernate.connection.characterEncoding", "UTF-8");
properties.put("hibernate.cache.use_second_level_cache", true);
properties.put("hibernate.cache.region.factory_class", "org.hibernate.cache.ehcache.EhCacheRegionFactory");
return properties;
}
}
我尝试添加注释@primary,也将类与两个类的配置分开。抱歉我的英文。