断言: AssertUtils
assert 关键字在 JDK1.4 中引入,可通过 JVM 参数-enableassertions开启
SpringBoot 中提供了 Assert 断言工具类,通常用于数据合法性检查
Assert.notNull(obj, "当object为null时抛出异常");
Assert.IsTrue():测试指定的条件是否为True
Assert.isTrue(zqts.compareTo(BigDecimal.valueOf(45))==1,"最大垫款天数45天");
对象、数组、集合
ObjectUtils:
1.获取对象的基本信息 2.判断工具 3.其他工具方法
        System.out.println("判断数组是否为空=="+ ObjectUtils.isEmpty(list));
System.out.println("判断参数对象是否为空=="+ ObjectUtils.isEmpty(obj));
//向参数数组的末尾追加新元素,并返回一个新数组
//<A, O extends A> A[] addObjectToArray(A[] array, O obj)
System.out.println("向参数数组的末尾追加新元素=="+ ObjectUtils.addObjectToArray(list));
System.out.println("返回一个新数组并且转成list=="+ Arrays.toString(ObjectUtils.addObjectToArray(arr, "a")));
StringUtils:
1.字符串判断工具 2.字符串操作工具  3.路径相关工具方法
1.字符串以prefix开始*/
StringUtils.startsWith("sssdf","");//结果是:true
StringUtils.startsWith("sssdf","s");//结果是:true 2.字符串以prefix开始,不区分大小写*/
StringUtils.startsWithIgnoreCase("sssdf","Sssdf");//结果是:true 5.替换字符串:把text中的searchString替换成replacement,max是最大替换次数,默认是替换所有*/
StringUtils.replaceOnce("sshhhss","ss","p");//只替换一次-->结果是:phhhss
StringUtils.replace("sshhhs","ss","p");//全部替换--->结果是:phhhs
StringUtils.replace("sshhhsshss","ss","7777",2);//max:最大替换次数-->结果是:7777hhh7777hss 7.比较两个字符串是否相等,如果两个均为null,则也认为相等*/
StringUtils.equals("","");//结果是true
StringUtils.equalsIgnoreCase("ss","Ss");//不区分大小写--结果是true 8.返回searchChar在字符串中第一次出现的位置,如果searchChar没有在字符串中出现,则返回-1*/
StringUtils.indexOf("sdfsfsfdsf","f");/*结果是2*/
StringUtils.lastIndexOf("aFkyk","k");//结果是4//查找searchChar在字符串中最后一次出现的索引*/ 9.找出字符数组searChars第一次出现在字符串中的位置*/
StringUtils.indexOfAny("sdsfhhl0","f");//结果是3 12.去掉参数2在参数1开始部分共有的字符串*/
StringUtils.difference("灌灌灌灌","灌灌灌灌啊啊");//结果是:啊啊 13.查找,不区分大小写,没有找到返回-1*/
StringUtils.indexOfIgnoreCase("aFabbSSdd","f");//返回1 15.截取指定字符串之前的内容*/
StringUtils.substringBefore("dskeabcee","e");/*结果是:dskeabce*/
StringUtils.substringBeforeLast("dskeabcee","e");//一直找到最后一个指定的字符串/*结果是:dskeabce*/
StringUtils.substringAfter("dskeabcedeh",""); /*结果是:dskeabcedeh*/
StringUtils.substringAfterLast("dskeabcedeh","");/*结果是:*/ 16.截取参数2和参数3中间的字符*/
StringUtils.substringsBetween("dskeabcedeh","d","h");//以数组方式返回参数2和参数3中间的字符串 17.按不同类型进行分割字符串*/
StringUtils.splitByCharacterType("aa3444张三Bcss");/*结果是:[aa,3444,张三,a,B,css,B]*/

18.StringUtils.join()和String.join()用途:将数组或集合以某拼接符拼接到一起形成新的字符串。
list.add("qq");
list.add("aa");
list.add("bb");
String join = StringUtils.join(list,"-");//传入String类型的List集合,使用"-"号拼接
System.out.println(join);

String[] s = new String[]{"Yuan","Mxy"};//传入String类型的数组,使用"-"号拼接
String join2 = StringUtils.join(s,"-");
System.out.println(join2);
//结果:qq-aa-bb Yuan-Mxy
CollectionUtils / Collections:
1.集合判断工具 2.集合操作工具:
CollectionUtils.union(a, b);          //并集
CollectionUtils.intersection(a, b); //交集
CollectionUtils.disjunction(a, b); //交集的补集
CollectionUtils.subtract(a, b); //集合相减
CollectionUtils.isEmpty(Collection<?> collection); //集合是否为空
CollectionUtils.isEqualCollection(first,second); //集合是否相等
CollectionUtils.unmodifiableCollection(c); //不可修改的集合

Collections:
  • singleton(T o):返回一个包含单个元素的不可修改集合。

  • singletonList(T o):返回一个包含单个元素的不可修改列表。

  • emptyList():返回一个空的不可修改列表。

  • Collections.unmodifiableList(List<? extends T> list):返回指定列表的不可修改视图。
//判断两个集合是否相等
CollectionUtils.isEqualCollection(list1, list2);
//添加数组数据到集合
String[] strArray = {"aaa", "bbb", "ccc"};
List strList = new ArrayList();
CollectionUtils.addAll(strList, strArray);
//添加集合数据到另一个集合
List<String> list = Arrays.asList(new String[]{"AAA", "BBB", "CCC"});
List<String> list1 = new ArrayList<>();
CollectionUtils.addAll(list1, list.iterator());
//数组反转
String[] arr = new String[]{"1", "4", "6", "7"};
CollectionUtils.reverseArray(arr);
//返回每个元素出现的个数
Map<String,String> cardinalityMap = CollectionUtils.getCardinalityMap(list1);
//返回对象在集合中出现的次数
int cardinality = CollectionUtils.cardinality("7", Arrays.asList(arr));
System.out.println("==="+cardinality)
<!-- commons-collections4 和 commons-collections 2选1-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.4</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.1</version>
</dependency>
FileUtils,FileCopyUtils:
1.文件、资源、IO 流输入  2.文件、资源、IO 流输出
        //写文件,不存在则自动创建
FileUtils.write(new File("D:/ab/cxyapi.txt"), "程序换api","UTF-8",true); //向已存在file文件添加文字
List<String> lines=new ArrayList<>();
lines.add("欢迎访问:");lines.add("www.cxyapi.com");
FileUtils.writeLines(new File("D:/ab/cxyapi.txt"),lines,true); //向已存在file文件添加文字
FileUtils.writeStringToFile(new File("D:/ab/cxyapi.txt"), "作者:cxy", "UTF-8",true); //读文件
System.out.println(FileUtils.readFileToString(new File("D:/ab/cxyapi.txt"), "UTF-8"));
System.out.println(FileUtils.readLines(new File("D:/ab/cxyapi.txt"), "UTF-8")); //返回一个list //旧方法:FileCopyUtils
fileOutputStream = new FileOutputStream(newFilePath.toString());
fileOutputStream.write(content);
fileOutputStream.close(); //新方法:FileCopyUtils
FileCopyUtils.copy(content, file);
ResourceUtils:
 1.从资源路径获取文件 2.Resource
//Spring 提供了一个 ResourceUtils 工具类,它支持“classpath:”和“file:”的地址前缀,它能够从指定的地址加载文件资源
File jsonfile = ResourceUtils.getFile("classpath:city.json");
File file = ResourceUtils.getFile("classpath:test.txt");

StreamUtils:

1.输入流  
//StreamUtils.copy()直接将输入流复制带输出流
InputStream in = new FileInputStream(inputFileName);
OutputStream out = new FileOutputStream(outputFileName);
StreamUtils.copy(in, out);
//复制字符串到输出流
@Test
public void test_StreamUtils_copyString() throws IOException {
String string = "Should be copied to OutputStream.";
String outputFileName = "src/test/resources/output.txt";
File outputFile = new File(outputFileName);
OutputStream out = new FileOutputStream("src/test/resources/output.txt");
StreamUtils.copy(string, StandardCharsets.UTF_8, out);
assertTrue(outputFile.exists());
String outputFileContent = getStringFromInputStream(new FileInputStream(outputFileName));
assertEquals(outputFileContent, string);
}
//将输入流的内容复制到字符串
@Test
public void test_StreamUtils_copyStringFromInputStream() throws IOException {
String inputFileName = "src/test/resources/input.txt";
InputStream is = new FileInputStream(inputFileName);
String content = StreamUtils.copyToString(is, StandardCharsets.UTF_8);
String inputFileContent = getStringFromInputStream(new FileInputStream(inputFileName));
assertEquals(inputFileContent, content);
}
ReflectionUtils:
1.反射、AOP 2.执行方法 3.获取字段  4.设置字段
        //通过反射获取对象属性值
User userInfo = new User();
userInfo.setName("李白");
//找到Field对象(在缓存中获取对象)
Field field = ReflectionUtils.findField(User.class, "name");//获取某个类特定类型的字段值
assert field != null;
Object value = ReflectionUtils.getField(field, userInfo);
System.out.println(value); //根据方法的可见性,前缀名,入参个数,获取某个类的对应方法
Method[] getters = ReflectionUtils.getAllDeclaredMethods(User.class); //反射给对象的属性赋值
ReflectionUtils.setField(field, userInfo, "ccdd");
System.out.println("userInfo="+userInfo.toString());
AopUtils:
2.获取被代理对象的 class
//===============演示AopUtils==================

        // AopUtils.isAopProxy:是否是代理对象
System.out.println(AopUtils.isAopProxy(helloService)); // true
System.out.println(AopUtils.isJdkDynamicProxy(helloService)); // false
System.out.println(AopUtils.isCglibProxy(helloService)); // true // 拿到目标对象
System.out.println(AopUtils.getTargetClass(helloService)); //class com.fsx.service.HelloServiceImpl // selectInvocableMethod:方法@since 4.3 底层依赖于方法MethodIntrospector.selectInvocableMethod
// 只是在他技术上做了一个判断: 必须是被代理的方法才行(targetType是SpringProxy的子类,且是private这种方法,且不是static的就不行)
// Spring MVC的detectHandlerMethods对此方法有大量调用~~~~~
Method method = ClassUtils.getMethod(HelloServiceImpl.class, "hello");
System.out.println(AopUtils.selectInvocableMethod(method, HelloServiceImpl.class)); //public java.lang.Object com.fsx.service.HelloServiceImpl.hello() // 是否是equals方法
// isToStringMethod、isHashCodeMethod、isFinalizeMethod 都是类似的
System.out.println(AopUtils.isEqualsMethod(method)); //false // 它是对ClassUtils.getMostSpecificMethod,增加了对代理对象的特殊处理。。。
System.out.println(AopUtils.getMostSpecificMethod(method,HelloService.class));
AopContext:
1.获取当前对象的代理对象
场景: 在同一个类中,非事务方法A调用事务方法B,事务失效,得采用AopContext.currentProxy().xx()来进行调用,事务才能生效。
使用AopContxt.currentProxy()方法获取当前代理对象;
AopContext.currentProxy()使用ThreadLocal保存了代理对象,因此在A方法中使用【((Service) AopContext.currentProxy()).B()】就能解决切入失效的问题。

Spring自带的Objects等工具类(减少繁琐代码)的更多相关文章

  1. spring自带的MD5加密工具类

    Spring 自带的md5加密工具类,本来打算自己找一个工具类的,后来想起来Spring有自带的,就翻了翻 //导入包import org.springframework.util.DigestUti ...

  2. ShareIntentUtil【调用系统自带的分享的工具类】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 根据参考资料的文章,整理了调用系统自带分享的工具类(实现了适配7.0FileProvider的功能),需要搭配<Android ...

  3. Spring 注解(二)注解工具类 AnnotationUtils 和 AnnotatedElementUtils

    Spring 注解(二)注解工具类 AnnotationUtils 和 AnnotatedElementUtils Spring 系列目录(https://www.cnblogs.com/binary ...

  4. 获取Spring容器中Bean实例的工具类(Java泛型方法实现)

    在使用Spring做IoC容器的时候,有的类不方便直接注入bean,需要手动获得一个类型的bean. 因此,实现一个获得bean实例的工具类,就很有必要. 以前,写了一个根据bean的名称和类型获取b ...

  5. Spring 注解(二)注解工具类

    本文转载自Spring 注解(二)注解工具类 导语 首先回顾一下 AnnotationUtils 和 AnnotatedElementUtils 这两个注解工具类的用法: @Test @GetMapp ...

  6. Spring中内置的一些工具类

    学习Java的人,或者开发很多项目,都需要使用到Spring 这个框架,这个框架对于java程序员来说.学好spring 就不怕找不到工作.我们时常会写一些工具类,但是有些时候 我们不清楚,我们些的工 ...

  7. spring data redis jackson 配置,工具类

    spring data redis 序列化有jdk .jackson.string 等几种类型,自带的jackson不熟悉怎么使用,于是用string类型序列化,把对象先用工具类转成string,代码 ...

  8. Spring的Bean,AOP以及工具类初探

    1.Bean(Ioc) BeanWrapper 根据JavaDoc中的说明,BeanWrapper提供了设置和获取属性值(单个的或者是批量的),获取属性描述信息.查询只读或者可写属性等功能.不仅如此, ...

  9. 【spring】spirng中的常用工具类

    一.概述 很多时候,很多工具类其实spring中就已经提供,常用的工具类有: 参考:https://www.cnblogs.com/langtianya/p/3875103.html 内置的resou ...

  10. spring boot 使用redis 及redis工具类

    1-添加maven依赖 2-添加redis配置 3-工具类 1-添加maven依赖 实际上是封装了jedis <!-- redis 依赖--> <dependency> < ...

随机推荐

  1. Serializable是什么?为什么在Entity层要实现Serializable接口

    我在做房产信息管理系统时用到了Serializable接口 Serializable含义: 一个对象序列化的接口,一个类只有实现了Serializable接口,它的对象才能被序列化. Serializ ...

  2. AutoCAD ObjectARX 二次开发(2020版)--4,使用ARX向导创建CAD二次开发项目(编程框架)--

    手动创建ObjectARX应用程序非常麻烦,在此步骤中,将介绍ObjectARX向导. 在这里,我们将使用ObjectARX向导创建我们的ObjectARX应用程序. 本节的程序的需求是,接收CAD用 ...

  3. Docker 安装教程

    一.离线安装 一.CentOS 离线安装 一.下载地址 1.选择系统的型号,选择linux-CentOS 下载地址 2.上传文件到CentOS 服务器 #选择文件 rz 二.开始安装 1.解压压缩包 ...

  4. echarts设置多条折线不是你想的那样简单

    简单的多条折线图 小伙伴写过多条折线图的都知道, 常见的折线图是 xAxis 配置项下的 data属性上设置时间或者日期. series配置项下是对应的 legend中的数据以及该条折线的数据. &l ...

  5. 记录一个异常 Gradle打包项目Lombok不生效 No serializer found for class com.qbb.User and no properties discovered to create BeanSerializer......

    完整的错误: 03-Dec-2022 16:57:22.941 涓ラ噸 [http-nio-8080-exec-5] org.apache.catalina.core.StandardWrapperV ...

  6. 如何使用 Helm 在 K8s 上集成 Prometheus 和 Grafana|Part 1

    本系列将分成三个部分,您将学习如何使用 Helm 在 Kubernetes 上集成 Prometheus 和 Grafana,以及如何在 Grafana 上创建一个简单的控制面板.Prometheus ...

  7. 华企盾DSC客户端右键菜单不显示常见处理方法

    1.检查控制台"客户端不显示右键菜单项" 2.未分发模块权限,若以分配可尝试去掉重新分配模块 3.检查杀毒软件是否杀掉了5097目录的文件(覆盖安装,以上两条没问题,这条比较常见) ...

  8. Swagger2的接口配置

    Swagger2的接口配置 /** * Swagger2的接口配置 * * @author ruoyi */ @Configuration public class SwaggerConfig { / ...

  9. 终于卷完了!MySQL 打怪升级进阶成神之路(2023 最新版)!

    从第一篇文章开始,我们逐步详细介绍了 MySQL 数据库的基础知识,如:数据类型.存储引擎.性能优化(软.硬及sql语句),MySQL 数据库的高可用架构的部分,如:主从同步.读写分离的原理与实践.跨 ...

  10. 如何开发一个ORM数据库框架

    如何开发一个ORM框架 ORM(Object Relational Mapping)对象关系映射,ORM的数据库框架有hibernate,mybatis.我该如何开发一个类似这样的框架呢? 为什么会有 ...