java – 使用ServletContainerInitializer时web.xml标签的等价物是什么?
作者:互联网
我正在尝试使用从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>
}
}
解决方法:
您可以使用以下内容手动将变量绑定到JNDI上下文
InitialContext context = new InitialContext(); //initialize a new JNDI context
context.addToEnvironment("logback/configuration-resource","logback.xml)"; //add a new named entry
标签:java,spring-mvc,spring,servlet-3-0,spring-java-config 来源: https://codeday.me/bug/20190628/1320994.html