栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java 小工具 (三)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java 小工具 (三)

文章目录
      • 1.IOUtils
        • 1.1 读取文件
        • 1.2 写入文件
        • 1.3 文件拷贝
        • 1.4 读取文件内容到字节数组
      • 2.MDC
      • 3.ClassUtils
        • 3.1 获取对象的所有接口
        • 3.2 获取某个类的包名
        • 3.3 判断某个类是否内部类
        • 3.4 判断对象是否代理对象
      • 4.BeanUtils
        • 4.1 拷贝对象的属性
        • 4.2 实例化某个类
        • 4.3 获取指定类的指定方法
        • 4.4 获取指定方法的参数

1.IOUtils

org.apache.commons.io包下的IOUtils类

1.1 读取文件
String str = IOUtils.toString(new FileInputStream("/temp/a.txt"), StandardCharsets.UTF_8);
System.out.println(str);
1.2 写入文件
String str = "abcde";
IOUtils.write(str, new FileOutputStream("/temp/b.tx"), StandardCharsets.UTF_8);
1.3 文件拷贝
IOUtils.copy(new FileInputStream("/temp/a.txt"), new FileOutputStream("/temp/b.txt"));
1.4 读取文件内容到字节数组
byte[] bytes = IOUtils.toByteArray(new FileInputStream("/temp/a.txt"));

2.MDC

MDC: Mapped Diagnostic Context, 一个线程安全的存放诊断日志的容器。底层是用了ThreadLocal来保存数据的。

例如现在有这样一种场景:我们使用RestTemplate调用远程接口时,有时需要在header中传递信息,比如:traceId,source等,便于在查询日志时能够串联一次完整的请求链路,快速定位问题。

这种业务场景就能通过ClientHttpRequestInterceptor接口实现,具体做法如下:

第一步,定义一个LogFilter拦截所有接口请求,在MDC中设置traceId:

public class LogFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        MdcUtil.add(UUID.randomUUID().toString());
        System.out.println("记录请求日志");
        chain.doFilter(request, response);
        System.out.println("记录响应日志");
    }

    @Override
    public void destroy() {
    }
}

第二步,实现ClientHttpRequestInterceptor接口,MDC中获取当前请求的traceId,然后设置到header中:

public class RestTemplateInterceptor implements ClientHttpRequestInterceptor {

    @Override
    public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
        request.getHeaders().set("traceId", MdcUtil.get());
        return execution.execute(request, body);
    }
}

第三步,定义配置类,配置上面定义的RestTemplateInterceptor类:

@Configuration
public class RestTemplateConfiguration {

    @Bean
    public RestTemplate restTemplate() {
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.setInterceptors(Collections.singletonList(restTemplateInterceptor()));
        return restTemplate;
    }

    @Bean
    public RestTemplateInterceptor restTemplateInterceptor() {
        return new RestTemplateInterceptor();
    }
}

其中MdcUtil其实是利用MDC工具在ThreadLocal中存储和获取traceId

public class MdcUtil {

    private static final String TRACE_ID = "TRACE_ID";

    public static String get() {
        return MDC.get(TRACE_ID);
    }

    public static void add(String value) {
        MDC.put(TRACE_ID, value);
    }
}

能使用MDC保存traceId等参数的根本原因是,用户请求到应用服务器,Tomcat会从线程池中分配一个线程去处理该请求。

保存到MDC的ThreadLocal中的参数,也是该线程独享的,所以不会有线程安全问题。

3.ClassUtils

spring的org.springframework.util包下的ClassUtils类.

类和对象相关的方法

3.1 获取对象的所有接口
Class[] allInterfaces = ClassUtils.getAllInterfaces(new User());
3.2 获取某个类的包名
String packageName = ClassUtils.getPackageName(User.class);
System.out.println(packageName);
3.3 判断某个类是否内部类
System.out.println(ClassUtils.isInnerClass(User.class));
3.4 判断对象是否代理对象
System.out.println(ClassUtils.isCglibProxy(new User()));

4.BeanUtils 4.1 拷贝对象的属性
User user1 = new User();
user1.setId(1L);
user1.setName("苏三说技术");
user1.setAddress("成都");

User user2 = new User();
BeanUtils.copyProperties(user1, user2);
System.out.println(user2);
4.2 实例化某个类
User user = BeanUtils.instantiateClass(User.class);
System.out.println(user);
4.3 获取指定类的指定方法
Method declaredMethod = BeanUtils.findDeclaredMethod(User.class, "getId");
System.out.println(declaredMethod.getName());
4.4 获取指定方法的参数
Method declaredMethod = BeanUtils.findDeclaredMethod(User.class, "getId");
PropertyDescriptor propertyForMethod = BeanUtils.findPropertyForMethod(declaredMethod);
System.out.println(propertyForMethod.getName());

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/948380.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号