断言: 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. 七天.NET 8操作SQLite入门到实战 - 第五天引入SQLite-net ORM并封装常用方法(SQLiteHelper)

    前言 上一章节我们搭建好了EasySQLite的前后端框架,今天我们的主要任务是在后端框架中引入SQLite-net ORM并封装常用方法(SQLiteHelper). 七天.NET 8操作SQLit ...

  2. .net中优秀依赖注入框架Autofac看一篇就够了

    Autofac 是一个功能丰富的 .NET 依赖注入容器,用于管理对象的生命周期.解决依赖关系以及进行属性注入.本文将详细讲解 Autofac 的使用方法,包括多种不同的注册方式,属性注入,以及如何使 ...

  3. 华企盾DSC导致Solidworks无法导出xls文件

    将Solidworks进程和Excel进程的OLE控制和启用虚拟重定向都关闭

  4. 使用 PostgreSQL 16.1 + Citus 12.1 作为多个微服务的分布式 Sharding 存储后端

    在本教程中,我们将使用 PostgreSQL 16.1 + Citus 12.1 作为多个微服务的存储后端,演示此类集群的样例设置和基本操作. Citus 12.1 实验环境设置 Docker 快速启 ...

  5. 『UniApp』uni-app-打包成App

    前言 大家好,我是 BNTang, 在上一节文章中,我给大家详细的介绍了如何将我开发好的项目打包为微信小程序并且发布到微信小程序商店 趁热打铁,在来一篇文章,给大家详细的介绍如何将项目打包成APP. ...

  6. 在macOS中搭建.NET MAUI开发环境

    @ 目录 准备 安装扩展 安装 .NET 安装工作负载 安装 Xcode 命令行工具 调试安卓应用 安装 JDK 安装 Android SDK 安装 Android 模拟器 安装模拟器 安装镜像 创建 ...

  7. 当创建一个ingress后,kubernetes会发什么?

    本文分享自华为云社区<当创建一个ingress后,kubernetes会发什么?>,作者:可以交个朋友. 一.Ingress概述 Ingress是一组路由转发规则合集,将集群内部服务通过7 ...

  8. flutter屏幕适配方案

    使用MediaQuery和比例因子 优点:使用简单,可以处理大多数情况下的屏幕适配需求. 缺点:需要手动计算比例因子,并且随着UI元素变得更加复杂和层次化(例如多层次列表或动画效果),使用此方法可能会 ...

  9. 云图说|图解制品仓库CodeArts Artifact

    摘要:制品仓库用于存放由源码编译生成的.可运行的二进制文件,重要作用是实现制品文件的可信存储,支撑软件开发活动. 本文分享自华为云社区<[云图说]第277期 图解制品仓库CodeArts Art ...

  10. AI如何提升10倍筛药效率?6月18日华为云携手中科院上海药物所揭开谜底

    摘要:6月18日,华为云TechWave全球技术峰会(人工智能&数据)围绕人工智能.大数据.数据库.华为云Stack等热点话题,携手来自全球的IT精英.技术大咖.先锋企业.合作伙伴共话前沿技术 ...