作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有人可以帮我将 SessionFactory
Autowiring 到 Spring
中的 Repository
类吗?以下代码给了我这个错误。任何解决这个问题的建议。我在 StackOverflow 上查了很多类似的主题,但没有一个成功。
"Error creating bean with name 'appointmentController': Unsatisfied dependency expressed through field 'iFactory': Error creating bean with name 'appointmentFactory': Unsatisfied dependency expressed through field 'factory': No qualifying bean of type [org.hibernate.SessionFactory] found for dependency ...etc"
存储库类
@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;
}
}
Controller 类
@RestController
@ComponentScan({"com.mobios.ep.services","com.ombios.ep.entity.factory")
public class AppointmentController {
@Autowired
private AppointmentService iService;
@Autowired
private AppointmentFactory iFactory;
@RequestMapping(value="appointment/get", method=RequestMethod.POST)
public AppoinmentWM getApointmentById(@RequestBody AppointmentReq appointment) throws Exception{
Log4JUtil.logger.info("APPOINTMENT,appointment_get_request,Request="+appointment.toString());
AppointmentService appoinmentService = new AppointmentService();
StatsService statsService = new StatsService();
Mapper mapper = new Mapper();
AppoinmentWM gotAppointment = null;
}
最佳答案
尝试以下操作
@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;
}
}
关于java - 如何在 Spring 中将 SessionFactory Autowiring 到 Repository 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47694766/
我是一名优秀的程序员,十分优秀!