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

【优雅代码】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);
}
复制代码

Supongo que te gusta

Origin juejin.im/post/7049680768829751333
Recomendado
Clasificación