AppConfig

 

@Configuration
@EnableConfigurationProperties(AppProperties.class)
public class AppConfig {
    @Value("${spring.mvc.servlet.path}")
    private String mvcServletPath;

    @Bean
    @ConditionalOnProperty(prefix = "app", name = "root-enabled", havingValue = "true", matchIfMissing = true)
    public ServletRegistrationBean rootServletRegistrationBean(AppProperties appProperties){
        String[] baseUrlMappings;
        String[] urlMappings;
        if(!"/".equals(Paths.get(mvcServletPath).getParent())){
            baseUrlMappings = new String[]{Paths.get(mvcServletPath).getParent() + "/", "/"};
            urlMappings = new String[]{Paths.get(mvcServletPath) + "/", "/"};
        } else {
            baseUrlMappings = new String[]{"/"};
            urlMappings = new String[]{"/"};
        }
        ServletRegistrationBean<RootServlet> bean = new ServletRegistrationBean<>(new RootServlet(appProperties, urlMappings),  baseUrlMappings);
        // 保险起见,把顺序放到最低,确保所有精确匹配先选
        bean.setOrder(Ordered.LOWEST_PRECEDENCE);
        return bean;
    }

    @Bean
    @ConditionalOnProperty(prefix = "app", name = "describe-enabled", havingValue = "true", matchIfMissing = true)
    public ServletRegistrationBean describeServletRegistrationBean(){
        return new ServletRegistrationBean<>(new DescribeServlet(), "/describe");
    }
}

 

posted @ 2025-09-19 15:46  牧之丨  阅读(4)  评论(0)    收藏  举报