【问题标题】:"Property 'sessionFactory' is required" in Spring?Spring中的“需要属性'sessionFactory'”?
【发布时间】:2011-10-30 08:58:07
【问题描述】:

我在 Spring 中遇到 sessionFactory 的问题。

public abstract class GenericDao<T,PK extends Serializable>
extends HibernateDaoSupport 
implements IGenericDao<T, Serializable> {

private Class<T> persistentClass;

public Class<T> getPersistentClass() {
    return persistentClass;
}

public void setPersistentClass(Class<T> persistentClass) {
    this.persistentClass = persistentClass;
}

@Repository
@Transactional
public class CategoryDao extends GenericDao<Category, Long> {
@Autowired
private SessionFactory sessionFactory;
public CategoryDao() {
    setPersistentClass(Category.class);
    setSessionFactory(sessionFactory);

}

我的 sessionFactoryBean 定义在 appContext.xml 文件中。

【问题讨论】:

    标签: hibernate spring sessionfactory


    【解决方案1】:

    @Autowired 在构造对象后起作用,因此您不能在构造函数中使用其结果。

    您可以使用this approachSessionFactory 自动连接到HibernateDaoSupport

    【讨论】:

      【解决方案2】:

      您的 appContext.xml 中需要这一行:

      <context:annotation-config/>
      

      这是一个完整的例子:http://blog.springsource.com/2007/05/14/annotation-driven-dependency-injection-in-spring-21/

      【讨论】:

        猜你喜欢
        • 2014-05-22
        • 1970-01-01
        • 2018-07-27
        • 2017-01-11
        • 1970-01-01
        • 2023-04-08
        • 1970-01-01
        • 2016-07-22
        • 1970-01-01
        相关资源
        最近更新 更多