Skip to content

Commit

Permalink
Fix #23 bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
yangtu222 committed Jun 5, 2019
1 parent bfc15c2 commit 570a336
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ This BeanUtils library is a Java bean copy utility with powerful functionality a
<dependency>
<groupId>com.github.yangtu222</groupId>
<artifactId>BeanUtils</artifactId>
<version>1.0.10</version>
<version>1.0.11</version>
</dependency>
~~~

Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<groupId>com.github.yangtu222</groupId>
<artifactId>BeanUtils</artifactId>
<name>BeanUtils</name>
<version>1.0.10</version>
<version>1.0.11</version>
<description>BeanUtils library is a Java bean copy utility with powerful functionality and high performance.</description>
<url>https://github.com/yangtu222/BeanUtils</url>

Expand All @@ -27,7 +27,7 @@
<connection>scm:git:https://github.com/yangtu222/BeanUtils.git</connection>
<developerConnection>scm:git:https://github.com/yangtu222/BeanUtils.git</developerConnection>
<url>https://github.com/yangtu222/BeanUtils.git</url>
<tag>v1.0.9</tag>
<tag>v1.0.11</tag>
</scm>

<profiles>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,25 +104,32 @@ public static BeanCopier getBeanCopy(Class<?> sourceClass, Class<?> targetClass,
return beanCopy;

synchronized (BeanCopyCache.class) {

if( beanCopyFactory == null ) {
try {
beanCopyFactory = beanCopyConfig.getBeanCopyFactory().newInstance();
} catch (Exception e) {
throw new BeanCopyException("BeanCopyConfig is not configured correctly!");
}
}
}

CopyFeature[] features = parseBeanCopyFeatures(sourceClass, targetClass, optionClass);
List<BeanCopyPropertyItem> itemList = buildBeanCopyPropertyItem(sourceClass, targetClass, optionClass);
beanCopy = beanCopyFactory.createBeanCopier(sourceClass, targetClass, itemList, features);
if( beanCopy != null ) {
beanCopyCacheMap.put(cacheKey, new SoftReference<BeanCopier>(beanCopy));
}
if( BeanCopyConfig.instance().getDumpOption() == BeanCopyConfig.DumpOption.AutoDumpAtFirstCopy ) {
BeanCopyDump.dumpPropertyMapping(sourceClass, targetClass, optionClass, itemList);
refBeanCopy = beanCopyCacheMap.get(cacheKey);
if( refBeanCopy != null )
beanCopy = refBeanCopy.get();
if( beanCopy != null )
return beanCopy;

CopyFeature[] features = parseBeanCopyFeatures(sourceClass, targetClass, optionClass);
List<BeanCopyPropertyItem> itemList = buildBeanCopyPropertyItem(sourceClass, targetClass, optionClass);
beanCopy = beanCopyFactory.createBeanCopier(sourceClass, targetClass, itemList, features);
if( beanCopy != null ) {
beanCopyCacheMap.put(cacheKey, new SoftReference<BeanCopier>(beanCopy));
}
if( BeanCopyConfig.instance().getDumpOption() == BeanCopyConfig.DumpOption.AutoDumpAtFirstCopy ) {
BeanCopyDump.dumpPropertyMapping(sourceClass, targetClass, optionClass, itemList);
}
}

return beanCopy;
}

Expand Down
14 changes: 14 additions & 0 deletions src/test/java/com/tuyang/test/testMultiThread/FromBean.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.tuyang.test.testMultiThread;

public class FromBean {

private String id;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

package com.tuyang.test.testMultiThread;

import com.tuyang.beanutils.BeanCopyConvertor;

public class StringToIntegerConverter implements BeanCopyConvertor<String,Integer> {

@Override
public Integer convertTo(String object) {
return Integer.parseInt(object);
}
}
32 changes: 32 additions & 0 deletions src/test/java/com/tuyang/test/testMultiThread/TestMultiThread.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.tuyang.test.testMultiThread;

import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

import com.tuyang.beanutils.BeanCopyUtils;

public class TestMultiThread {

@Test
public void testMultiThread() {
List<Thread> threads = new ArrayList<>();
for (int i=0;i<10;i++){
threads.add(new Thread(()->{
FromBean fromBean = new FromBean();
fromBean.setId("1");
ToBean toBean = BeanCopyUtils.copyBean(fromBean, ToBean.class);
System.out.println(toBean.getId());
} ));
}
threads.forEach(e->e.start());
threads.forEach(e-> {
try {
e.join();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
});
}
}
19 changes: 19 additions & 0 deletions src/test/java/com/tuyang/test/testMultiThread/ToBean.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.tuyang.test.testMultiThread;

import com.tuyang.beanutils.annotation.BeanCopySource;
import com.tuyang.beanutils.annotation.CopyProperty;

@BeanCopySource(source=FromBean.class)
public class ToBean {

@CopyProperty(property="id", convertor=StringToIntegerConverter.class)
private Integer id;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}
}

0 comments on commit 570a336

Please sign in to comment.