【优雅代码】07-spring下的优秀工具类

欢迎关注b站账号/公众号【六边形战士夏宁】,一个要把各项指标拉满的男人。该文章已在github目录收录。

屏幕前的大帅比大漂亮如果有帮助到你的话请顺手点个赞、加个收藏这对我真的很重要。别下次一定了,都不关注上哪下次一定。

1.反射相关(重要)

1.1背景

ReflectionUtils和AnnotationUtils,各种姿势进行反射,最关键是不用抛异常出去,相当舒心

1.2使用

@Nullable
private static void reflectionExample() {
// 各种反射姿势,可以躲过代码检查
Method reflectionExample = ReflectionUtils.findMethod(SpringExample.class, "reflectionExample");
System.out.println(reflectionExample);
Field testName = ReflectionUtils.findField(SpringExample.class, "testName");
ReflectionUtils.makeAccessible(testName);
System.out.println(testName);
Nullable annotation = AnnotationUtils.findAnnotation(reflectionExample, Nullable.class);
System.out.println(annotation);
}

2.cglib相关(重要)

2.1背景

cglib作为直接生成字节码,速度上不言而喻,而作为spring引入不用额外引入再次加分,以下介绍个人觉得用起来非常舒服的姿势(代理模式不介绍,直接用springAop即可)

2.2使用

/private static void cglibExample() {
// 注意cglib是对字节码操作,代理模式就不在这里介绍了,spring aop非常好用了,不过这个是spring带的cglib实际上不是spring的东西 // 创建不可变bean,简直太好用了,避免缓存被别人瞎改
SpringExample bean = new SpringExample();
bean.setTestName("hello");
SpringExample immutableBean = (SpringExample) ImmutableBean.create(bean);
// 下面这步会直接报错
// immutableBean.setTestName("123"); // 对象复制,目前最快的复制,第一个source,第二个target,如果要复制list需要自行循环
BeanCopier copier = BeanCopier.create(SpringExample.class, SpringExample.class, false);
SpringExample sourceBean = new SpringExample();
SpringExample targetBean = new SpringExample();
sourceBean.setTestName("123");
targetBean.setTestName("223");
copier.copy(sourceBean, targetBean, null);
System.out.println(targetBean);
// 注意第一步可以static缓存起来,BulkBean虽然可以处理复杂逻辑,但是个人认为复杂逻辑就老实写代码实现,用这个反而累赘 // 对象转map,可以重新封装,也可以直接用
Map<String, Object> map = new HashMap<>();
map.putAll(BeanMap.create(targetBean));
Map<String, Object> beanMap = BeanMap.create(targetBean);
System.out.println(map);
System.out.println(beanMap); // map转对象
SpringExample springExampleFinal = new SpringExample();
BeanMap.create(springExampleFinal).putAll(map);
System.out.println(springExampleFinal);
}

3.spring相关(重要)

3.1背景

列了4个比较好用的工具类,request非常棒,扩展的话可以在任意位置获取到用户,StopWatch在流程繁杂的方法中可以直观输出速度消耗的百分比,花板子,但是我很喜欢

3.2使用

private static void springExample() {
// 获取request
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
// 获取cookie
Cookie cookie = WebUtils.getCookie(request, "hello");
// 转义url
UriUtils.decode("", StandardCharsets.UTF_8);
UriUtils.encode("", StandardCharsets.UTF_8);
// 记录时间戳
StopWatch sw = new StopWatch("startTest");
sw.start("step 1");
sw.stop();
sw.start("step 2");
sw.stop();
System.out.println(sw.prettyPrint());
}

4.bean相关(重要)

4.1背景

列了3个使用姿势,应该是使用频率最高的3个

4.2使用

private void beanExample(){
// 获取bean
SpringExample bean = ac.getBean(SpringExample.class);
// 根据继承或实现获取bean
Map<String, SpringExample> beansOfType = ac.getBeansOfType(SpringExample.class);
// 获取当前代理对象,service层常用
AopContext.currentProxy();
}

5.assert相关

5.1背景

断言工具,可以直接抛异常,不用写trycatch,节省代码

5.2使用

private static void assertExample() {
Assert.notEmpty(new ArrayList<>());
}

6.其它

6.1背景

其它spring中的东西,基本都是下位替代,没其它姿势的时候可以勉强用一下

6.2使用

private void otherExample(){
// 其下有各种转义,用处有限
System.out.println(StringEscapeUtils.class);
// 资源加载工具类,但是不如springBoot注解好用
System.out.println(ResourceUtils.class);
// 读取properties,马马虎虎的东西,java自带的也不差
System.out.println(LocalizedResourceHelper.class);
// apache的IO包可太好用了,以及很多其它和apache重复的就不介绍了
System.out.println(FileCopyUtils.class);
}

最新文章

  1. Html文档流和文档对象模型DOM理解
  2. 树莓派3b+ 用samba与windows共享文件
  3. 一个参数大小写引发的uploadify报错 &quot;Syntax error, unrecognized expression: #&quot;
  4. 安装完MySQL数据库,在服务列表里找不到MySQL的解决办法
  5. While reading xxx.png pngcrush caught libpng error: Not a PNG file..
  6. jquery 分页控件(一)
  7. MySQL启动和关闭服务命令
  8. GCC基本知识
  9. careercup-链表 2.5
  10. Mysql相关操作
  11. Cocos2d-X学习之Ref类
  12. 04737_C++程序设计_第9章_运算符重载及流类库
  13. dataset 用法(3)
  14. WebService使用入门(包括发布服务,调用服务)
  15. 一次enq: TX - index contention等待事件处理
  16. hibernate09--连接查询
  17. spring bean的初始化以及销毁
  18. HDU 2048 神、上帝以及老天爷 【递推】【错排】
  19. 求1000以内的质数c语言
  20. js身份证校验

热门文章

  1. Mysql 常见报错和疑问汇总
  2. 【Linux】【Commands】文本查看类
  3. 【Linux】【Services】【Project】Cobbler自动化装机
  4. js--生成器总结
  5. 『与善仁』Appium基础 — 20、Appium元素定位
  6. CSS伪类选择器实现三角形
  7. Apache log4j2-RCE 漏洞复现(CVE-2021-44228)
  8. 减轻内存负担,在 pymysql 中使用 SSCursor 查询结果集较大的 SQL
  9. C++ happens-before 关系是不可传递的
  10. Python+Robot Framework实现UDS诊断自动化测试