字符串处理 1. StringUtils 一个用于常见字符串操作的综合工具,包括检查空字符串或仅包含空白字符的字符串。
1 2 3 4 5 6 7 8 9 10 11 import org.springframework.util.StringUtils;boolean isEmpty1 = StringUtils.isEmpty(null ); boolean isEmpty2 = StringUtils.isEmpty("" ); boolean hasText1 = StringUtils.hasText(" " ); boolean hasText2 = StringUtils.hasText("hello" ); String[] parts = StringUtils.tokenizeToStringArray("a,b,c" , "," ); String trimmed = StringUtils.trimWhitespace(" hello " );
2. AntPathMatcher 一个强大的工具,用于根据 Ant 风格的路径模式匹配字符串,常用于 URL 路由和安全配置。
1 2 3 4 5 6 7 8 9 import org.springframework.util.AntPathMatcher;AntPathMatcher matcher = new AntPathMatcher ();boolean match1 = matcher.match("/users/*" , "/users/123" ); boolean match2 = matcher.match("/users/**" , "/users/123/orders" ); boolean match3 = matcher.match("/user?" , "/user1" ); Map<String, String> vars = matcher.extractUriTemplateVariables( "/users/{id}" , "/users/42" );
3. PatternMatchUtils 提供了一种更简单的方法来执行基本的通配符模式匹配。
1 2 3 4 5 6 import org.springframework.util.PatternMatchUtils;boolean matches1 = PatternMatchUtils.simpleMatch("user*" , "username" ); boolean matches2 = PatternMatchUtils.simpleMatch("user?" , "user1" ); boolean matches3 = PatternMatchUtils.simpleMatch( new String []{"user*" , "admin*" }, "adminuser" );
4. PropertyPlaceholderHelper 使用给定的属性源解析字符串中的占位符(如 ${name})。
1 2 3 4 5 6 7 8 9 import org.springframework.util.PropertyPlaceholderHelper;import java.util.Properties;PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper ("${" , "}" );Properties props = new Properties ();props.setProperty("name" , "World" ); props.setProperty("greeting" , "Hello ${name}!" ); String result = helper.replacePlaceholders("${greeting}" , props::getProperty);
集合与数组处理 5. CollectionUtils 提供广泛的集合操作工具,例如检查是否为空以及执行集合运算。
1 2 3 4 5 6 7 8 9 10 import org.springframework.util.CollectionUtils;import java.util.*;boolean isEmpty1 = CollectionUtils.isEmpty(null ); boolean isEmpty2 = CollectionUtils.isEmpty(Collections.emptyList()); List<String> list1 = Arrays.asList("a" , "b" , "c" ); List<String> list2 = Arrays.asList("b" , "c" , "d" ); Collection<String> intersection = CollectionUtils.intersection(list1, list2);
6. MultiValueMap 标准 Map接口的扩展,允许为一个键存储多个值。
1 2 3 4 5 6 7 8 import org.springframework.util.LinkedMultiValueMap;import org.springframework.util.MultiValueMap;MultiValueMap<String, String> map = new LinkedMultiValueMap <>(); map.add("colors" , "red" ); map.add("colors" , "blue" ); map.add("sizes" , "large" ); List<String> colors = map.get("colors" );
7. ConcurrentReferenceHashMap 一个线程安全、基于软引用的 Map,非常适合缓存场景,允许垃圾收集器在需要时回收内存。
1 2 3 4 5 6 import org.springframework.util.ConcurrentReferenceHashMap;import java.util.Map;Map<String, Object> cache = new ConcurrentReferenceHashMap <>(); cache.put("key1" , new Object ());
8. SystemPropertyUtils 解析 JVM 系统属性中的占位符。
1 2 3 4 5 6 7 import org.springframework.util.SystemPropertyUtils;String javaHome = SystemPropertyUtils.resolvePlaceholders("${java.home}" );String pathWithDefault = SystemPropertyUtils.resolvePlaceholders( "${unknown.property:default_value}" );
反射与类操作 9. ReflectionUtils 一组用于低级反射任务(如查找字段或调用方法)的静态方法,并能优雅地处理异常。
1 2 3 4 5 6 7 8 9 10 11 import org.springframework.util.ReflectionUtils;import java.lang.reflect.Field;import java.lang.reflect.Method;Field field = ReflectionUtils.findField(MyClass.class, "name" );ReflectionUtils.makeAccessible(field); ReflectionUtils.setField(field, myObject, "John" ); Method method = ReflectionUtils.findMethod(MyClass.class, "setAge" , int .class);ReflectionUtils.invokeMethod(method, myObject, 30 );
10. ClassUtils 提供处理 Class对象的广泛工具,例如获取短类名或查找接口。
1 2 3 4 5 6 7 8 import org.springframework.util.ClassUtils;String shortName = ClassUtils.getShortName("org.example.MyClass" ); boolean exists = ClassUtils.isPresent("java.util.List" , null ); ClassLoader classLoader = ClassUtils.getDefaultClassLoader();
11. MethodInvoker 一种在目标对象上准备并调用带参数方法的便捷方式。
1 2 3 4 5 6 7 8 import org.springframework.util.MethodInvoker;MethodInvoker invoker = new MethodInvoker ();invoker.setTargetObject(new MyService ()); invoker.setTargetMethod("calculateTotal" ); invoker.setArguments(100 , 0.2 ); invoker.prepare(); Object result = invoker.invoke();
12. BeanUtils 一个强大的工具,用于常见的 Bean 相关操作,包括属性复制和实例化。
1 2 3 4 5 6 7 8 import org.springframework.beans.BeanUtils;Person source = new Person ("John" , 30 );PersonDTO target = new PersonDTO ();BeanUtils.copyProperties(source, target); Person newPerson = BeanUtils.instantiateClass(Person.class);
I/O 操作 13. FileCopyUtils 提供简单的静态方法,用于在文件、流、Reader 和 Writer 之间复制内容。
1 2 3 4 5 6 7 8 9 10 import org.springframework.util.FileCopyUtils;import java.io.*;byte [] bytes = FileCopyUtils.copyToByteArray(new File ("input.txt" ));FileCopyUtils.copy(bytes, new File ("output.txt" )); String content = FileCopyUtils.copyToString(new FileReader ("input.txt" ));FileCopyUtils.copy(new FileInputStream ("input.txt" ), new FileOutputStream ("output.txt" ));
14. ResourceUtils 用于将资源位置(如 classpath:或 file:)解析为 File或 URL对象的工具。
1 2 3 4 5 6 7 8 9 10 import org.springframework.util.ResourceUtils;import java.io.File;import java.net.URL;File file = ResourceUtils.getFile("classpath:config.properties" );boolean isUrl = ResourceUtils.isUrl("http://example.com" ); URL url = ResourceUtils.getURL("classpath:data.json" );
15. StreamUtils 包含处理 InputStream和 OutputStream的实用方法,防止常见的样板代码。
1 2 3 4 5 6 7 8 9 import org.springframework.util.StreamUtils;import java.nio.charset.StandardCharsets;byte [] data = StreamUtils.copyToByteArray(inputStream);String text = StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8);StreamUtils.copy("Hello" , StandardCharsets.UTF_8, outputStream);
16. FileSystemUtils 提供文件系统操作工具,最值得注意的是递归复制和删除目录。
1 2 3 4 5 6 7 import org.springframework.util.FileSystemUtils;import java.io.File;boolean deleted = FileSystemUtils.deleteRecursively(new File ("/tmp/test" ));FileSystemUtils.copyRecursively(new File ("source" ), new File ("target" ));
17. ResourcePatternUtils 将资源模式(如 classpath*:**/*.xml)解析为 Resource对象数组。
1 2 3 4 5 6 7 8 import org.springframework.core.io.Resource;import org.springframework.core.io.support.PathMatchingResourcePatternResolver;import org.springframework.core.io.support.ResourcePatternResolver;import org.springframework.core.io.support.ResourcePatternUtils;ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver ();Resource[] resources = resolver.getResources("classpath*:META-INF/*.xml" );
Web 相关 18. WebUtils 各种 Web 实用程序的集合,适用于获取 Cookie 或请求参数等任务。
1 2 3 4 5 6 7 8 import org.springframework.web.util.WebUtils;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.Cookie;Cookie cookie = WebUtils.getCookie(request, "sessionId" );int pageSize = WebUtils.getIntParameter(request, "pageSize" , 10 );
19. UriUtils 根据 RFC 3986 对 URI 组件进行编码和解码的辅助工具。
1 2 3 4 5 6 7 import org.springframework.web.util.UriUtils;import java.nio.charset.StandardCharsets;String encoded = UriUtils.encodePathSegment("path with spaces" , StandardCharsets.UTF_8);String decoded = UriUtils.decode(encoded, StandardCharsets.UTF_8);
20. UriComponentsBuilder 一个可变构建器,用于从头开始创建 UriComponents或通过修改现有 URI 来操作它。
1 2 3 4 5 6 7 8 9 import org.springframework.web.util.UriComponentsBuilder;import java.net.URI;URI uri = UriComponentsBuilder.fromHttpUrl("http://example.com" ) .path("/products/{id}" ) .queryParam("category" , "books" ) .build("123" );
21. ContentCachingRequestWrapper 包装 HttpServletRequest以缓存其主体,允许读取多次请求负载。
1 2 3 4 5 6 7 8 import org.springframework.web.util.ContentCachingRequestWrapper;import javax.servlet.http.HttpServletRequest;ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper (request);byte [] body = wrapper.getContentAsByteArray();String bodyAsString = new String (body, wrapper.getCharacterEncoding());
22. HtmlUtils 用于 HTML 转义和反转义的工具,有助于防止跨站脚本 (XSS) 攻击。
1 2 3 4 5 6 7 8 import org.springframework.web.util.HtmlUtils;String escaped = HtmlUtils.htmlEscape("<script>alert('XSS')</script>" );String unescaped = HtmlUtils.htmlUnescape("<b>Bold</b>" );
验证与断言 23. Assert 提供静态断言方法来检查前置条件。如果断言失败,它会抛出 IllegalArgumentException。
1 2 3 4 5 6 7 8 import org.springframework.util.Assert;Assert.notNull(object, "Object must not be null" ); Assert.hasText(name, "Name must not be empty" ); Assert.isTrue(amount > 0 , "Amount must be positive" ); Assert.notEmpty(items, "Items collection must not be empty" ); Assert.state(isInitialized, "Service is not initialized" );
24. ObjectUtils 用于处理对象的工具,特别是用于空安全操作和检查各种类型(数组、集合等)是否为空。
1 2 3 4 5 6 7 8 9 import org.springframework.util.ObjectUtils;boolean isEmpty1 = ObjectUtils.isEmpty(null ); boolean isEmpty2 = ObjectUtils.isEmpty(new int [0 ]); boolean equals = ObjectUtils.nullSafeEquals(obj1, obj2);String value = ObjectUtils.getOrDefault(null , "default" );
25. NumberUtils 用于在不同数字类型之间转换并将字符串解析为数字的工具。
1 2 3 4 5 6 import org.springframework.util.NumberUtils;Integer parsedInt = NumberUtils.parseNumber("42" , Integer.class);Double convertedDouble = NumberUtils.convertNumberToTargetClass(42 , Double.class);
日期与时间 一个灵活的 java.util.Date对象格式化程序,用于日期和字符串之间的转换。
1 2 3 4 5 6 7 import org.springframework.format.datetime.DateFormatter;import java.util.Date;import java.util.Locale;DateFormatter formatter = new DateFormatter ("yyyy-MM-dd HH:mm:ss" );String formattedDate = formatter.print(new Date (), Locale.getDefault());
27. StopWatch 一个简单的任务计时工具,提供了一种方便的方法来测量不同代码块的执行时间。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 import org.springframework.util.StopWatch;StopWatch watch = new StopWatch ("MyTask" );watch.start("Phase 1: Data Loading" ); Thread.sleep(100 ); watch.stop(); watch.start("Phase 2: Data Processing" ); Thread.sleep(200 ); watch.stop(); System.out.println(watch.prettyPrint());
安全相关 28. DigestUtils 提供创建消息摘要(哈希)(如 MD5)的静态方法。
1 2 3 4 5 6 7 8 9 10 import org.springframework.util.DigestUtils;import java.io.InputStream;import java.io.FileInputStream;String md5Hex = DigestUtils.md5DigestAsHex("password" .getBytes());try (InputStream is = new FileInputStream ("file.txt" )) { String fileMd5 = DigestUtils.md5DigestAsHex(is); }
29. Base64Utils 用于 Base64 编码和解码数据的工具。
1 2 3 4 5 6 7 import org.springframework.util.Base64Utils;byte [] data = "Hello World" .getBytes();String encoded = Base64Utils.encodeToString(data);byte [] decoded = Base64Utils.decodeFromString(encoded);
30. TextEncryptor Spring Security 提供的接口,用于文本的双向加密和解密。
1 2 3 4 5 6 7 8 9 import org.springframework.security.crypto.encrypt.Encryptors;import org.springframework.security.crypto.encrypt.TextEncryptor;String password = "my-secret-password" ;String salt = "deadbeef" ; TextEncryptor encryptor = Encryptors.text(password, salt);String encryptedText = encryptor.encrypt("This is a secret message" );String decryptedText = encryptor.decrypt(encryptedText);
JSON 与数据转换 31. JsonParserFactory 用于获取 JsonParser实例的工厂,提供了一种简单的方法来解析 JSON 字符串,而无需完整的数据绑定库。
1 2 3 4 5 6 7 8 9 10 11 import org.springframework.boot.json.JsonParser;import org.springframework.boot.json.JsonParserFactory;import java.util.Map;import java.util.List;JsonParser parser = JsonParserFactory.getJsonParser();Map<String, Object> map = parser.parseMap("{\"name\":\"John\", \"age\":30}" ); List<Object> list = parser.parseList("[1, 2, 3]" );
32. ResolvableType 提供一种机制来在运行时处理 java.lang.Class无法单独处理的复杂泛型类型。
1 2 3 4 5 6 7 8 9 import org.springframework.core.ResolvableType;import java.util.List;import java.util.Map;ResolvableType listType = ResolvableType.forField(getClass().getDeclaredField("myList" ));ResolvableType elementType = listType.getGeneric(0 ); ResolvableType mapType = ResolvableType.forClassWithGenerics(Map.class, String.class, Integer.class);
33. MappingJackson2HttpMessageConverter Spring MVC 中用于在 Java 对象和 JSON 之间进行转换的核心组件,可以进行自定义。
1 2 3 4 5 6 7 import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;import com.fasterxml.jackson.databind.ObjectMapper;ObjectMapper customMapper = new ObjectMapper ();MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter (customMapper);
其他实用工具 34. RandomStringUtils (Apache Commons) 虽然不是 Spring 类,但在 Spring 项目中经常使用(通常通过 spring-boot-starter包含),值得一提的是它用于生成随机字符串。
1 2 3 4 5 6 7 import org.apache.commons.lang3.RandomStringUtils;String randomAlpha = RandomStringUtils.randomAlphabetic(10 );String randomAlphanumeric = RandomStringUtils.randomAlphanumeric(10 );String randomNumeric = RandomStringUtils.randomNumeric(6 );
35. CompletableFuture 标准 Java 类,但在现代异步 Spring 应用程序中必不可少。使用其静态方法来组合结果。
1 2 3 4 5 6 7 8 9 10 11 12 import java.util.concurrent.CompletableFuture;import java.util.List;import java.util.stream.Collectors;List<CompletableFuture<String>> futures = ; CompletableFuture<Void> allOf = CompletableFuture.allOf( futures.toArray(new CompletableFuture [0 ])); CompletableFuture<List<String>> allResults = allOf.thenApply(v -> futures.stream() .map(CompletableFuture::join) .collect(Collectors.toList()));
36. CacheControl 一个构建器,用于以类型安全的方式创建 HTTP Cache-Control标头值。
1 2 3 4 5 6 7 8 import org.springframework.http.CacheControl;import java.util.concurrent.TimeUnit;CacheControl cacheControl = CacheControl.maxAge(1 , TimeUnit.HOURS) .noTransform() .mustRevalidate(); String headerValue = cacheControl.getHeaderValue();
37. AnnotationUtils 用于查找类、方法和字段上的注解的强大工具,包括支持元注解和合并注解属性。
1 2 3 4 5 6 7 import org.springframework.core.annotation.AnnotationUtils;import org.springframework.stereotype.Component;Component annotation = AnnotationUtils.findAnnotation(MyService.class, Component.class);String value = (String) AnnotationUtils.getValue(annotation, "value" );
38. DefaultConversionService Spring 对 ConversionService的默认实现,可以在许多标准类型之间进行转换(例如,字符串到整数)。
1 2 3 4 5 6 7 import org.springframework.core.convert.support.DefaultConversionService;DefaultConversionService conversionService = new DefaultConversionService ();Integer intValue = conversionService.convert("42" , Integer.class);Boolean boolValue = conversionService.convert("true" , Boolean.class);
一个专门用于处理 HTTP 标头的 MultiValueMap,提供方便的常量和方法。
1 2 3 4 5 6 7 8 import org.springframework.http.HttpHeaders;import org.springframework.http.MediaType;HttpHeaders headers = new HttpHeaders ();headers.setContentType(MediaType.APPLICATION_JSON); headers.setContentLength(1024 ); headers.setCacheControl("max-age=3600" );
提供一种根据文件名确定资源的 MediaType(MIME 类型)的方法。
1 2 3 4 5 6 7 import org.springframework.http.MediaType;import org.springframework.http.MediaTypeFactory;import java.util.Optional;Optional<MediaType> mediaType = MediaTypeFactory.getMediaType("document.pdf" );
41. MimeTypeUtils 包含常见 MIME 类型的常量,并提供处理它们的实用方法。
1 2 3 4 5 6 import org.springframework.util.MimeTypeUtils;MimeType a = MimeTypeUtils.APPLICATION_JSON;MimeType b = MimeType.valueOf("application/*" );boolean isCompatible = a.isCompatibleWith(b);
42. WebClient.Builder 创建 WebClient(Spring 的现代非阻塞 HTTP 客户端)实例的主要工具。
1 2 3 4 5 6 7 import org.springframework.web.reactive.function.client.WebClient;WebClient webClient = WebClient.builder() .baseUrl("https://api.example.com" ) .defaultHeader("Authorization" , "Bearer my-token" ) .build();
43. PropertySourceUtils 一个辅助工具,用于处理环境中所包含的 PropertySource对象中的属性值。
1 2 3 4 5 6 7 8 9 import org.springframework.core.env.ConfigurableEnvironment;import org.springframework.core.env.MapPropertySource;import org.springframework.core.env.PropertySource;Map<String, Object> myProps = new HashMap <>(); myProps.put("app.name" , "MyApp" ); PropertySource<?> myPropertySource = new MapPropertySource ("my-properties" , myProps); environment.getPropertySources().addFirst(myPropertySource);
44. ApplicationEventPublisher Spring 中用于在应用程序上下文中发布事件的核心接口,支持解耦的事件驱动架构。
1 2 3 4 5 6 7 8 9 10 import org.springframework.context.ApplicationEventPublisher;import org.springframework.beans.factory.annotation.Autowired;@Autowired private ApplicationEventPublisher eventPublisher;public void doSomething () { MyCustomEvent event = new MyCustomEvent (this , "Something important happened" ); eventPublisher.publishEvent(event); }
45. LocaleContextHolder 保存当前线程的 Locale,对于国际化 (i18n) 至关重要。
1 2 3 4 5 6 7 import org.springframework.context.i18n.LocaleContextHolder;import java.util.Locale;Locale currentLocale = LocaleContextHolder.getLocale();LocaleContextHolder.setLocale(Locale.FRENCH);
46. AopUtils 一组用于处理 AOP 代理的静态实用方法。
1 2 3 4 5 6 7 8 import org.springframework.aop.support.AopUtils;boolean isAopProxy = AopUtils.isAopProxy(myBean);boolean isCglibProxy = AopUtils.isCglibProxy(myBean);Class<?> targetClass = AopUtils.getTargetClass(myBean);
47. ProxyFactory 一种以编程方式创建 AOP 代理的方法,使你可以细粒度地控制应用哪些接口和建议。
1 2 3 4 5 6 import org.springframework.aop.framework.ProxyFactory;ProxyFactory factory = new ProxyFactory (myTargetObject);factory.addAdvice(new MyMethodInterceptor ()); MyInterface proxy = (MyInterface) factory.getProxy();
48. ClassPathScanningCandidateComponentProvider 一个强大的工具,用于扫描类路径以查找符合特定条件的组件(通常由过滤器定义)。
1 2 3 4 5 6 7 8 9 10 import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;import org.springframework.core.type.filter.AnnotationTypeFilter;import org.springframework.beans.factory.config.BeanDefinition;import java.util.Set;ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider (true ); scanner.addIncludeFilter(new AnnotationTypeFilter (Component.class)); Set<BeanDefinition> components = scanner.findCandidateComponents("com.example.myapp" );
49. YamlPropertiesFactoryBean 一个工厂 Bean,可以加载 YAML 文件并将其转换为 Properties对象。
1 2 3 4 5 6 7 8 9 import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;import org.springframework.core.io.ClassPathResource;import java.util.Properties;YamlPropertiesFactoryBean yamlFactory = new YamlPropertiesFactoryBean ();yamlFactory.setResources(new ClassPathResource ("application.yml" )); Properties properties = yamlFactory.getObject();String appName = properties.getProperty("spring.application.name" );