带Java注释的Spring MVC多servlet
当尝试设置多个servlet并从基于XML的配置切换到Java注释时,带Java注释的Spring MVC多servlet,java,spring-mvc,servlets,spring-annotations,servlet-3.0,Java,Spring Mvc,Servlets,Spring Annotations,Servlet 3.0,当尝试设置多个servlet并从基于XML的配置切换到Java注释时,@EnableWebMvc注释与 我已经将xml配置转换为Java注释类。但是,当在我的WebApplicationInitializer中注册java类时,XML配置会为每个servlet很好地加载我的控制器,但是当使用java注释时,我的控制器上会出现404。如果我只在注释配置中注册了一个servlet,它可以正常工作,但两个或更多的servlet会导致404 web-servlet.xml <beans...>
@EnableWebMvc
注释与
我已经将xml配置转换为Java注释类。但是,当在我的WebApplicationInitializer中注册java类时,XML配置会为每个servlet很好地加载我的控制器,但是当使用java注释时,我的控制器上会出现404。如果我只在注释配置中注册了一个servlet,它可以正常工作,但两个或更多的servlet会导致404
web-servlet.xml
<beans...>
<context:component-scan base-package="com.foo.bar" use-default-filters="false">
<context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
<context:exclude-filter type="aspectj" expression="com.foo.bar.server.external.*"/>
</context:component-scan>
<security:global-method-security proxy-target-class="true" secured-annotations="enabled" pre-post-annotations="enabled"/>
<mvc:annotation-driven/>
<mvc:resources location="/, classpath:/META-INF/web-resources/" mapping="/resources/**"/>
<mvc:default-servlet-handler/>
<mvc:interceptors>
...
</mvc:interceptors>
<!-- selects a static view for rendering without the need for an explicit controller -->
<mvc:view-controller path="/login"/>
<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="ignoreAcceptHeader" value="true"/>
<property name="mediaTypes">
<map>
<entry key="json" value="application/json"/>
</map>
</property>
</bean>
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="contentNegotiationManager" ref="contentNegotiationManager"/>
<property name="defaultViews">
<list>
<bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" />
</list>
</property>
<property name="order" value="1" />
</bean>
</beans>
webAppInitializer.java
public class FooWebAppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
servletContext.setInitParameter("defaultHtmlEscape", "true");
servletContext.setInitParameter("spring.profiles.default", "dev");
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
rootContext.setDisplayName("Foo Bar");
rootContext.register(FooApplication.class);
rootContext.setServletContext(servletContext);
createFilters(servletContext);
servletContext.addListener(new Log4jConfigListener());
servletContext.addListener(new ContextLoaderListener(rootContext));
// THIS WORKS
// XmlWebApplicationContext webContext = new XmlWebApplicationContext();
// webContext.setConfigLocation("/WEB-INF/spring/web-servlet.xml");
// THIS DOESN'T WHEN 2 SERVLETS ARE REGISTERED
AnnotationConfigWebApplicationContext webContext = new AnnotationConfigWebApplicationContext();
webContext.register(WebServlet.class);
ServletRegistration.Dynamic webServlet = servletContext.addServlet("web", new DispatcherServlet(webContext));
webServlet.setLoadOnStartup(1);
webServlet.addMapping( "/*");
// THIS WORKS
// XmlWebApplicationContext extContext = new XmlWebApplicationContext();
// extContext.setConfigLocation("/WEB-INF/spring/ext-servlet.xml");
// THIS DOESN'T WHEN 2 SERVLETS ARE REGISTERED
AnnotationConfigWebApplicationContext extContext = new AnnotationConfigWebApplicationContext();
extContext.register(ExtServlet.class);
ServletRegistration.Dynamic extServlet = servletContext.addServlet("ext",
new DispatcherServlet(extContext));
extServlet.setLoadOnStartup(2);
extServlet.addMapping("/ext/*");
}
}
我希望注释配置的作用相同,但不确定潜在的区别是什么。关于在两个servlet上都有两个具有
@EnableWebMvc
的servlet,没有太多信息,并且当不使用XML配置时,控制器不应该返回404;在发布的代码中,第二个DispatcherServlet将重写第一个;请查看更多详细信息。这可能有点晚了;在发布的代码中,第二个DispatcherServlet将重写第一个;详情请参阅。
<beans...>
<context:component-scan base-package="com.foo.bar.server.external" use-default-filters="false">
<context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>
<mvc:annotation-driven/>
<mvc:interceptors>
<bean class="com.foo.bar.server.external.CheckExtInterceptor"/>
</mvc:interceptors>
</beans>
@Configuration
@ComponentScan(basePackages = "com.foo.bar.server.external",
useDefaultFilters = false,
includeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION, value = Controller.class))
@EnableWebMvc
public class ExtServlet extends WebMvcConfigurerAdapter {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(checkExtInterceptor());
}
@Bean
public checkExtInterceptor checkExtInterceptor() {
return new CheckExtInterceptor();
}
}
public class FooWebAppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
servletContext.setInitParameter("defaultHtmlEscape", "true");
servletContext.setInitParameter("spring.profiles.default", "dev");
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
rootContext.setDisplayName("Foo Bar");
rootContext.register(FooApplication.class);
rootContext.setServletContext(servletContext);
createFilters(servletContext);
servletContext.addListener(new Log4jConfigListener());
servletContext.addListener(new ContextLoaderListener(rootContext));
// THIS WORKS
// XmlWebApplicationContext webContext = new XmlWebApplicationContext();
// webContext.setConfigLocation("/WEB-INF/spring/web-servlet.xml");
// THIS DOESN'T WHEN 2 SERVLETS ARE REGISTERED
AnnotationConfigWebApplicationContext webContext = new AnnotationConfigWebApplicationContext();
webContext.register(WebServlet.class);
ServletRegistration.Dynamic webServlet = servletContext.addServlet("web", new DispatcherServlet(webContext));
webServlet.setLoadOnStartup(1);
webServlet.addMapping( "/*");
// THIS WORKS
// XmlWebApplicationContext extContext = new XmlWebApplicationContext();
// extContext.setConfigLocation("/WEB-INF/spring/ext-servlet.xml");
// THIS DOESN'T WHEN 2 SERVLETS ARE REGISTERED
AnnotationConfigWebApplicationContext extContext = new AnnotationConfigWebApplicationContext();
extContext.register(ExtServlet.class);
ServletRegistration.Dynamic extServlet = servletContext.addServlet("ext",
new DispatcherServlet(extContext));
extServlet.setLoadOnStartup(2);
extServlet.addMapping("/ext/*");
}
}