使用火焰图分析org.apache.commons.beanutils.BeanUtils

Java性能优化:StringBuffer、反射与BeanUtils.copyProperties的瓶颈分析
本文通过JavaFlightRecorder分析了测试程序的性能瓶颈,发现StringBuffer.append、isReadable/isWriteable以及getSimpleProperty方法调用耗时较长。分析指出,频繁的字符串拼接、反射判断可读写性以及获取属性读方法导致了性能下降。优化建议包括缓存可读写性判断结果和减少不必要的日志输出。

一、Java Flight Recorder

参考:https://blog.youkuaiyun.com/qq_37552993/article/details/102545780
配置之后让测试程序运行五分钟,得到下面这张图
ps:所copy的对象只有俩字段
在这里插入图片描述

二、分析耗时原因

2.1 isReadable isWriteable

每次copy时会对每个字段的可读性和可写性进行重复判断,而由于在复制过程中类的结构一般不会改变,所以这俩状态应该被缓存下来。

结论来源:https://blog.youkuaiyun.com/u010209217/article/details/84837821

通过反射方式获取可读性和可写性

isPackageAccessible:195, ReflectUtil (sun.reflect.misc)
get:71, MethodRef (java.beans)
getReadMethod:207, PropertyDescriptor (java.beans)
getReadMethod:1256, PropertyUtilsBean (org.apache.commons.beanutils)
isReadable:1435, PropertyUtilsBean (org.apache.commons.beanutils)
copyProperties:281, BeanUtilsBean (org.apache.commons.beanutils)
copyProperties:137, BeanUtils (org.apache.commons.beanutils)
copyProperties:8, CommonsBeanUtilsPropertiesCopier (com.test.BeanutilsTest.copier.Impl)
CommonsBeanUtilsPropertiesCopierTest:25, CopierShowError (com.test.BeanutilsTest)
main:38, CopierShowError (com.test.BeanutilsTest)
isPackageAccessible:195, ReflectUtil (sun.reflect.misc)
getBeanInfo:164, Introspector (java.beans)
getPropertyDescriptors:980, PropertyUtilsBean (org.apache.commons.beanutils)
getPropertyDescriptors:1084, PropertyUtilsBean (org.apache.commons.beanutils)
getPropertyDescriptor:912, PropertyUtilsBean (org.apache.commons.beanutils)
isWriteable:1523, PropertyUtilsBean (org.apache.commons.beanutils)
copyProperties:281, BeanUtilsBean (org.apache.commons.beanutils)
copyProperties:137, BeanUtils (org.apache.commons.beanutils)
copyProperties:8, CommonsBeanUtilsPropertiesCopier (com.test.BeanutilsTest.copier.Impl)
CommonsBeanUtilsPropertiesCopierTest:25, CopierShowError (com.test.BeanutilsTest)
main:38, CopierShowError (com.test.BeanutilsTest)

2.2 StringBuffer.append

输出了大量的日志调试信息

结论来源:https://segmentfault.com/a/1190000019356477

append:262, StringBuffer (java.lang)
convert:1077, BeanUtilsBean (org.apache.commons.beanutils)
copyProperty:437, BeanUtilsBean (org.apache.commons.beanutils)
copyProperties:286, BeanUtilsBean (org.apache.commons.beanutils)
copyProperties:137, BeanUtils (org.apache.commons.beanutils)
copyProperties:8, CommonsBeanUtilsPropertiesCopier (com.test.BeanutilsTest.copier.Impl)
CommonsBeanUtilsPropertiesCopierTest:25, CopierShowError (com.test.BeanutilsTest)
main:38, CopierShowError (com.test.BeanutilsTest)

2.3、getSimpleProperty

与isReadable类似,此函数内调用了getReadMethod以获取读方法,在getReadMethod又进一步调用反射方法,所以性能低。

isPackageAccessible:195, ReflectUtil (sun.reflect.misc)
get:71, MethodRef (java.beans)
getReadMethod:207, PropertyDescriptor (java.beans)
getReadMethod:1256, PropertyUtilsBean (org.apache.commons.beanutils)
getSimpleProperty:1325, PropertyUtilsBean (org.apache.commons.beanutils)
copyProperties:284, BeanUtilsBean (org.apache.commons.beanutils)
copyProperties:137, BeanUtils (org.apache.commons.beanutils)
copyProperties:8, CommonsBeanUtilsPropertiesCopier (com.test.BeanutilsTest.copier.Impl)
CommonsBeanUtilsPropertiesCopierTest:25, CopierShowError (com.test.BeanutilsTest)
main:38, CopierShowError (com.test.BeanutilsTest)

这个函数中获取的读方法实际上已存在于descriptorsCache这个缓存中,但是需要通过反射进一步校验访问权限

在这里插入图片描述校验访问权限。

    Method get() {
        if (this.methodRef == null) {
            return null;
        }
        Method method = this.methodRef.get();
        if (method == null) {
            method = find(this.typeRef.get(), this.signature);
            if (method == null) {
                this.signature = null;
                this.methodRef = null;
                this.typeRef = null;
                return null;
            }
            this.methodRef = new SoftReference<>(method);
        }
        return isPackageAccessible(method.getDeclaringClass()) ? method : null;// 若有权限,返回方法,反之返回null
    }

三、总结

通过火焰图发现StringBuffer.append isReadable isWriteable getSimpleProperty调用时间长,进一步分析发现,耗时问题可以被分为两类,一类是字符串拼接,一类是反射方法isPackageAccessible的调用。

除了上述方法之外,还有很多其他方法也调用了isPackageAccessible
在这里插入图片描述

end

<think> 我们正在解决用户关于`org.apache.commons.beanutils.BeanUtils`依赖的问题。用户提到自己下载了jar包,但运行时出现了`java.lang.NoClassDefFoundError`。这通常意味着编译时找到了类,但运行时类路径(classpath)中缺少相应的jar包。 根据用户提供的引用[1]和[2],问题可能涉及到两个不同的Apache Commons组件: - `commons-beanutils`: 包含`BeanUtils`类 - `commons-lang`: 包含`NestableRuntimeException`类(在引用[2]中缺失) 用户遇到的错误是: 1. 编译通过,但运行时报`java.lang.NoClassDefFoundError: org/apache/commons/beanutils/BeanUtils` -> 说明运行时缺少`commons-beanutils`的jar。 2. 另外引用[2]提到`org.apache.commons.lang.exception.NestableRuntimeException`找不到,这属于`commons-lang`。 因此,我们需要确保两个依赖都正确引入。 ### 解决方案 #### 1. Maven项目 如果是Maven项目,在`pom.xml`中添加以下依赖: ```xml <dependencies> <!-- BeanUtils 核心依赖 --> <dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.9.4</version> </dependency> <!-- 可选的,如果还需要commons-lang --> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version> </dependency> </dependencies> ``` #### 2. 手动添加JAR 如果手动管理JAR,需要下载以下两个JAR包并添加到项目的类路径中: - [commons-beanutils-1.9.4.jar](https://commons.apache.org/proper/commons-beanutils/download_beanutils.cgi) - [commons-lang-2.6.jar](https://commons.apache.org/proper/commons-lang/download_lang.cgi) 注意:`commons-beanutils`还依赖`commons-logging`,所以可能还需要下载[commons-logging](https://commons.apache.org/proper/commons-logging/download_logging.cgi)。 #### 3. 解决类路径问题 确保下载的JAR包已经正确添加到项目的类路径中。在IDE(如Eclipse或IntelliJ IDEA)中,需要将JAR包添加到项目的构建路径(Build Path)中。 ### 验证 添加依赖后,尝试运行以下测试代码: ```java import org.apache.commons.beanutils.BeanUtils; public class TestBeanUtils { public static void main(String[] args) { try { // 创建一个测试对象 Object object = new Object(); // 使用BeanUtils的简单方法,比如复制属性(这里仅测试类加载) System.out.println("BeanUtils loaded successfully!"); } catch (Exception e) { e.printStackTrace(); } } } ``` ### 常见问题 1. **版本冲突**:如果项目中其他依赖使用了不同版本的`commons-beanutils`或`commons-lang`,可能需要排除冲突的版本。 2. **依赖传递**:如果使用Maven,注意依赖传递可能会引入多个版本,可以通过`<exclusions>`排除。 ### 引用说明 在引用[2]中提到的`ClassNotFoundException: org.apache.commons.lang.exception.NestableRuntimeException`,这个类实际上在`commons-lang`的较老版本(2.x)中。注意,在`commons-lang3`中,包名改为`org.apache.commons.lang3`,且移除了`NestableRuntimeException`。因此,如果你使用的是`commons-lang3`,那么需要确保代码中引用的包名正确。 如果项目已经升级到`commons-lang3`,但是某个依赖仍然需要`commons-lang`(老版本),则可以同时引入两个依赖,因为它们的包名不同。 ### 总结 - 确保`commons-beanutils`的JAR在运行时类路径中。 - 根据错误提示,可能还需要`commons-lang`(老版本)的JAR。 - 使用构建工具(如Maven)可以自动处理依赖关系,避免手动管理。 §§相关问题§§ 1. 如何解决Java中的NoClassDefFoundError错误? 2. Maven项目中如何排除传递依赖? 3. commons-lang和commons-lang3有什么区别? 4. 如何在IntelliJ IDEA中添加外部JAR包? </think>### `org.apache.commons.beanutils.BeanUtils`依赖信息 #### 1. **Maven依赖配置** ```xml <dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.9.4</version> </dependency> ``` **最新版本**:截至2023年,最新稳定版为`1.9.4`([官方仓库](https://mvnrepository.com/artifact/commons-beanutils/commons-beanutils)) --- #### 2. **Gradle依赖配置** ```groovy implementation 'commons-beanutils:commons-beanutils:1.9.4' ``` --- ### 常见问题解决方案 #### 问题1:`NoClassDefFoundError: BeanUtils` **原因**:运行时缺少依赖或依赖冲突 **解决**: 1. 检查是否包含所有传递依赖: ```xml <!-- 必须同时引入这些依赖 --> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>3.2.2</version> </dependency> ``` 2. 使用`mvn dependency:tree`检查版本冲突 --- #### 问题2:`ClassNotFoundException: NestableRuntimeException` **原因**:老版本`commons-lang`缺失(引用[2]) **解决方案**: ```xml <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version> <!-- 注意不是lang3 --> </dependency> ``` --- ### 替代方案(避免依赖问题) #### 使用Spring BeanWrapper ```java import org.springframework.beans.BeanWrapper; import org.springframework.beans.BeanWrapperImpl; Map<String, Object> map = ...; Object target = new YourBean(); BeanWrapper wrapper = new BeanWrapperImpl(target); map.forEach((key, value) -> { if (wrapper.isWritableProperty(key)) { wrapper.setPropertyValue(key, value); } }); ``` **优势**:无需额外依赖(Spring项目自带) --- ### 完整使用示例 ```java import org.apache.commons.beanutils.BeanUtils; public class User { private String name; private int age; // getters/setters } public static void main(String[] args) { Map<String, Object> map = new HashMap<>(); map.put("name", "John"); map.put("age", 30); User user = new User(); BeanUtils.populate(user, map); // 自动注入属性 } ``` > **注意**: > 1. Map的key必须与对象属性名**严格匹配**(包括大小写) > 2. 基本类型需手动转换(如String→int) > 3. 推荐使用`BeanUtils.copyProperties()`避免类型问题
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值