我正在将我的 Spring MVC 项目迁移到基于代码的配置,但不确定如何添加侦听器,
这是我的 web.xml 中的内容
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>WEB-INF/spring/log4j.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
这是我的初始化类:
@Order(1)
public class Initializer extends
AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] { RootConfig.class, SecurityConfig.class };
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] { WebConfig.class };
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}
我检查了 的声明AbstractAnnotationConfigDispatcherServletInitializer
,并尝试onStartUp()
像下面那样实现,但它抱怨addListener
没有找到。
@Override
public void onStartup(ServletContext container) throws ServletException {
org.springframework.web.util.Log4jConfigListener log4jConfigListener = new org.springframework.web.util.Log4jConfigListener();
container.addListener(log4jConfigListener);
super.onStartup(container);
}