原始写法如下
@Configuration
@Slf4j
public class WebConf extends WebMvcConfigurationSupport {@Value("${ees.static.path}")private String staticPath;@Value("${ees.swagger.enable}")private Boolean swaggerEnable;@Overrideprotected void addResourceHandlers(ResourceHandlerRegistry registry) {log.info("====================================="+swaggerEnable);log.info("====================================="+staticPath);if (swaggerEnable == null || swaggerEnable) {registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/", "classpath:/META-INF/resources/webjars/");registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/", "classpath:/META-INF/resources/webjars/");} .addResourceLocations(staticPath);registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");}}
配置完之后发现配置不生效日志也不打印~
后续排查发下
WebConf类继承自WebMvcConfigurationSupport,这可能会覆盖Spring Boot的自动配置。尽量避免直接继承WebMvcConfigurationSupport,而是实现WebMvcConfigurer接口,并使用@Configuration注解。
后续修改为如下写法:
@Configuration
@Slf4j
public class WebConf implements WebMvcConfigurer {@Value("${ees.static.path}")private String staticPath;@Value("${ees.swagger.enable}")private Boolean swaggerEnable;@Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {log.info("====================================="+swaggerEnable);log.info("====================================="+staticPath);// swagger访问配置if (swaggerEnable == null || swaggerEnable) {registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/", "classpath:/META-INF/resources/webjars/");registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/", "classpath:/META-INF/resources/webjars/");}registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");}}
修改后正常运行!配置生效!