内置类搜索
可以通过进入IDE并在*Utils
类搜索或*Utils.java
文件搜索中进行搜索来找到它们
常用
让我们看看其中的一些。
BeanUtils
-处理JavaBeans的有用功能ClassUtils
-有用的功能,用于反思性地询问类型SystemPropertyUtils
-处理System
属性的有用功能FileCopyUtils
-用于复制InputStream
和OutputStream
实现的有用功能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 {
@Data
@AllArgsConstructor
@Component
public static class DemoClass {
@PostConstruct
public void begin() {
log.info("begin()");
}
private final List<Map<String, Object>> list = new ArrayList<>();
}
@Bean
ApplicationListener<ApplicationReadyEvent> ready(DemoClass demo) {
return event -> {
Assert.notNull(demo.getList(), "the list can't be null");
beansUtils(demo);
classUtils();
systemPropertyUtils();
fileCopyUtils();
aop(demo);
reflection();
ensure();
collections();
serialize();
};
}
private void ensure() {
int counter = 2;
Assert.state(counter == 2, () -> "the counter should be 2 or more. Was " + counter);
Assert.hasText("Hello, world!", () -> "this string should be a non-null, non-empty String");
}
private void reflection() {
ReflectionUtils.doWithFields(DemoClass.class, field -> log.info("field = " + field.toString()));
ReflectionUtils.doWithMethods(DemoClass.class, method -> log.info("method = " + method.toString()));
Field list = ReflectionUtils.findField(DemoClass.class, "list");
log.info(Objects.requireNonNull(list).toString());
ResolvableType rt = ResolvableType.forField(list);
log.info(rt.toString());
}
private void aop(DemoClass demoClass) {
Class<?> targetClass = AopUtils.getTargetClass(demoClass);
log.info("Class<?> is " + targetClass);
log.info("is AOP proxy? " + AopUtils.isAopProxy(demoClass));
log.info("is CGlib proxy? " + AopUtils.isCglibProxy(demoClass));
}
private void collections() {
Collection<String> names = Arrays.asList("Tammie", "Kimly", "Josh");
boolean contains = CollectionUtils.containsAny(names, Arrays.asList("Josh"));
Assert.state(contains, () -> "one or more of the names in " + names.toString() + " should be present");
}
private void serialize() {
Customer in = new Customer(593232329, "Josh");
byte[] bytes = SerializationUtils.serialize(in);
Customer out = (Customer) SerializationUtils.deserialize(bytes);
Assert.state(out.getId() == in.getId() && out.getName().equals(in.getName()),
() -> "the " + Customer.class.getName() + " did not serialize correctlyy");
}
private void fileCopyUtils() {
Resource cpr = new ClassPathResource("/application.properties");
try (Reader r = new InputStreamReader(cpr.getInputStream())) {
String contents = FileCopyUtils.copyToString(r);
log.info("application.properties contents: " + contents);
}
catch (Exception e) {
throw new RuntimeException(e);
}
}
private void systemPropertyUtils() {
String resolvedText = SystemPropertyUtils.resolvePlaceholders("my home directory is ${user.home}");
log.info("resolved text: " + resolvedText);
}
private void classUtils() {
Constructor<DemoClass> demoClassConstructor = ClassUtils.getConstructorIfAvailable(DemoClass.class);
log.info("demoClassConstructor: " + demoClassConstructor);
try {
DemoClass demoClass = demoClassConstructor.newInstance();
log.info("newInstance'd demoClass: " + demoClass);
}
catch (Exception e) {
throw new RuntimeException(e);
}
}
private void beansUtils(DemoClass demo) {
PropertyDescriptor[] descriptors = BeanUtils.getPropertyDescriptors(demo.getClass());
for (PropertyDescriptor pd : descriptors) {
log.info("pd: " + pd.getName());
}
}
public static void main(String[] args) {
SpringApplication.run(BootifulApplication.class, args);
}
}
@Data
class Customer implements Serializable {
static final long serialVersionUID = 1L;
private int id;
private String name;
public Customer(int id, String name) {
this.id = id;
this.name = name;
}
}
参考:
https://spring.io/blog/2021/01/27/ymnnalft-the-spring-utils-classes