gpt4 book ai didi

spring - 如何加载applicationContext.xml

转载 作者:行者123 更新时间:2023-12-05 08:58:55 45 4
gpt4 key购买 nike

我尝试使用

从 web 应用程序中的 java 类加载 applicationContext.xml
  ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

ApplicationContext context = new FileSystemXmlApplicationContext("applicationContext.xml");

我的问题是如何从 java 类加载 applicationContext.xml。 applicationContext.xml 是 WEB-INF。这可能吗?

最佳答案

您可以在 web.xml 文件中使用 ContextLoaderListener:

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

然后你可以使用 WebApplicationContext 加载上下文:

WebApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(servlet.getServletContext());

希望这对您有所帮助。

关于spring - 如何加载applicationContext.xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19291797/

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