1 Star 0 Fork 7

samuelfuck / abc

forked from 黄亿华 / abc 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

ABC

Another Bean Copier.

本来想自己用反射来做一个,因为cglib使用了动态字节码生成,所以效率比反射要高,参考了一些工具,决定直接在cglib的BeanCopier上改了

特点

本着为90%的使用场景,提供更便捷和直观的功能的思想,我又造了一个轮子。它有以下特点:

1. 创建新对象的功能

90%情况,我们都会在拷贝前创建对象。把两行代码变为一行代码,并且内聚性更高。

B b = new B();
BeanCopier.create(classFrom, classTo, false).copy(a,b,null);
B b = new SimpleBeanCopier(A.class,B.class).copy(a);

2. 性能更好

BeanCopy是一个比较常用的操作,我也自己通过反射实现了一个BeanCopy,经过调研,cglib的性能是最好的。

1,000,000 round
jdk set/get takes 17ms
cglib takes 117ms
abc(reflection version) takes 496ms
jodd takes 5309ms
apche beanutils takes 6264ms

3. 规划化业务

我们一般在BeanCopy有两种场景:

  1. 简单的field-copy,如无特殊需要,我们希望两个Bean字段名称相同
  2. 穿插业务逻辑,例如新对象的某些属性,需要额外的数据源取得

下面是用Spring实现一个带业务的BeanCopy的过程:

@Service
public class A2BBeanCopier extends BeanCopier<A,B> {

	@PostConstruct
	public void init(){
		setSourceClass(A.class);
		setTargetClass(B.class);
		super.init();
	}

	@Override
	public B postCopy(A source, B target) {
		target.setF5("aaa");
		return target;
	}
}

4. 兼容Guava的Function<F,T>

一个集合的转换可以变得非常简单:

BeanCopier<A, B> beanCopier = new SimpleBeanCopier<A, B>(A.class, B.class);
listB = Lists.transform(listA, beanCopier);
listA = Lists.transform(listB, beanCopier.reverse());

License

MIT License, see file LICENSE

The MIT License Copyright (c) 2009, 2010, 2011, 2012, 2013 Jonathan Hedley <jonathan@hedley.net> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

'A'nother 'B'ean 'C'opier. 展开 收起
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/samuelfuck/abc.git
git@gitee.com:samuelfuck/abc.git
samuelfuck
abc
abc
master

搜索帮助