|
《spring自定义作用域实现》, v! s5 B8 Q* Q8 b& W3 {
& P9 z: b/ @; D部分java源码:MyScope.javapackage com.javazx.po;" d/ L9 e) o2 ?0 M6 v1 L
! b1 ]2 n; t% Y" t+ f. l4 j- t
import org.springframework.beans.factory.ObjectFactory;' h8 E/ n D' G8 {' f; N
import org.springframework.beans.factory.config.Scope;
# D8 L% |$ t, P- R' B( e/ C8 e6 Z
1 o8 _$ y7 F" s3 Limport java.util.Map;
1 g6 y9 i. L6 ~6 `: ]import java.util.Random;7 F! M- Q1 @- O: x
import java.util.concurrent.ConcurrentHashMap;( c* f+ ^9 b/ c2 v
3 R2 A" F. _0 Z2 g+ f* x
/**% b4 s: {' {7 B! m7 L/ S% J |
* @author: itmrchen& {/ j' ]1 S1 i- S, o$ p' h
* @Description:双例模式-自定义作用域/ F" l3 E! H% g# v
* @date 2019/7/30 8:590 A& S' m5 O9 {" W% S6 x0 j$ g1 N
*/
% w2 J p v. }, Hpublic class MyScope implements Scope {$ t: x: O# A8 t' ~" @6 L5 J
Map<String, Object> map1 = new ConcurrentHashMap<String, Object>(16);) K5 |: q* d. O2 D
Map<String, Object> map2 = new ConcurrentHashMap<String, Object>(16);" K) F: O2 F3 V5 P; Z' \! o
1 c" C- K9 [: r* w public Object get(String name, ObjectFactory<?> objectFactory) {7 R5 @6 i) [4 X( ]3 O# a* L- D
if (!map1.containsKey(name)) {, l3 _! _6 ]! R7 g
Object o = objectFactory.getObject();- _3 ]' l0 S$ k) N
map1.put(name, o);/ E0 v( g1 l* U
return o;$ d: |- s- h7 E5 u) b
}
8 l/ w6 ~# H8 O, t4 w: n if (!map2.containsKey(name)) {
3 K& F3 C4 N3 I* T R Object o = objectFactory.getObject();
% X& l3 h$ V1 [1 P8 q# n x5 \ map2.put(name, o);8 I& |7 p% n0 @0 E# B+ M
return o;% \/ { e! o1 n( i }* m4 Z
}
6 W' L: S/ B* ~* i5 ^: T int i = new Random().nextInt(2);
* c0 N2 T8 T/ S$ _) ]' u8 R if (i == 0) {+ X4 n5 j+ d3 ~9 d: I
return map1.get(name);
% R: e s& v7 r# S } else {
: V+ P# C( j1 V+ H# c return map2.get(name);+ \$ p) ~( }- v& J( m: T, S4 e$ ~
}
( u& G, R/ k3 T7 d$ x
$ X! n$ d1 y. R$ n- E( Z }) Z7 f' B' @- b: z
3 l W0 n/ o; }
public Object remove(String name) {
/ y$ y, H% V" L- p3 K' t1 `) I if (map2.containsKey(name)) {
: |3 f# P0 r6 v# r1 A4 d Object o = map2.get(name);
7 G$ d" X G- m0 t; ?2 Z# j7 Q! E( W map2.remove(name);
& W1 ~1 O0 m9 y1 n7 g6 a return o;
9 `: |0 k) c1 y) V2 g) @2 ?4 } }( J+ Z4 @9 n1 J7 n8 x/ S; H& V( X
if (map1.containsKey(name)) {6 l' ]# Z$ A" ^. S6 i) _
Object o = map1.get(name);1 o/ }0 M# z+ T1 J
map1.remove(name); ^: ]. a# E! I8 H
return o;& u* g) X, g w: \% y
}
6 @) P& P R- _2 e return null;% g# T1 p9 F, x( T
}5 E& ]" _; p' R. A
' C4 D* h7 ?0 ` public void registerDestructionCallback(String s, Runnable runnable) {9 m7 F6 h0 A% c
3 c& U; v1 c6 m: I2 ]4 ? }: H, R" V3 L. ~
( c2 T1 r& ~; ]& x+ z) H public Object resolveContextualObject(String s) {
# l3 K I, J7 [. y# Z return null;$ i! c. A5 t* |8 e% [. l- t
}
& h. F9 I$ H j1 x
$ _; P4 `: V! D! e! p public String getConversationId() {
+ p$ }$ G5 t4 z" _7 }5 L( ] return null;" I% J. d) w6 }% N' Q) s5 W$ ?
}
) m2 S+ Q* V, h5 \) J}
2 t V* u% Y( W- w& [5 j
/ W0 a8 h" p2 Ispring.xml关键代码:
) o8 {. h) D7 a- `
' I- s o) u0 F% B3 i<!-- 自定义作用域 --># B S( Q; e2 k0 B3 C, u( @4 l8 U
<bean id="myScope" class="com.javazx.po.MyScope"/>
9 J, |! d% `: j4 s% w( n5 d5 G h<bean id="simpleThreadScope" class="org.springframework.context.support.SimpleThreadScope"/>6 N$ l- n0 D7 r1 r3 c% T
<bean class="org.springframework.beans.factory.config.CustomScopeConfigurer"># A1 C' ^0 K Q7 E( m- s
<property name="scopes">
! o: C' f5 I" d3 I$ M' F <map># J' E* o6 Q, ^- G) ~* v) b
<entry key="myScope" value-ref="myScope"/>: m1 R% u. w; s
<!-- spring自定义的作用域 不同线程创建新实例-->) X! P" }7 N) p/ I' [
<entry key="simpleThreadScope" value-ref="simpleThreadScope"/>; [+ Q9 r/ T" l" ~+ B
</map>" F% M# |5 h8 W# V6 p
</property>
0 h: d( d# f0 C$ t& A5 W" [/ t1 l</bean>
' q8 r# _/ m" U9 ^! M9 [<!--<bean id="bean3" class="com.javazx.po.Bean3" scope="myScope"/>--> j0 r! k2 Q9 o: A
<bean id="bean3" class="com.javazx.po.Bean3" scope="simpleThreadScope"/>
% L% q9 K( Y- x8 A, }) r0 {3 w- M' Y$ x! I4 i( Y
) d# O: U: l( _" O8 @- X* Rgithub地址:https://github.com/itmrchen/springPro. n, ^7 Y+ m2 J# X$ V" H1 w
2 G1 Z+ U* R' p }0 D. `6 c2 I1 S7 }/ W3 `% y
" m% M- T! t! j- a
|
|