断言: 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. 快速下载vscode、git

    在官网下载vscode太慢,解决方式: http://vscode.cdn.azure.cn/stable/ea3859d4ba2f3e577a159bc91e3074c5d85c0523/VSCod ...

  2. 【WCH以太网接口系列芯片】基于CH395的组播请求(IGMP)

    在上一篇文章中,我们通过直连电脑测试了CH395在组播环境中进行数据的收发,但在实际的使用场景中更多的是将CH395接入局域网环境中.因此,我们需要使用到一个协议--IGMP(Internet Gro ...

  3. AR9271无线网卡Win10配置热点

    AR9271无线网卡Win10配置热点 需要的无线网卡如下图 1 准备工作 网卡参数 Atheros AR9271是一款高性能的无网络模块,采用802.11b/g/n标准,支持2.4GH频段.它被广泛 ...

  4. [CF403E]Two Rooted Trees

    Two Rooted Trees 题面翻译 题目描述 你有两棵有根树,每棵树都有 \(n\) 个结点.不妨将这两棵树上的点都用 \(1\) 到 \(n\) 之间的整数编号.每棵树的根结点都是 \(1\ ...

  5. Hive select查询语句

    创建表 CREATE TABLE t_usa_covid19( count_date string, county string, state string, fips int, cases int, ...

  6. MinIO客户端之rm

    MinIO提供了一个命令行程序mc用于协助用户完成日常的维护.管理类工作. 官方资料 mc rm 删除指定的对象. 准备待删除的对象,查看对象,命令如下: ./mc ls local1/bkt2/ 控 ...

  7. java实现一个录像大师

    java实现一个录像大师 javacv从入门到入土系列,发现了个好玩的东西,视频处理,于是我想搞了个屏幕录屏大师,这里我使用javafx进行页面显示. 依赖 <!-- 需要注意,javacv主要 ...

  8. jumpserver连接ecs实例报错:UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: ssh_exchange_identification: Connection closed by remote host", "unreachable": true

    报错分析思路: 1.是ssh密钥设置有没有对接 2.防火墙拦截问题 3.用户设置问题 4.sshd配置问题 问题解决: 无法与221.229.216.39端口35846进行协商:找不到匹配的主机密钥类 ...

  9. spring-mvc 系列:HttpMessageConverter(@RequestBody、RequestEntity、@ResponseBody、@RestController、ResponseEntity、文件上传下载)

    目录 一.@RequestBody 二.RequestEntity 三.@ResponseBody 四.SpringMVC处理json 五.@RestController 六.ResponseEnti ...

  10. 云小课|云小课带你玩转可视化分析ELB日志

    阅识风云是华为云信息大咖,擅长将复杂信息多元化呈现,其出品的一张图(云图说).深入浅出的博文(云小课)或短视频(云视厅)总有一款能让您快速上手华为云.更多精彩内容请单击此处. 云日志服务支持可视化查看 ...