1

我要开发一个Web应用程序,我想使用hibernate + spring。在独立应用程序中,为了初始化 hibernate 或 spring,我们将调用工厂对象等。我的意思是创建第一个 spring 或 hibernate bean 是主要功能。

但是在基于jsp+servlet和其他bean的web应用程序中,我不知道谁负责准备spring或hibernate。解决办法是什么?

4

3 回答 3

1

我认为上下文侦听器。在 web.xml 中

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
于 2012-04-23T09:15:58.733 回答
0

If you're developing an application based on JSP + servlets (does not matter if you actually use Spring MVC), you should configure a dataSource bean and a SessionFactory bean in your Spring configuration file. Then, from within your application, you could autowire SessionFactory where it is needed, and use it to open new sessions.

于 2012-04-23T09:18:20.227 回答
0

在一般的 web 应用程序中,servlet容器将加载一个名为 web.xml 的文件,您可以在其中启动 web 应用程序所需的配置文件,在您的情况下,spring 和 hibernate 配置文件位于context-paramlistener部分。
您应该在 Spring 配置文件中配置dataSourceSessionFactorybeans。
然后,从您的 java 类中,您应该autowire在需要它们的地方使用 bean。

于 2012-04-23T09:26:22.447 回答