gpt4 book ai didi

java - 使用 ServletContainerInitializer 时,web.xml 的 标记的等效项是什么?

转载 作者:搜寻专家 更新时间:2023-11-01 03:05:17 24 4
gpt4 key购买 nike

我正在尝试用从 Spring 的 WebApplicationInitializer 扩展的基于代码的类替换我的 web.xml 文件。我的 web.xml 文件有几个“env-entry”元素。我试图找出如何在我的 WebApplicationInitializer 类中设置这些,但没有运气。也许有人知道这些标签的等效代码?

public class MyWebApplicationInitializer implements WebApplicationInitializer {

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
XmlWebApplicationContext appContext = new XmlWebApplicationContext();
appContext.setConfigLocation("WEB-INF/springmvc-servlet.xml");

Dynamic servlet = servletContext.addServlet("springmvc", new DispatcherServlet(appContext));
servlet.setLoadOnStartup(1);
servlet.addMapping("/*");

//How do I add this?
// <env-entry>
// <env-entry-name>logback/configuration-resource</env-entry-name>
// <env-entry-type>java.lang.String</env-entry-type>
// <env-entry-value>logback.xml</env-entry-value>
// </env-entry>
}
}

最佳答案

<env-entry>本质上只是声明了一个 Web 应用程序上下文属性,您可以使用 ServletContext#setAttribute() 来绑定(bind)自己。你已经在那里了

   servletContext.setAttribute("logback/configuration-resource", "logback.xml");

关于java - 使用 ServletContainerInitializer 时,web.xml 的 <env-entry> 标记的等效项是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24872350/

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