如何在Spring Boot中获得DispatcherServeletInitializer功能 [英] How to get DispatcherServeletInitializer functionality in Spring Boot
问题描述
我们希望将我们的项目迁移到Spring Boot.但是还不清楚如何在Spring Boot中复制AbstractAnnotationConfigDispatcherServletInitializer
的功能?
We are looking to migrate our project to Spring Boot. However it is unclear how to replicate the functionality of AbstractAnnotationConfigDispatcherServletInitializer
in Spring Boot?
public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer
{
@Override
protected Class<?>[] getRootConfigClasses()
{
return new Class<?>[]{AppConfig.class};
}
@Override
protected Class<?>[] getServletConfigClasses()
{
return new Class<?>[]{WebappConfig.class};
}
@Override
protected void customizeRegistration(ServletRegistration.Dynamic registration) {
registration.setAsyncSupported(true);
}
@Override
protected String[] getServletMappings()
{
return new String[]{"/"};
}
@Override
protected Filter[] getServletFilters()
{
DelegatingFilterProxy shiroFilter = new DelegatingFilterProxy("shiroFilter");
shiroFilter.setTargetFilterLifecycle(true);
CompositeFilter compositeFilter = new CompositeFilter();
compositeFilter.setFilters(ImmutableList.of(new CorsFilter(),shiroFilter));
return new Filter[]{compositeFilter};
}
}
推荐答案
SpringApplicationBuilder
可以处理AppConfig
和WebappConfig
父/子关系,尽管您也可以考虑使用统一的层次结构.
The AppConfig
and WebappConfig
parent/child relationship can be handled by SpringApplicationBuilder
, although you might also consider a flat hierarchy.
假设您要花费大量精力,并运行一个嵌入式servlet容器,则可以将Filters
和Servlets
直接注册为bean.
Assuming that you are going the whole hog, and running an embedded servlet container you can register Filters
and Servlets
directly as beans.
如果需要设置诸如setAsyncSupported
之类的内容,也可以使用ServletRegistrationBean
和FilterRegistrationBean
.最后一个选择是添加一个实现org.springframework.boot.context.embedded.ServletContextInitializer
的bean,然后自己进行注册.
You can also use ServletRegistrationBean
and FilterRegistrationBean
if you need to set things such as setAsyncSupported
. The final option is to add a bean that implements org.springframework.boot.context.embedded.ServletContextInitializer
then do the registration yourself.
类似的事情可能会让您更进一步:
Something like this might get you a bit further:
@Bean
public ServletRegistrationBean dispatcherServlet() {
ServletRegistrationBean registration = new ServletRegistrationBean(
new DispatcherServlet(), "/");
registration.setAsyncSupported(true);
return registration;
}
@Bean
public Filter compositeFilter() {
CompositeFilter compositeFilter = new CompositeFilter();
compositeFilter.setFilters(ImmutableList.of(new CorsFilter(), shiroFilter));
return compositeFilter
}
Also, take a look at this section in the reference manual http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-embedded-container
这篇关于如何在Spring Boot中获得DispatcherServeletInitializer功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!