2
我想使用2個休眠數據庫連接。我面臨這個錯誤,我試圖解決,但無法找到一個方法。我製作了兩個不同的配置文件,並註釋了@bean和@qualifires,但它仍然很有用。使用2休眠會話函數的Spring配置
錯誤
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'transactionManager' defined in class path resource [com/project/configuration/RepositoryConfiguration.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [org.hibernate.SessionFactory]: : No qualifying bean of type [org.hibernate.SessionFactory] is defined: expected single matching bean but found 2: sessionFactory2,sessionFactory; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.hibernate.SessionFactory] is defined: expected single matching bean but found 2: sessionFactory2,sessionFactory
我的配置:
@Configuration
@EnableTransactionManagement
@PropertySource({ "classpath:hibernate.properties" })
public class RepositoryConfig2 {
@Autowired
private Environment environment;
@Bean(name="sessionFactory2")
public LocalSessionFactoryBean sessionFactory2() {
LocalSessionFactoryBean sessionFactory2 = new LocalSessionFactoryBean();
sessionFactory2.setDataSource(dataSource2());
sessionFactory2.setHibernateProperties(hibernateProperties());
sessionFactory2.setPackagesToScan(new String[] { "com.project" });
return sessionFactory2;
}
@Bean
public Properties hibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect2"));
properties.put("hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql2"));
properties.put("hibernate.hbm2ddl.auto", "update");// environment.getRequiredProperty("hibernate.hbm2ddl.auto")
return properties;
}
@Bean(name = "datasource2")
public DataSource dataSource2() {
DriverManagerDataSource dataSource2 = new DriverManagerDataSource();
dataSource2.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName2"));
dataSource2.setUrl(environment.getRequiredProperty("jdbc.url2"));
dataSource2.setUsername(environment.getRequiredProperty("jdbc.username2"));
dataSource2.setPassword(environment.getRequiredProperty("jdbc.password2"));
return dataSource2;
}
@Autowired
@Qualifier(value = "sessionFactory2")
public HibernateTransactionManager transactionManager(SessionFactory s) {
HibernateTransactionManager txManager2 = new HibernateTransactionManager();
txManager2.setSessionFactory(s);
return txManager2;
}
}
二配置:
@Configuration
@EnableTransactionManagement
@PropertySource({ "classpath:hibernate.properties" })
public class RepositoryConfiguration {
@Autowired
private Environment environment;
@Bean(name="sessionFactory")
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setHibernateProperties(hibernateProperties());
sessionFactory.setPackagesToScan(new String[] { "com.project" });
return sessionFactory;
}
@Bean
public Properties hibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect"));
properties.put("hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql"));
properties.put("hibernate.hbm2ddl.auto", "update");// environment.getRequiredProperty("hibernate.hbm2ddl.auto")
return properties;
}
@Bean(name = "datasource")
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName"));
dataSource.setUrl(environment.getRequiredProperty("jdbc.url"));
dataSource.setUsername(environment.getRequiredProperty("jdbc.username"));
dataSource.setPassword(environment.getRequiredProperty("jdbc.password"));
return dataSource;
}
@Bean
@Autowired
@Qualifier(value = "sessionFactory")
public HibernateTransactionManager transactionManager(SessionFactory s) {
HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(s);
return txManager;
}
}
爲什麼'@ Bean'和'@ Autowired'上同樣的方法? –
是的,早就意識到了。感謝問題解決的方式(大量的研發) – Kharoud