【Java源码】双例模式与自定义作用域MyScope SimpleThreadScope
《spring自定义作用域实现》部分java源码:MyScope.javapackage com.javazx.po;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.config.Scope;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
/**
* @author: itmrchen
* @Description:双例模式-自定义作用域
* @date 2019/7/30 8:59
*/
public class MyScope implements Scope {
Map<String, Object> map1 = new ConcurrentHashMap<String, Object>(16);
Map<String, Object> map2 = new ConcurrentHashMap<String, Object>(16);
public Object get(String name, ObjectFactory<?> objectFactory) {
if (!map1.containsKey(name)) {
Object o = objectFactory.getObject();
map1.put(name, o);
return o;
}
if (!map2.containsKey(name)) {
Object o = objectFactory.getObject();
map2.put(name, o);
return o;
}
int i = new Random().nextInt(2);
if (i == 0) {
return map1.get(name);
} else {
return map2.get(name);
}
}
public Object remove(String name) {
if (map2.containsKey(name)) {
Object o = map2.get(name);
map2.remove(name);
return o;
}
if (map1.containsKey(name)) {
Object o = map1.get(name);
map1.remove(name);
return o;
}
return null;
}
public void registerDestructionCallback(String s, Runnable runnable) {
}
public Object resolveContextualObject(String s) {
return null;
}
public String getConversationId() {
return null;
}
}
spring.xml关键代码:
<!-- 自定义作用域 -->
<bean id="myScope" class="com.javazx.po.MyScope"/>
<bean id="simpleThreadScope" class="org.springframework.context.support.SimpleThreadScope"/>
<bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
<property name="scopes">
<map>
<entry key="myScope" value-ref="myScope"/>
<!-- spring自定义的作用域 不同线程创建新实例-->
<entry key="simpleThreadScope" value-ref="simpleThreadScope"/>
</map>
</property>
</bean>
<!--<bean id="bean3" class="com.javazx.po.Bean3" scope="myScope"/>-->
<bean id="bean3" class="com.javazx.po.Bean3" scope="simpleThreadScope"/>
github地址:https://github.com/itmrchen/springPro
可以看看吗 可以下载 好好学习 天天向上 大佬厉害呀 佩服佩服! 感谢感谢感谢感谢感谢感谢感谢感谢 真的是太棒啦 非常好非常好非常好非常好非常好非常好 很完美的教程!!!!