From 81ad48374688981d5a79177c5b2c4f1ed8131036 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=B8=E6=99=93=E7=A3=8A?= Date: Wed, 3 Jan 2024 19:24:10 +0800 Subject: [PATCH] =?UTF-8?q?fixed:EnvironmentPostProcessor=E4=B8=AD?= =?UTF-8?q?=E5=AD=98=E5=9C=A8defaultProperties=E4=B8=8E=E4=B8=9A=E5=8A=A1?= =?UTF-8?q?=E5=86=B2=E7=AA=81=E7=9A=84=E9=97=AE=E9=A2=98=20xiaoyumin?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SpringFoxEnvironmentPostProcessor.java | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/knife4j/knife4j-openapi2-spring-boot-starter/src/main/java/com/github/xiaoymin/knife4j/spring/common/bean/SpringFoxEnvironmentPostProcessor.java b/knife4j/knife4j-openapi2-spring-boot-starter/src/main/java/com/github/xiaoymin/knife4j/spring/common/bean/SpringFoxEnvironmentPostProcessor.java index 3663fcac..f3cd2247 100644 --- a/knife4j/knife4j-openapi2-spring-boot-starter/src/main/java/com/github/xiaoymin/knife4j/spring/common/bean/SpringFoxEnvironmentPostProcessor.java +++ b/knife4j/knife4j-openapi2-spring-boot-starter/src/main/java/com/github/xiaoymin/knife4j/spring/common/bean/SpringFoxEnvironmentPostProcessor.java @@ -23,6 +23,9 @@ import org.springframework.boot.DefaultPropertiesPropertySource; import org.springframework.boot.SpringApplication; import org.springframework.boot.env.EnvironmentPostProcessor; import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.core.env.MapPropertySource; +import org.springframework.core.env.MutablePropertySources; +import org.springframework.core.env.PropertySource; import org.springframework.util.ClassUtils; import java.util.HashMap; @@ -34,18 +37,18 @@ import java.util.Map; * 2022/8/11 21:52 */ public class SpringFoxEnvironmentPostProcessor implements EnvironmentPostProcessor { - + /** * 配置名称key值 */ final static String SPRING_MVC_MATCHING_STRATEGY = "spring.mvc.pathmatch.matching-strategy"; - + /** * 当前策略枚举类全路径 */ final static String MATCHING_CLASS_NAME = "org.springframework.boot.autoconfigure.web.servlet.WebMvcProperties.MatchingStrategy"; Logger logger = LoggerFactory.getLogger(SpringFoxEnvironmentPostProcessor.class); - + /** * 处理springfox2.x版本兼容高版本Spring Boot出现空指针的异常的情况,为开发者自动配置matchingStrategy策略(如果开发者没要求的情况下) * springfox2 默认使用{@link org.springframework.util.AntPathMatcher} @@ -68,7 +71,14 @@ public class SpringFoxEnvironmentPostProcessor implements EnvironmentPostProcess sources.put(SPRING_MVC_MATCHING_STRATEGY, "ant_path_matcher"); // 添加 // fixed https://github.com/xiaoymin/knife4j/issues/686 - DefaultPropertiesPropertySource.addOrMerge(sources, environment.getPropertySources()); + // 如果environment中存在defaultProperties则直接将配置加入到该PropertySource中,不存在则创建新的defaultProperties加入到environment中 + MutablePropertySources propertySources = environment.getPropertySources(); + PropertySource defaultProperties = propertySources.remove(DefaultPropertiesPropertySource.NAME); + if (defaultProperties == null) { + defaultProperties = new MapPropertySource(DefaultPropertiesPropertySource.NAME, new HashMap<>()); + } + ((MapPropertySource) defaultProperties).getSource().putAll(sources); + propertySources.addLast(defaultProperties); } } } -- Gitee