Tomcat with Spring
问题描述:
我有一个Tomcat托管的3层appln;网页,服务和DAO图层。Tomcat with Spring
你如何整合Tomcat和Spring?我需要利用Spring的依赖注入,事务管理等。
我只能想到实例化一个ClassPathXmlApplicationContext,但这种方式ApplicationContext单例实例在层间不可见。
在此先感谢。
问候
将
答
如果创建Web应用程序不使用ClassPathXmlApplicationContext
。而不是你使用web容器的功能。
您可以在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>
有关详细信息,请参阅文档Convenient ApplicationContext instantiation for web applications。
如果一个bean需要应用程序上下文的实例,请使用ApplicationContextAware
接口。