带有 Spring 的 Tomcat

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4066971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 04:11:01  来源:igfitidea点击:

Tomcat with Spring

springtomcat

提问by Will Sumekar

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

我有一个托管在 Tomcat 中的 3 层应用程序;Web、服务和 DAO 层。

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

你如何集成Tomcat和Spring?我需要利用Spring的依赖注入、事务管理等。

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

我只能想到实例化 ClassPathXmlApplicationContext ,但这样 ApplicationContext 单例实例在层间不可见。

回答by amra

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

如果您正在创建 Web 应用程序,则不使用ClassPathXmlApplicationContext. 取而代之的是,您使用网络容器的功能。

You define application context in web.xml.

您在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.

有关详细信息,请查看文档方便的 Web 应用程序的 ApplicationContext 实例化

If a bean needs instance of application context, use ApplicationContextAwareinterface.

如果 bean 需要应用程序上下文的实例,请使用ApplicationContextAware接口。