Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tomcat with Spring

Tags:

spring

tomcat

I have a 3-tier application hosted in Tomcat; web, service and DAO layers.

How do you integrate Tomcat and Spring? I need to make use of Spring's dependency injection, transaction management, etc.

I can only think of instantiating a ClassPathXmlApplicationContext but this way the ApplicationContext singleton instance is not visible across layers.

like image 943
Will Sumekar Avatar asked Nov 01 '10 06:11

Will Sumekar


1 Answers

If you are creating web application you don't use ClassPathXmlApplicationContext. Instead of that you use features of web-container.

You define application context in web.xml.

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
</context-param>

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

For details take a look into documentation Convenient ApplicationContext instantiation for web applications.

If a bean needs instance of application context, use ApplicationContextAware interface.

like image 128
amra Avatar answered Sep 27 '22 23:09

amra