解决@Scope(“prototype“)不生效
使用spring的时候,我们一般都是使用@Component实现bean的注入,这个时候我们的bean如果不指定@Scope,默认是单例模式,另外还有很多模式可用,用的最多的就是多例模式了,顾名思义就是每次使用都会创建一个新的对象,比较适用于写一些job,比如在多线程环境下可以使用全局变量之类的
创建一个测试任务,这里在网上看到大部分都是直接@Scope(“prototype”),这里测试是不生效的,再加上proxyMode才行,代码如下
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
@Component
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE, proxyMode = ScopedProxyMode.TARGET_CLASS)
public class TestAsyncClient {
private int a = 0;
@Async
public void test(int a) {
this.a = a;
CommonAsyncJobs.list.add(this.a + "");
}
}
测试
import cn.hutool.core.collection.CollectionUtil;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Set;
import java.util.Vector;
@Slf4j
@EnableAsync
@SpringBootTest
@RunWith(SpringRunner.class)
public class CommonAsyncJobs {
@Autowired
private TestAsyncClient testAsyncClient;
// 多线程环境下,普通的list.add不适用,用Vector处理就行了,效率低,但无所谓反正测试的
public static Vector<String> list = new Vector<>();
@Test
public void testAsync() throws Exception {
// 循环里面异步处理
int a = 100000;
for (int i = 0; i < a; i++) {
testAsyncClient.test(i);
}
System.out.println("多线程结果:" + list.size());
System.out.println("单线程结果:" + a);
Set<String> set = CollectionUtil.newHashSet();
Set<String> exist = CollectionUtil.newHashSet();
for (String s : list) {
if (set.contains(s)) {
exist.add(s);
} else {
set.add(s);
}
}
// 没重复的值,说明多线程环境下,全局变量没有问题
System.out.println("重复的值:" + exist.size());
System.out.println("重复的值:" + exist);
// 单元测试内主线程结束会终止子线程任务
Thread.sleep(Long.MAX_VALUE);
}
}
结果 没重复的值,说明多线程环境下,全局变量没有问题
上一篇:
IDEA上Java项目控制台中文乱码
