フレームワークツールの使用

1:配列へのリスト

1.1:ツール

org.springframework.util.StringUtils

1.2:ユースケース

private List<String> filter(List<String> configurations, AutoConfigurationMetadata autoConfigurationMetadata) {
    
    
	...
	String[] candidates = StringUtils.toStringArray(configurations);
}

2:META-INF/spring.factoriesデータをロードする

SpringFactoriesLoader.loadFactoryNames({class 全限定名}, {类加载器});、といった:

List<String> configurations = SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(), getBeanClassLoader());

3:重複排除を一覧表示します

protected final <T> List<T> removeDuplicates(List<T> list) {
    
    
	return new ArrayList<>(new LinkedHashSet<>(list));
}

4:特定のクラスがクラスパスに存在するかどうかを取得します

4.1:ツール

org.springframework.util.ClassUtils

4.2:ユースケース

ClassUtils.isPresent("org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration", getClass().getClassLoader())

5:コンマ区切りの文字列を配列に変換します

5.1:ツール

org.springframework.util.StringUtils

5.2:ユースケース

// javax.servlet.Servlet,org.springframework.web.servlet.config.annotation.WebMvcConfigurer,org.springframework.web.servlet.DispatcherServlet
for (String candidate : StringUtils.commaDelimitedListToStringArray(candidates)) {
    
    
	ConditionOutcome outcome = getOutcome(candidate, this.beanClassLoader);
	if (outcome != null) {
    
    
		return outcome;
	}
}

6:現在使用されているクラスローダーを取得します

6.1:ツール

org.springframework.util.ClassUtils

6.2:ユースケース

public static boolean isPresent(String className, ClassLoader classLoader) {
    
    
	if (classLoader == null) {
    
    
		classLoader = ClassUtils.getDefaultClassLoader();
	}
	try {
    
    
		forName(className, classLoader);
		return true;
	}
	catch (Throwable ex) {
    
    
		return false;
	}
}

7:配列をコンマ区切りの文字列に変換します

7.1:ツール

org.springframework.util.StringUtils#arrayToDelimitedString

7.2:ユースケース

public Builder andCondition(String condition, Object... details) {
    
    
	Assert.notNull(condition, "Condition must not be null");
	String detail = StringUtils.arrayToDelimitedString(details, " ");
	if (StringUtils.hasLength(detail)) {
    
    
		return new Builder(condition + " " + detail);
	}
	return new Builder(condition);
}

8:配列コピー

8.1:ツール

java.lang.System#arraycopy

8.2:ユースケース

ConditionOutcome[] secondHalf = secondHalfResolver.resolveOutcomes();
ConditionOutcome[] firstHalf = firstHalfResolver.resolveOutcomes();
ConditionOutcome[] outcomes = new ConditionOutcome[autoConfigurationClasses.length];
System.arraycopy(firstHalf, 0, outcomes, 0, firstHalf.length);
System.arraycopy(secondHalf, 0, outcomes, split, secondHalf.length);

添付のソースコード:

/*
src:源数组
srcPos:源数组拷贝的开始位置
dest:目标数组
destPos:拷贝到目标数组的开始位置
length:拷贝源数组的元素的个数
*/
public static native void arraycopy(Object src,  
									int srcPos,
                                    Object dest, 
                                    int destPos,
                                    int length);

9:クラスに基づいてオブジェクトを作成する

9.1:ツール

org.springframework.beans.BeanUtils#instantiateClass(java.lang.Class<T>)

9.2:使用例

protected ConfigurableApplicationContext createApplicationContext() {
    
    
	Class<?> contextClass = this.applicationContextClass;
	if (contextClass == null) {
    
    
		try {
    
    
			switch (this.webApplicationType) {
    
    
			case SERVLET:
				contextClass = Class.forName(DEFAULT_SERVLET_WEB_CONTEXT_CLASS);
				break;
			case REACTIVE:
				contextClass = Class.forName(DEFAULT_REACTIVE_WEB_CONTEXT_CLASS);
				break;
			default:
				contextClass = Class.forName(DEFAULT_CONTEXT_CLASS);
			}
		}
		catch (ClassNotFoundException ex) {
    
    
			throw new IllegalStateException(
					"Unable create a default ApplicationContext, " + "please specify an ApplicationContextClass",
					ex);
		}
	}
	return (ConfigurableApplicationContext) BeanUtils.instantiateClass(contextClass);
}

10:URL转URI

10.1:ツール

org.springframework.util.ResourceUtils

10.2:ユースケース

org.springframework.core.io.AbstractResource#getURI
@Override
publicURI getURI() throws IOException {
    
    
	URL url = getURL();
	try {
    
    
		return ResourceUtils.toURI(url);
	}
	catch (URISyntaxException ex) {
    
    
		throw new NestedIOException("Invalid URI [" + url + "]", ex);
	}
}

11:処理パスは標準フォーマットです

11.1:機能の説明

相対パスを処理...およびその他の情報については、次のように削除..
ここに画像の説明を挿入します
処理スラッシュ:
ここに画像の説明を挿入します

11.2:ツール

org.springframework.util.StringUtils#cleanPath

11.3:ユースケース

org.springframework.core.io.FileSystemResource#FileSystemResource(java.lang.String)
public FileSystemResource(String path) {
    
    
	Assert.notNull(path, "Path must not be null");
	this.path = StringUtils.cleanPath(path);
	this.file = new File(path);
	this.filePath = this.file.toPath();
}

12:file:プロトコルファイルのURLからFileオブジェクト取得します

12.1:ツール

org.springframework.util.ResourceUtils#getFile(java.net.URL, java.lang.String)

12.2:ユースケース

org.springframework.core.io.AbstractFileResolvingResource#getFile()
@Override
public File getFile() throws IOException {
    
    
	URL url = getURL();
	if (url.getProtocol().startsWith(ResourceUtils.URL_PROTOCOL_VFS)) {
    
    
		return VfsResourceDelegate.getResource(url).getFile();
	}
	return ResourceUtils.getFile(url, getDescription());
}

13:パスにワイルドカードが含まれているかどうかを確認します

13.1:ツール

org.springframework.util.AntPathMatcher#isPattern

13.1:使用例

new AntPathMatcher().isPattern("yudaosourcecode/spring/*.class")

14:.propertiesファイルをロードします

14.1:ツール

org.springframework.core.io.support.PropertiesLoaderUtils

14.2:使用例

// META-INF/spring.schemas
Properties mappings = PropertiesLoaderUtils.loadAllProperties(this.schemaMappingsLocation, this.classLoader);
schemaMappings = new ConcurrentHashMap<>(mappings.size());
// 合并properties内容到map中
CollectionUtils.mergePropertiesIntoMap(mappings, schemaMappings);
this.schemaMappings = schemaMappings;

15:.propertiesをマップにマージします

15.1:ツール

org.springframework.util.CollectionUtils

15.2:使用例

// META-INF/spring.schemas
Properties mappings = PropertiesLoaderUtils.loadAllProperties(this.schemaMappingsLocation, this.classLoader);
schemaMappings = new ConcurrentHashMap<>(mappings.size());
// 合并properties内容到map中
CollectionUtils.mergePropertiesIntoMap(mappings, schemaMappings);

16:あるコレクションから別のコレクションに存在する最初の要素を見つける

16.1:ツール

org.springframework.util.CollectionUtils#findFirstMatch

16.2:ユースケース

org.springframework.beans.factory.xml.BeanDefinitionParserDelegate#checkNameUniqueness
protected void checkNameUniqueness(String beanName, List<String> aliases, Element beanElement) {
    
    
	String foundName = null;

	if (StringUtils.hasText(beanName) && this.usedNames.contains(beanName)) {
    
    
		foundName = beanName;
	}
	if (foundName == null) {
    
    
		foundName = CollectionUtils.findFirstMatch(this.usedNames, aliases);
	}
	if (foundName != null) {
    
    
		error("Bean name '" + foundName + "' is already used in this <beans> element", beanElement);
	}

	this.usedNames.add(beanName);
	this.usedNames.addAll(aliases);
}

おすすめ

転載: blog.csdn.net/wang0907/article/details/113460564