带有 Spring 的 Tomcat [英] Tomcat with Spring

查看:27
本文介绍了带有 Spring 的 Tomcat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

Tomcat 和 Spring 是如何集成的?需要用到Spring的依赖注入、事务管理等

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

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

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

推荐答案

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

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

您在 web.xml 中定义应用程序上下文.

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>

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

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

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

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

这篇关于带有 Spring 的 Tomcat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆