断言: 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. Why Microservices ?

    微服务(Microservices)是一种软件架构设计风格,其中应用程序由一组小型.独立.自治的服务组成,这些服务共同工作以构建整体应用.每个服务都专注于一个特定的业务功能,可以独立部署.扩展和维护. ...

  2. BAPI_ACC_DOCUMENT_POST 解决多行一次性供应商凭证导入问题

    POST 函数将一次性供应商 的信息放在抬头入参上,业务需要多个一次性供应商一起做凭证时,就满足不了. 抬头入参会把所有行的一次性给一样的名称. 想起之前做的 IF_EX_ACC_DOCUMENT~C ...

  3. 数据结构(C语言版 第2版)课后习题答案全集 严蔚敏

    有的小伙伴在网上奋力的找 严蔚敏版 第2版 数据结构 的始终无果,那么我这里就整理好,放在同名公众号中了,也可扫文章末尾的二维码直达公众号,回复数据结构的关键词即可拿到.

  4. 如何在 PyQt 中实现异步数据库请求

    需求 开发软件的时候不可避免要和数据库发生交互,但是有些 SQL 请求非常耗时,如果在主线程中发送请求,可能会造成界面卡顿.这篇博客将会介绍一种让数据库请求变得和前端的 ajax 请求一样简单,且不会 ...

  5. SpringBoot对象拷贝

    目录 概述 定义实体类 Car size carInfo 造测试数据 Spring BeanUtils Apache BeanUtils Cglib BeanCopier MapStruct 性能测试 ...

  6. 强!推荐一款自动化神器Autolt:不再重复工作

    随着互联网不断发展,它给我们带来便利的同时,也带来了枯燥.重复.机械的重复工作.今天,我要和大家分享一款老牌实用的自动化工具:AutoIt,它能够让你告别繁琐的重复性工作,提高工作效率. 这里透露一下 ...

  7. 【Python】【OpenCV】【NumPy】图像和原始字节的转换

    学习完基础的图像算法,开始接触OpenCV学习: 灰度图中,一个像素点上的灰度级需要一个字节(byte,2^8,8 bit)进行存储,此时的灰度图是二维的.而当我们需要转换为彩色图时,即三维,便会产生 ...

  8. Odoo接口说明

    通过使用PostMan调用Odoo - POST { "jsonrpc":"2.0", "params":{ "db": ...

  9. 技本功|Hive优化之监控(三)

    Hive是大数据领域常用的组件之一,主要是大数据离线数仓的运算,关于Hive的性能调优在日常工作和面试中是经常涉及的一个点,因此掌握一些Hive调优是必不可少的技能.影响Hive效率的主要有数据倾斜. ...

  10. 【csharp】抽象类与接口有哪些不同?什么时候应该使用抽象类?

    抽象类与接口有哪些不同? 抽象类和接口是在面向对象编程中两个不同的概念,它们有一些重要的区别.以下是抽象类和接口的主要不同点: 抽象类(Abstract Class): 成员类型: 抽象类可以包含抽象 ...