Comprehensive Guide to Spring Utility Classes: Assertions, ObjectUtils, StringUtils, CollectionUtils, FileCopyUtils, ReflectionUtils, and AOP Helpers
This article presents a comprehensive guide to Spring's utility classes—including Assert, ObjectUtils, StringUtils, CollectionUtils, FileCopyUtils, ResourceUtils, StreamUtils, ReflectionUtils, and AOP helpers—detailing their purpose, usage patterns, and code examples, while also containing promotional material for related services.
Introduction: The author, a senior architect, noticed many duplicate utility classes in projects and compiled this guide to help developers use Spring's built‑in utilities.
01 Assertion
SpringBoot provides the Assert class for logical checks such as notNull , isNull , isTrue , notEmpty , etc.
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)02 Object, Array, Collection Utilities
ObjectUtils offers methods to safely obtain class names, hash codes, and string representations, handling null values gracefully.
String nullSafeClassName(Object obj)
int nullSafeHashCode(Object object)
String nullSafeToString(boolean[] array)
String getIdentityHexString(Object obj)
String identityToString(Object obj)
String getDisplayString(Object obj)CollectionUtils provides checks for emptiness, containment, uniqueness, and merging operations.
boolean isEmpty(Collection
collection)
boolean isEmpty(Map
map)
boolean containsInstance(Collection
collection, Object element)
boolean containsAny(Collection
source, Collection
candidates)
boolean hasUniqueObject(Collection
collection)
<E> void mergeArrayIntoCollection(Object array, Collection
collection)
<K,V> void mergePropertiesIntoMap(Properties props, Map
map)
<T> T lastElement(List
list)
<T> T lastElement(Set
set)
<E> E findFirstMatch(Collection
source, Collection
candidates)
<T> T findValueOfType(Collection
collection, Class
type)
Object findValueOfType(Collection
collection, Class
[] types)
Class
findCommonElementType(Collection
collection)03 File, Resource, IO Stream Utilities
FileCopyUtils and StreamUtils simplify copying between files, streams, and byte arrays.
byte[] copyToByteArray(File in)
byte[] copyToByteArray(InputStream in)
String copyToString(Reader in)
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)
int copyRange(InputStream in, OutputStream out, long start, long end)ResourceUtils helps resolve URLs, files, and classpath resources, providing methods such as isUrl , getURL , and getFile .
static boolean isUrl(String resourceLocation)
static URL getURL(String resourceLocation)
static File getFile(String resourceLocation)Resource abstractions (FileSystemResource, UrlResource, ClassPathResource, ServletContextResource) expose methods like exists() , getFile() , getURI() , getURL() , getInputStream() , and getDescription() .
04 Reflection and AOP Utilities
ReflectionUtils offers methods to find fields, methods, constructors, and to manipulate accessibility.
Method findMethod(Class
clazz, String name)
Method findMethod(Class
clazz, String name, Class
... paramTypes)
Method[] getAllDeclaredMethods(Class
leafClass)
Constructor
accessibleConstructor(Class
clazz, Class
... parameterTypes)
Field findField(Class
clazz, String name)
Field findField(Class
clazz, String name, Class
type)
boolean isPublicStaticFinal(Field field)
Object getField(Field field, Object target)
void setField(Field field, Object target, Object value)
void makeAccessible(Method method)
void makeAccessible(Field field)
Object invokeMethod(Method method, Object target)
Object invokeMethod(Method method, Object target, Object... args)AopUtils and AopContext help identify proxy types and retrieve the underlying target class or current proxy instance.
boolean isAopProxy()
boolean isJdkDynamicProxy()
boolean isCglibProxy()
Class
getTargetClass()
Object currentProxy()The article concludes with an invitation for readers to discuss the content, share opinions, and contact the author for further communication.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
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.