Master Spring’s Core Utility Classes: Assertions, Collections, IO, and Reflection Explained
This article provides a comprehensive guide to Spring's most useful utility classes—including Assert, ObjectUtils, StringUtils, CollectionUtils, FileCopyUtils, ResourceUtils, ReflectionUtils, and AopUtils—detailing their purpose, key methods, and practical code examples for backend developers.
Spring offers a rich set of utility classes that simplify common programming tasks. Below is a concise reference of the most frequently used utilities, their core methods, and example signatures.
Assert
Logical checks that validate conditions which should never occur.
// Enable assertions with JVM option
-enableassertions
// Example methods
void notNull(Object object, String message)
void isNull(Object object, String message)
void isTrue(boolean expression, String message)
void notEmpty(Collection collection, String message)
void hasLength(String text, String message)
void hasText(String text, String message)
void isInstanceOf(Class type, Object obj, String message)
void isAssignable(Class superType, Class subType, String message)ObjectUtils
Utilities for handling objects, arrays, and collections.
// Basic object information
String nullSafeClassName(Object obj)
int nullSafeHashCode(Object obj)
String nullSafeToString(boolean[] array)
String getIdentityHexString(Object obj)
String identityToString(Object obj)
String getDisplayString(Object obj)
// Checks
boolean isEmpty(Object[] array)
boolean isArray(Object obj)
boolean containsElement(Object[] array, Object element)
boolean nullSafeEquals(Object o1, Object o2)
boolean isEmpty(Object obj) // works for Optional, arrays, CharSequence, Collection, Map
// Miscellaneous
<A, O extends A> A[] addObjectToArray(A[] array, O obj)
String toObjectArray(Object source)
String[] toObjectArray(boolean[] source)StringUtils
String validation and manipulation helpers.
// Validation
boolean isEmpty(Object str)
boolean endsWithIgnoreCase(String str, String suffix)
boolean startsWithIgnoreCase(String str, String prefix)
boolean containsWhitespace(String str)
boolean hasLength(CharSequence str)
boolean hasText(CharSequence str)
boolean substringMatch(CharSequence str, int index, CharSequence substring)
int countOccurrencesOf(String str, String sub)
// Operations
String replace(String inString, String oldPattern, String newPattern)
String trimTrailingCharacter(String str, char trailingCharacter)
String trimLeadingCharacter(String str, char leadingCharacter)
String trimLeadingWhitespace(String str)
String trimTrailingWhitespace(String str)
String trimWhitespace(String str)
String trimAllWhitespace(String str)
String delete(String inString, String pattern)
String deleteAny(String inString, String charsToDelete)
String[] trimArrayElements(String[] array)
String uriDecode(String source, Charset charset)CollectionUtils
Helpers for collections and maps.
// Collection checks
boolean isEmpty(Collection<?> collection)
boolean isEmpty(Map<?,?> map)
boolean containsInstance(Collection<?> collection, Object element)
boolean contains(Iterator<?> iterator, Object element)
boolean containsAny(Collection<?> source, Collection<?> candidates)
boolean hasUniqueObject(Collection<?> collection)
// Collection operations
<E> void mergeArrayIntoCollection(Object array, Collection<E> collection)
<K,V> void mergePropertiesIntoMap(Properties props, Map<K,V> map)
<T> T lastElement(List<T> list)
<T> T lastElement(Set<T> set)
<E> E findFirstMatch(Collection<?> source, Collection<E> candidates)
<T> T findValueOfType(Collection<?> collection, Class<T> type)
Object findValueOfType(Collection<?> collection, Class<?>[] types)
Class<?> findCommonElementType(Collection<?> collection)FileCopyUtils
Convenient methods for file and stream copying.
// Input
byte[] copyToByteArray(File in)
byte[] copyToByteArray(InputStream in)
String copyToString(Reader in)
// Output
void copy(byte[] in, File out)
int copy(File in, File out)
void copy(byte[] in, OutputStream out)
int copy(InputStream in, OutputStream out)
int copy(Reader in, Writer out)
void copy(String in, Writer out)ResourceUtils
Utilities for loading resources from various locations.
// URL handling
static boolean isUrl(String resourceLocation)
static URL getURL(String resourceLocation)
static File getFile(String resourceLocation)
// Resource types
FileSystemResource
UrlResource
ClassPathResource
ServletContextResource
// Resource operations
boolean exists()
File getFile()
URI getURI()
URL getURL()
InputStream getInputStream()
String getDescription()ReflectionUtils
Reflection helpers for methods, fields, and constructors.
// Method lookup
Method findMethod(Class<?> clazz, String name)
Method findMethod(Class<?> clazz, String name, Class<?>... paramTypes)
Method[] getAllDeclaredMethods(Class<?> leafClass)
Constructor<T> accessibleConstructor(Class<T> clazz, Class<?>... parameterTypes)
boolean isEqualsMethod(Method method)
boolean isHashCodeMethod(Method method)
boolean isToStringMethod(Method method)
boolean isObjectMethod(Method method)
boolean declaresException(Method method, Class<?> exceptionType)
// Method invocation
Object invokeMethod(Method method, Object target)
Object invokeMethod(Method method, Object target, Object... args)
void makeAccessible(Method method)
void makeAccessible(Constructor<?> ctor)
// Field lookup
Field findField(Class<?> clazz, String name)
Field findField(Class<?> clazz, String name, Class<?> type)
boolean isPublicStaticFinal(Field field)
// Field access
Object getField(Field field, Object target)
void setField(Field field, Object target, Object value)
void shallowCopyFieldState(Object src, Object dest)
void makeAccessible(Field field)
void doWithFields(Class<?> clazz, ReflectionUtils.FieldCallback fc)
void doWithFields(Class<?> clazz, ReflectionUtils.FieldCallback fc, ReflectionUtils.FieldFilter ff)
void doWithLocalFields(Class<?> clazz, ReflectionUtils.FieldCallback fc)AopUtils
Utility methods for working with Spring AOP proxies.
// Proxy type checks
boolean isAopProxy()
boolean isJdkDynamicProxy()
boolean isCglibProxy()
// Retrieve target class
Class<?> getTargetClass()AopContext
Access the current AOP proxy. Object currentProxy() These utilities together form a powerful toolbox for backend developers working with the Spring framework, reducing boilerplate and improving code readability.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
