有人可以帮助我将SessionFactory
自动装到Repository
的{{1}}课程中吗?以下代码给了我这个错误。任何克服这个问题的建议。我在StackOverflow中检查了许多类似的主题,但没有一个成功。
"使用名称' appointmentController创建bean时出错':不满意 通过字段表达的依赖关系iFactory':创建bean时出错 名称' appointmentFactory':表达不满意的依赖关系 通过现场'工厂':没有合格的bean类型 [org.hibernate.SessionFactory]找到依赖...等"
存储库类
Spring
控制器类
@Repository
@ComponentScan({"org.hibernate.SessionFactory"})
public class AppointmentFactory {
@Autowired
private SessionFactory factory ;
public SessionFactory getFactory() {
return factory;
}
public void setFactory(SessionFactory factory) {
this.factory = factory;
}
}
答案 0 :(得分:1)
尝试以下
@Autowired
private Environment env;
@Autowired
DataSource dataSource;
@Bean(name = { "sessionFactory" })
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
sessionFactory.setPackagesToScan(new String[] { "com.mypackage.my.entities" });
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
@Bean
public HibernateTransactionManager transactionManager() {
final HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(sessionFactory().getObject());
return txManager;
}
Properties hibernateProperties() {
return new Properties() {
{
// @formatter:off
setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
setProperty("hibernate.globally_quoted_identifiers", env.getProperty("hibernate.dialect"));
}
};
}
或
@Configuration
public class DataConfigTest {
@Autowired
EntityManagerFactory entityManagerFactory;
//@Autowired
//HibernateJpaSessionFactoryBean hibernateJpaSessionFactoryBean;
@Autowired
HibernateEntityManagerFactory hemf;
@Autowired
DataSource dataSource;
@Bean(name = { "sessionFactory" })
public SessionFactory sessionFactory() {
return hemf.getSessionFactory();
// HibernateJpaSessionFactoryBean factory = new
// HibernateJpaSessionFactoryBean();
// factory.setEntityManagerFactory(entityManagerFactory);
// return factory.getObject();
}
@Bean
public HibernateTransactionManager transactionManager() {
final HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(sessionFactory());
return txManager;
}
}
答案 1 :(得分:1)
我认为在构造函数中,但我目前也在为此苦苦挣扎
public AppointmentFactory(@Qualifier("mySessionFactory") SessionFactory sessionFactory){
super.setSessionFactory(sessionFactory);
}