Java 在spring mv3应用程序中找不到hibernate.cfg.xml
我正在使用SpringMVC3和hibernate3。在我的应用程序中,我需要使用servlet。 在这个Servlet中,我必须调用我的DAO层。但当我在servlet中使用以下代码时Java 在spring mv3应用程序中找不到hibernate.cfg.xml,java,hibernate,spring-mvc,Java,Hibernate,Spring Mvc,我正在使用SpringMVC3和hibernate3。在我的应用程序中,我需要使用servlet。 在这个Servlet中,我必须调用我的DAO层。但当我在servlet中使用以下代码时 Session session = HibernateUtil.getSessionFactory().openSession(); public class HibernateUtil { private static final SessionFactory sessionFactory;
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文件夹中,以便在运行时自动获取它。但我在这里使用的是SpringMVC。
所以我有点困惑,请帮助我如何解决它
我使用控制器的所有其他地方工作正常
我正在carpool-servlet.xml中使用以下条目
下面是我的carpool-hibernate.xml
<?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>
公共域
common.businessclass.PostAdRR
下面是我的hibernate.cfg.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>
org.hibernate.dialogue.mysql5innodbdialogue
真的
现在如何设置hibernate config.xml
请帮助我建议将.cfg.xml移到类路径中。这意味着将它放在WEB-INF/类中,而不是WEB-INF中。在哪里设置hibernate.config
p:configLocation="${hibernate.config}"
如果您使用的是maven,您可以将该文件移动到src/main/resources,然后执行classpath:hibernate.config.xml,否则,如duffymo所述,将其移动到WEB-INF/classes下的classpath中。实际上,问题是我想在servlet中使用我的dao层,如果我们使用SpringMVC,dao层意味着daoservice在默认情况下是可访问的,因为这个服务是自动连接的,但是如果我想在我的servet中使用相同的服务,那不是控制器,那么我就无法配置。请提供帮助,以便您能够访问MVC控制器之外的服务?如果是这样的话,我会在一个不同的URL上创建一个单独的控制器(比如在本例中是/api之类的),它公开允许访问DAO的服务。这个控制器在操作上可以更加简洁,它的作用更像是一个web服务,而不是一个视图控制器,我假设您的原始控制器正在这样做。如果你想走另一条路,比如Jersey servlet之类的,你必须在上下文初始化时进行另一个包扫描。感谢dardo,我也在尝试同样的方法,但仍然不走运,现在我正在尝试如何将我的servlet用作控制器,如果我将我的servlet放入扫描包并放置所有注释,我正在努力,但它不起作用。运气不好:(我正在尝试您的建议,比如jersey servlet,但不清楚如何实现它,这意味着我们需要再次复制所有配置。您将什么类型的servlet绑定到扫描包。DispatcherServlet之类的spring servlet自然会寻找应用程序上下文,如果您只使用JavaEE servlet,就不会有任何问题。)弹簧容器已创建,包扫描将不起作用。