Spring WebApplicationInitializer不是';检测不到
在我的小型Spring MVC项目中,我可以使用web.xml配置dispatcher:Spring WebApplicationInitializer不是';检测不到,spring,spring-mvc,servlet-3.0,Spring,Spring Mvc,Servlet 3.0,在我的小型Spring MVC项目中,我可以使用web.xml配置dispatcher: <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>myweb.ApplicationConfig</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
不幸的是,上面的代码不会被调用,我也不知道为什么。有人能帮我一下原因是什么
环境:JBossEAP6.4,spring位于EAR中WAR的lib文件夹中
如果我是对的,它应该是这样工作的:
Spring中的SpringServletContainerInitializer类实现了ServletContainerInitializer,并具有注释@HandleTypes(WebApplicationInitializer.class)。在spring-web.jar的META-INF/services文件夹中,声明这个类是一个服务,应该调用它。使用类作为参数调用该类是Servlet 3.0的一个特性,它实现了WebApplicationInitializer接口。问题是,SpringServletContainerInitializer没有被web容器调用,我不知道为什么。是否有任何错误?。您缺少容器搜索bean所需的组件扫描。您有错误“javax.servlet.ServletContainerInitializer:Provider org.springframework.web.SpringServletContainerInitializer不是子类型”,如果您想看到错误,您已将模块org.springframework.spring放入jboss中否,没有错误,未调用onStartup。组件扫描在扩展WebMVCConfigureAdapter的另一个类中声明,配置和组件扫描可以工作。查看我修改过的帖子,在那里我写下了我认为它应该如何工作。
@Configuration
@EnableWebMvc
public class ApplicationInitializer implements WebApplicationInitializer {
protected Logger logger = Logger.getLogger(ApplicationInitializer.class);
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
logger.info("Application initialization ...");
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.setConfigLocation("myweb");
servletContext.addListener(new ContextLoaderListener(context));
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("DispatcherServlet", new DispatcherServlet(context));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
logger.info("Application initialized");
}
}