update 优化 YmlPropertySourceFactory 代码警告问题

This commit is contained in:
疯狂的狮子Li
2026-04-10 15:01:18 +08:00
parent 347e536a61
commit d9cbba6b7b
2 changed files with 11 additions and 2 deletions

View File

@@ -1,6 +1,7 @@
package org.dromara.common.core.factory; package org.dromara.common.core.factory;
import org.dromara.common.core.utils.StringUtils; import org.dromara.common.core.utils.StringUtils;
import org.jspecify.annotations.Nullable;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean; import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.env.PropertiesPropertySource; import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.PropertySource; import org.springframework.core.env.PropertySource;
@@ -8,6 +9,7 @@ import org.springframework.core.io.support.DefaultPropertySourceFactory;
import org.springframework.core.io.support.EncodedResource; import org.springframework.core.io.support.EncodedResource;
import java.io.IOException; import java.io.IOException;
import java.util.Objects;
/** /**
* yml 配置源工厂 * yml 配置源工厂
@@ -17,13 +19,13 @@ import java.io.IOException;
public class YmlPropertySourceFactory extends DefaultPropertySourceFactory { public class YmlPropertySourceFactory extends DefaultPropertySourceFactory {
@Override @Override
public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException { public PropertySource<?> createPropertySource(@Nullable String name, EncodedResource resource) throws IOException {
String sourceName = resource.getResource().getFilename(); String sourceName = resource.getResource().getFilename();
if (StringUtils.isNotBlank(sourceName) && StringUtils.endsWithAny(sourceName, ".yml", ".yaml")) { if (StringUtils.isNotBlank(sourceName) && StringUtils.endsWithAny(sourceName, ".yml", ".yaml")) {
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
factory.setResources(resource.getResource()); factory.setResources(resource.getResource());
factory.afterPropertiesSet(); factory.afterPropertiesSet();
return new PropertiesPropertySource(sourceName, factory.getObject()); return new PropertiesPropertySource(sourceName, Objects.requireNonNull(factory.getObject()));
} }
return super.createPropertySource(name, resource); return super.createPropertySource(name, resource);
} }

View File

@@ -555,4 +555,11 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
return Strings.CS.replaceOnce(text, searchString, replacement); return Strings.CS.replaceOnce(text, searchString, replacement);
} }
/**
* 测试 CharSequence 是否以所提供的任何后缀结尾。
*/
public static boolean endsWithAny(final CharSequence sequence, final CharSequence... searchStrings) {
return Strings.CS.endsWithAny(sequence, searchStrings);
}
} }