如何在Spring Boot中获得DispatcherServeletInitializer功能 [英] How to get DispatcherServeletInitializer functionality in Spring Boot

查看:54
本文介绍了如何在Spring Boot中获得DispatcherServeletInitializer功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们希望将我们的项目迁移到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可以处理AppConfigWebappConfig父/子关系,尽管您也可以考虑使用统一的层次结构.

The AppConfig and WebappConfig parent/child relationship can be handled by SpringApplicationBuilder, although you might also consider a flat hierarchy.

假设您要花费大量精力,并运行一个嵌入式servlet容器,则可以将FiltersServlets直接注册为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之类的内容,也可以使用ServletRegistrationBeanFilterRegistrationBean.最后一个选择是添加一个实现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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆