简体   繁体   English

在Web应用程序中初始化spring和hibernate

[英]initializing spring and hibernate in a web application

i`m going to develop a web application and I wanted to use hibernate + spring. 我要开发一个Web应用程序,我想使用Hibernate + Spring。 in a standalone application, for initializing hibernate or spring we will call the factory object or etc. i mean it is the main function responsibility to create first spring or hibernate beans. 在独立应用程序中,为了初始化休眠或弹簧,我们将调用工厂对象等。我的意思是创建第一个spring或休眠bean是主要功能。

but in a web application which is based on jsp+servlets and other beans, i don`t know who is responsible to prepare spring or hibernate. 但是在基于jsp + servlet和其他bean的Web应用程序中,我不知道谁负责准备spring或hibernate。 what is the solution? 解决办法是什么?

I think context listener. 我认为上下文监听器。 In web.xml 在web.xml中

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

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. 如果您正在开发基于JSP + servlet的应用程序(实际上是否使用Spring MVC都没有关系),则应在Spring配置文件中配置dataSource bean和SessionFactory bean。 Then, from within your application, you could autowire SessionFactory where it is needed, and use it to open new sessions. 然后,可以在应用程序内部将SessionFactory自动连接到需要的地方,并使用它来打开新的会话。

In web applications in general, the servlet container will load a file named web.xml where you can initiate the configuration files that your web-app needs, in your case spring and hibernate config files in the context-param and listener sections. 通常,在Web应用程序中, servlet容器将加载一个名为web.xml的文件,您可以在其中启动Web应用程序所需的配置文件,在context-paramlistener部分中,可以使用spring和hibernate配置文件。
You should configure dataSource and SessionFactory beans in your Spring configuration file. 您应该在Spring配置文件中配置dataSourceSessionFactory bean。
Then, from your java classes, you should autowire the beans where they are needed. 然后,从您的Java类中,应该在需要它们的地方autowire Bean。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM