Will Sumekar
Will Sumekar

Reputation: 1269

Tomcat with Spring

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.

Upvotes: 6

Views: 29779

Answers (1)

amra
amra

Reputation: 16935

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.

Upvotes: 19

Related Questions