gpt4 book ai didi

java - SPRING MVC - IllegalArgumentException : 'sessionFactory' or 'hibernateTemplate' is required

转载 作者:行者123 更新时间:2023-12-01 10:06:54 30 4
gpt4 key购买 nike

我有这个 Maven 项目及其模块

父级

|_____型号

|_____坚持

     |_ persistence-context.xml

|_____服务

     |_ service-context.xml

|_____查看

     |_ spring/app-config.xml

在 persistence-context.xml 中有以下内容:

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ” 默认自动线=“byName”>

<tx:annotation-driven transaction-manager="persistence.transactionManager"  proxy-target-class="true" />

<bean id="persistence.propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:META-INF/jdbc.properties</value>
<value>classpath*:META-INF/hibernate.properties</value>
</list>
</property>
</bean>

<bean id="persistence.transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="persistence.sessionFactory" />
<property name="jdbcExceptionTranslator" ref="persistence.jdbcExceptionTranslator" />
</bean>

<bean name="persistence.jdbcExceptionTranslator" class="org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator">
<constructor-arg>
<ref bean="persistence.dataSource" />
</constructor-arg>
</bean>

<bean id="persistence.dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.db.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="defaultAutoCommit" value="false" />
<property name="poolPreparedStatements" value="true" />
<property name="initialSize" value="20" />
<property name="maxActive" value="30" />
<property name="maxIdle" value="25" />
</bean>

<bean id="persistence.sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="persistence.dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
<prop key="hibernate.connection.autocommit">false</prop>
<prop key="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</prop>
</props>
</property>
<property name="mappingLocations">
<list>
<value>classpath:mappings/items/servicio.hbm.xml</value>
<value>classpath:mappings/items/stockable.hbm.xml</value>
<value>classpath:mappings/items/bigstockable.hbm.xml</value>
</list>
</property>
</bean>

<!-- Daos beans -->

<bean id="servicioDao" class="daos.ServicioDao" >
<property name="sessionFactory" ref="persistence.sessionFactory" />
</bean>
<bean id="stockableDao" class="daos.StockableDao" >
<property name="sessionFactory" ref="persistence.sessionFactory" />
</bean>
<bean id="bigStockableDao" class="daos.BigStockableDao" >
<property name="sessionFactory" ref="persistence.sessionFactory" />
</bean>

在该xml中,我用它的sessionFactory制作了我的daos,但是当我启动该项目时,我得到了java.lang.IllegalArgumentException:需要'sessionFactory'或'hibernateTemplate',因为我的hibernateTemplate为空。我的 daos 扩展自 HibernateDaoSupport,我知道如果你给你的 dao 一个 sessionFactory,它会自动创建一个 hibernateTemplate,并且不知道会发生什么。

我的 daos 有一个 @Repository(例如 @Repository(value="servicioDao"))

以及 @Service 和 setter 中的 @Autowired 服务

我将它们添加到上下文中

   <context:component-scan base-package="controllers" />
<context:component-scan base-package="servicios" />
<context:component-scan base-package="daos" />

我只是将其添加到 persistence-context.xml

<!-- Daos beans -->

<bean id="servicioDao" class="daos.ServicioDao" >
<property name="sessionFactory" ref="persistence.sessionFactory" />
<property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>
<bean id="stockableDao" class="daos.StockableDao" >
<property name="sessionFactory" ref="persistence.sessionFactory" />
<property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>
<bean id="bigStockableDao" class="daos.BigStockableDao" >
<property name="sessionFactory" ref="persistence.sessionFactory" />
<property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>

我也遇到同样的错误。

我的一些 daos 代码:

@Repository(value="servicioDao")
@SuppressWarnings("serial")
public class ServicioDao extends GenericHome<Servicio>{
public ServicioDao(){}
@Override
protected Class<Servicio> getDomainClass() {
return Servicio.class;
}
}
public abstract class GenericHome<T> extends HibernateDaoSupport implements Serializable{
protected Class<T> persistentClass = this.getDomainClass();
protected abstract Class<T> getDomainClass();
}
public class ServicioService {
private ServicioDao servicioDao;
public ServicioService(){}
public ServicioDao getServicioDao() {
return servicioDao;
}
@Autowired
public void setServicioDao(ServicioDao servicioDao) {
this.servicioDao = servicioDao;
}
}

最佳答案

我注意到,当我使用@Service和@Repository时,bean不是由xml创建的,所以当它给我错误“需要'sessionFactory'或'hibernateTemplate'”时,是因为dao已创建但从未填充它sessionFactory,因此为了使用我的 xml 文件,我像普通 bean 一样创建了 Controller

关于java - SPRING MVC - IllegalArgumentException : 'sessionFactory' or 'hibernateTemplate' is required,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36379633/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com