gpt4 book ai didi

spring - 带 Spring 的 Tomcat

转载 作者:行者123 更新时间:2023-11-28 21:46:49 26 4
gpt4 key购买 nike

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

如何整合Tomcat和Spring?我需要利用Spring的依赖注入(inject)、事务管理等。

我只能想到实例化一个 ClassPathXmlApplicationContext,但这样 ApplicationContext 单例实例在各层之间是不可见的。

最佳答案

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

您在 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 接口(interface)。

关于spring - 带 Spring 的 Tomcat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4066971/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com