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.
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.
精彩评论