我正在使用spring mvc3和hibernate3。在我的應用程序中,我需要使用一個servlet。 在那個servlet中我必須調用我的DAO層。但是當我在我的servlet中使用以下代碼時。在Spring mv3應用程序中找不到hibernate.cfg.xml
Session session = HibernateUtil.getSessionFactory().openSession();
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
sessionFactory = new Configuration().configure()
.buildSessionFactory();
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
我收到錯誤org.hibernate.HibernateException:找不到hibernate.cfg.xml。
目前,我的xml文件與其他配置文件一起位於WEB-INF文件夾中。
所有的解決方案都說我需要將它保存在src文件夾中,以便自動在運行時提取它。但我在這裏使用spring mvc。 所以我有點困惑,請幫助我如何解決它。
我使用的所有其他地方控制器工作正常。
我正在使用拼車-servlet.xml中
以下下面的條目是我的拼車,hibernate.xml以下是我hibernate.cfg
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">
<context:property-placeholder location="/WEB-INF/dbproperties.properties" />
<!-- Enable annotation style of managing transactions -->
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
p:dataSource-ref="dataSource"
p:configLocation="${hibernate.config}"
p:packagesToScan="store.custom.controllers">
<property name="annotatedClasses">
<list>
<value>common.domain.Ride</value>
<value>common.businessclass.PostAdRR</value>
</list>
</property>
</bean>
<!-- Declare a datasource that has pooling capabilities-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close"
p:driverClass="${jdbc.driverClassName}"
p:jdbcUrl="${jdbc.url}"
p:user="${jdbc.username}"
p:password="${jdbc.password}"
p:acquireIncrement="5"
p:idleConnectionTestPeriod="60"
p:maxPoolSize="100"
p:maxStatements="50"
p:minPoolSize="10" />
<!-- Declare a transaction manager-->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory" />
</beans>
。 xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- We're using MySQL database so the dialect needs to MySQL as well-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<!-- Enable this to see the SQL statements in the logs-->
<property name="show_sql">true</property>
<!-- This will drop our existing database and re-create a new one.
Existing data will be deleted! -->
<!-- <property name="hbm2ddl.auto">create</property>-->
</session-factory>
</hibernate-configuration>
現在怎麼設置我的休眠config.xml中 請幫
其實問題是我想在我的servlet中使用我的dao圖層,並且如果我們使用spring mvc,dao層meand daoservice將默認可訪問,因爲此服務是自動裝配的,但如果我想在我的用戶中使用相同的服務器servet,那不是控制器,那麼我無法配置。請幫助 – Pedantic
因此,您希望能夠訪問MVC控制器之外的服務?如果是這種情況,我會在另一個URL(比如說在這種情況下稱爲/ api)上創建一個單獨的控制器,從而公開允許訪問DAO的服務。這個控制器在操作上可以更簡潔,並且比我假設您的原始控制器正在執行的視圖控制器更像Web服務。如果你想要走另外一條路線,比如有一個Jersey servlet或者其他東西,那麼當上下文被初始化時,你必須做另一個包掃描。 – dardo
謝謝dardo,我想同樣的,仍然不是運氣,現在我試圖如果任何如何我可以使用我的servlet作爲控制器,如果我將我的servlet放在掃描包中,並把所有的註釋,我想但它沒有加工。沒有運氣:( – Pedantic