1
votes

i`m going to develop a web application and I wanted to use 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.

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. what is the solution?

3

3 Answers

1
votes

I think context listener. In web.xml

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

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.

0
votes

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.
You should configure dataSource and SessionFactory beans in your Spring configuration file.
Then, from your java classes, you should autowire the beans where they are needed.