Utils工具类

Spring框架包含了一些Utils工具类,这些工具类被用于Spring框架的编写同时也开放了出来,我们开发中也可以使用。这篇笔记我们介绍一些常用的Utils工具类。

StringUtils

StringUtils我们肯定不陌生,各种工具包例如Apache的commons-lang3等都包含类似功能的工具类,Spring内置的StringUtils其实也很好用,大部分常用的方法都包含了。下面例子代码演示了判断字符串是否为空或null

package com.gacfox.demo.demoboot;

import org.springframework.util.StringUtils;

public class Main {
    public static void main(String[] args) {
        String s = "";
        if (StringUtils.hasText(s)) {
            System.out.println("字符串s不为空");
        }
    }
}

除此之外,StringUtils还有许多其它的用法,具体可以参考文档。

BeanUtils

BeanUtils最常见的用法是拷贝属性,下面是一个拷贝属性的例子。

package com.gacfox.demo.demoboot;

import lombok.Data;
import lombok.ToString;
import org.springframework.beans.BeanUtils;

@Data
@ToString
class StudentDto {
    private String id;
    private String name;
    private Integer age;
}

@Data
@ToString
class StudentVo {
    private String name;
    private Integer age;
}

public class Main {
    public static void main(String[] args) {
        StudentDto studentDto = new StudentDto();
        studentDto.setId("c3m1o2avk1");
        studentDto.setName("Tom");
        studentDto.setAge(18);

        StudentVo studentVo = new StudentVo();
        BeanUtils.copyProperties(studentDto, studentVo);

        System.out.println(studentVo);
    }
}

Base64Utils

Base64Utils用于Base64的编码和解码,不过由于Java8的JDK新增加了Base64编解码工具类,因此这个Spring版的Base64Utils其实就比较鸡肋了,不过对于Java7还是很有用的。

package com.gacfox.demo.demoboot;

import org.springframework.util.Base64Utils;

import java.nio.charset.StandardCharsets;

public class Main {
    public static void main(String[] args) {
        String s = "Hello, world!";
        String sEncoded = Base64Utils.encodeToString(s.getBytes(StandardCharsets.UTF_8));
        System.out.println(sEncoded);
    }
}

ClassUtils

ClassUtils用于获取一个Class对象的各种信息,比如它实现的接口、类名、类全限定名、类文件名等。

package com.gacfox.demo.demoboot;

import org.springframework.util.ClassUtils;

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        DemoComponent demoComponent = new DemoComponent();
        Class<?>[] interfaces = ClassUtils.getAllInterfaces(demoComponent);
        String qualifiedName = ClassUtils.getQualifiedName(demoComponent.getClass());
        String shortName = ClassUtils.getShortName(demoComponent.getClass());
        String classFileName = ClassUtils.getClassFileName(demoComponent.getClass());

        System.out.println(Arrays.toString(interfaces));
        System.out.println(qualifiedName);
        System.out.println(shortName);
        System.out.println(classFileName);
    }
}

AnnotationUtils

AnnotationUtils用于获取一个Class的注解信息,我们可以用这个工具判断类是否标注了一个注解并取值。

package com.gacfox.demo.demoboot;

import org.springframework.core.annotation.AnnotationUtils;

public class Main {
    public static void main(String[] args) {
        DemoAnnotation annotation = AnnotationUtils.findAnnotation(DemoComponent.class, DemoAnnotation.class);
        if (annotation != null) {
            System.out.println(annotation.value());
        }
    }
}

ReflectionUtils

ReflectionUtils是Spring对JDK的反射API进行的一些封装,其中包括获取类的方法等功能。

package com.gacfox.demo.demoboot;

import org.springframework.util.ReflectionUtils;

import java.lang.reflect.Method;
import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        Method method = ReflectionUtils.findMethod(DemoComponent.class, "sayHello");
        if (method != null) {
            System.out.println(Arrays.toString(method.getParameterTypes()));
            System.out.println(method.getReturnType());
        }
    }
}
作者:Gacfox
版权声明:本网站为非盈利性质,文章如非特殊说明均为原创,版权遵循知识共享协议CC BY-NC-ND 4.0进行授权,转载必须署名,禁止用于商业目的或演绎修改后转载。
Copyright © 2017-2024 Gacfox All Rights Reserved.
Build with NextJS | Sitemap