Spring 内置工具类总结

╰+哭是因爲堅強的太久メ 2022-10-29 03:23 295阅读 0赞

内置类搜索

可以通过进入IDE并在*Utils类搜索或*Utils.java文件搜索中进行搜索来找到它们
在这里插入图片描述

常用

让我们看看其中的一些。

  • BeanUtils -处理JavaBeans的有用功能
  • ClassUtils -有用的功能,用于反思性地询问类型
  • SystemPropertyUtils-处理System属性的有用功能
  • FileCopyUtils-用于复制InputStreamOutputStream实现的有用功能
  • AopUtils -处理Spring的AOP代理的有用功能
  • ReflectionUtils -广泛用于处理反思的有用功能
  • Assert -有用的功能,有助于按合同设计的断言
  • CollectionUtils-用于处理各种Javajava.util.Collection类型的有用函数
  • SerializeUtils -用于Java序列化的有用功能

    package bootiful.utils;

    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.extern.log4j.Log4j2;
    import org.springframework.aop.support.AopUtils;
    import org.springframework.beans.BeanUtils;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.context.event.ApplicationReadyEvent;
    import org.springframework.context.ApplicationListener;
    import org.springframework.context.annotation.Bean;
    import org.springframework.core.ResolvableType;
    import org.springframework.core.io.ClassPathResource;
    import org.springframework.core.io.Resource;
    import org.springframework.jmx.support.JmxUtils;
    import org.springframework.stereotype.Component;
    import org.springframework.util.*;

    import javax.annotation.PostConstruct;
    import java.beans.PropertyDescriptor;
    import java.io.InputStreamReader;
    import java.io.Reader;
    import java.io.Serializable;
    import java.lang.reflect.Constructor;
    import java.lang.reflect.Field;
    import java.util.*;

    @Log4j2
    @SpringBootApplication
    public class BootifulApplication {

    1. @Data
    2. @AllArgsConstructor
    3. @Component
    4. public static class DemoClass {
    5. @PostConstruct
    6. public void begin() {
    7. log.info("begin()");
    8. }
    9. private final List<Map<String, Object>> list = new ArrayList<>();
    10. }
    11. @Bean
    12. ApplicationListener<ApplicationReadyEvent> ready(DemoClass demo) {
    13. return event -> {
    14. Assert.notNull(demo.getList(), "the list can't be null");
    15. beansUtils(demo);
    16. classUtils();
    17. systemPropertyUtils();
    18. fileCopyUtils();
    19. aop(demo);
    20. reflection();
    21. ensure();
    22. collections();
    23. serialize();
    24. };
    25. }
    26. private void ensure() {
    27. int counter = 2;
    28. Assert.state(counter == 2, () -> "the counter should be 2 or more. Was " + counter);
    29. Assert.hasText("Hello, world!", () -> "this string should be a non-null, non-empty String");
    30. }
    31. private void reflection() {
    32. ReflectionUtils.doWithFields(DemoClass.class, field -> log.info("field = " + field.toString()));
    33. ReflectionUtils.doWithMethods(DemoClass.class, method -> log.info("method = " + method.toString()));
    34. Field list = ReflectionUtils.findField(DemoClass.class, "list");
    35. log.info(Objects.requireNonNull(list).toString());
    36. ResolvableType rt = ResolvableType.forField(list);
    37. log.info(rt.toString());
    38. }
    39. private void aop(DemoClass demoClass) {
    40. Class<?> targetClass = AopUtils.getTargetClass(demoClass);
    41. log.info("Class<?> is " + targetClass);
    42. log.info("is AOP proxy? " + AopUtils.isAopProxy(demoClass));
    43. log.info("is CGlib proxy? " + AopUtils.isCglibProxy(demoClass));
    44. }
    45. private void collections() {
    46. Collection<String> names = Arrays.asList("Tammie", "Kimly", "Josh");
    47. boolean contains = CollectionUtils.containsAny(names, Arrays.asList("Josh"));
    48. Assert.state(contains, () -> "one or more of the names in " + names.toString() + " should be present");
    49. }
    50. private void serialize() {
    51. Customer in = new Customer(593232329, "Josh");
    52. byte[] bytes = SerializationUtils.serialize(in);
    53. Customer out = (Customer) SerializationUtils.deserialize(bytes);
    54. Assert.state(out.getId() == in.getId() && out.getName().equals(in.getName()),
    55. () -> "the " + Customer.class.getName() + " did not serialize correctlyy");
    56. }
    57. private void fileCopyUtils() {
    58. Resource cpr = new ClassPathResource("/application.properties");
    59. try (Reader r = new InputStreamReader(cpr.getInputStream())) {
    60. String contents = FileCopyUtils.copyToString(r);
    61. log.info("application.properties contents: " + contents);
    62. }
    63. catch (Exception e) {
    64. throw new RuntimeException(e);
    65. }
    66. }
    67. private void systemPropertyUtils() {
    68. String resolvedText = SystemPropertyUtils.resolvePlaceholders("my home directory is ${user.home}");
    69. log.info("resolved text: " + resolvedText);
    70. }
    71. private void classUtils() {
    72. Constructor<DemoClass> demoClassConstructor = ClassUtils.getConstructorIfAvailable(DemoClass.class);
    73. log.info("demoClassConstructor: " + demoClassConstructor);
    74. try {
    75. DemoClass demoClass = demoClassConstructor.newInstance();
    76. log.info("newInstance'd demoClass: " + demoClass);
    77. }
    78. catch (Exception e) {
    79. throw new RuntimeException(e);
    80. }
    81. }
    82. private void beansUtils(DemoClass demo) {
    83. PropertyDescriptor[] descriptors = BeanUtils.getPropertyDescriptors(demo.getClass());
    84. for (PropertyDescriptor pd : descriptors) {
    85. log.info("pd: " + pd.getName());
    86. }
    87. }
    88. public static void main(String[] args) {
    89. SpringApplication.run(BootifulApplication.class, args);
    90. }

    }

    @Data
    class Customer implements Serializable {

    1. static final long serialVersionUID = 1L;
    2. private int id;
    3. private String name;
    4. public Customer(int id, String name) {
    5. this.id = id;
    6. this.name = name;
    7. }

    }

参考:
https://spring.io/blog/2021/01/27/ymnnalft-the-spring-utils-classes

发表评论

表情:
评论列表 (有 0 条评论,295人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Springboot工具之ObjectUtils

    在实际业务开发中,有时候经常需要判断对象是否为空、数组是否为空、两个对象是否相等,数组中是否包含某个元素,往数组中追加元素等这些操作,每次都手写太麻烦,然后很多人的选择是封装成