|
《spring自定义作用域实现》/ J- u: i* A4 m- O" h- e
" }: G5 @5 y& p: ~( G) P2 F部分java源码:MyScope.javapackage com.javazx.po;1 c% c1 N% ]" L/ |
+ R3 P& f d- O! G/ ~7 pimport org.springframework.beans.factory.ObjectFactory;
1 ~- K5 w% N6 G5 U6 y: V/ d& vimport org.springframework.beans.factory.config.Scope;9 Q" _% n: h* b1 d- C
" Q0 {+ A5 K$ A6 f% n6 h0 gimport java.util.Map;
6 m# e; t6 L1 o9 ^import java.util.Random;
9 i" p3 {) R+ _9 L4 H$ kimport java.util.concurrent.ConcurrentHashMap;
; |2 u m" t2 b: x$ m8 ]5 J
5 r: s5 `3 I5 P" I8 a Y# K+ t/**6 p" ~6 e+ b9 ?% v, n
* @author: itmrchen. @- J% r! d0 b
* @Description:双例模式-自定义作用域( [" N% x2 L$ j4 p: I1 E
* @date 2019/7/30 8:59
3 F. s6 [) F7 Q6 X( A: _ */2 [5 [7 z1 b5 i) p. e8 \9 O
public class MyScope implements Scope {
' E! W% Z. M) n* X" M Map<String, Object> map1 = new ConcurrentHashMap<String, Object>(16);+ W% g o4 X i* T4 t
Map<String, Object> map2 = new ConcurrentHashMap<String, Object>(16);
0 d# _( T& ]1 O# O1 w4 A
4 x3 M5 d1 L5 k: Q/ R. M public Object get(String name, ObjectFactory<?> objectFactory) {0 Y$ W8 t; q- ]. m) ]7 ]
if (!map1.containsKey(name)) {; H Z N0 N4 k- e/ l% s3 X: z# N
Object o = objectFactory.getObject();# c0 }, N1 y `# j9 a
map1.put(name, o);( c1 [5 m/ n6 v- x
return o;. Z9 S9 l2 W: X# Q; G. T9 Q
}
0 Z2 [! Y- R! ?+ T. A; ^ if (!map2.containsKey(name)) {
7 L) g& ]# b( Q' a! j Object o = objectFactory.getObject();
. z/ d2 \6 E1 ~# ]" t' | Q map2.put(name, o);) R. Z0 @& l7 ]7 x2 G# [
return o;
# k* l {" ?. F- j7 N3 u, | }) a3 \) x0 f: e$ U- p( u( i
int i = new Random().nextInt(2);$ d* I% S! ~9 u7 T T! `
if (i == 0) {8 ?0 M4 n8 m& y1 F# `4 v4 P8 Y" C
return map1.get(name);
6 m$ M# j5 ^3 E2 n9 `: G* g } else {7 ~& y% d/ ^/ W! ^+ L8 x; t6 O
return map2.get(name);
- d: S# K! L5 g, C( y }
* w6 x! R- |( d' U* V( e O' S" D5 p$ w! l: K
}
8 H& h1 d0 F+ N/ [" }9 W9 k9 [: K2 [! N4 q; R
public Object remove(String name) {! ]" c- s) N2 e# P
if (map2.containsKey(name)) {% B2 R% g9 I: x) y* p9 e4 `8 O4 t
Object o = map2.get(name);
( O) L6 z8 ~# v9 h5 @ map2.remove(name);# ]( \* k( h5 v, C6 L
return o;6 l5 m/ I& \0 v. p/ L6 Y. n
}
2 }: j. o6 Q6 o: H7 x if (map1.containsKey(name)) {
8 L5 d" N( F. t( T* x/ \6 b+ J Object o = map1.get(name);
, Y5 }! g: n' }, J! U/ n map1.remove(name);
8 J0 R" j' t/ g6 V- i" T4 w return o;* E* K5 g: w+ p2 o7 _7 C4 X- g" \
}9 `2 L* S* u$ }; o& s y
return null;
$ M7 {$ f% H; `9 A: e5 n8 y }- a2 ]0 H2 j! F- ^2 U
$ A' U+ ~+ `% ?- }
public void registerDestructionCallback(String s, Runnable runnable) {* r7 `* e9 d* u$ s+ H% q, P
' ~7 f( L8 p9 L }
$ S+ J, V# W m. H8 w' B0 M+ s+ H. {) B7 o" s
public Object resolveContextualObject(String s) {4 [+ w7 I4 H; D. z
return null;
6 ]0 p* |1 k; ^; j& C; X) v$ V }
" t' f; _1 Q0 I+ K
, Q& V5 I8 C6 v5 S( P public String getConversationId() {
! @; | \! y! M return null;8 D7 l& l; C& |
}
5 j( d9 q8 _' d7 Y; R}8 R" O, o: M' a) z! Y6 a
- g$ }- y0 \- r. U8 r. `7 Y+ i; Aspring.xml关键代码:
4 j4 ?, f. f1 l( P0 A/ R F, Z/ a% w B* I% ^0 i; O& ?
<!-- 自定义作用域 -->
9 w4 v4 B, Z5 z<bean id="myScope" class="com.javazx.po.MyScope"/>
* f b9 c v5 ~: J& p, ~<bean id="simpleThreadScope" class="org.springframework.context.support.SimpleThreadScope"/>* `+ [# J5 I! i5 \' H( R! y
<bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">6 E% f! a, ~2 d T. A
<property name="scopes">
$ X6 l; S6 ]" ~1 @ <map>
4 [" v) f7 L/ J) Z$ I7 t, W <entry key="myScope" value-ref="myScope"/>" ]* x" _& H! a" X j8 _ N" ~
<!-- spring自定义的作用域 不同线程创建新实例-->
9 Q1 h) ^: P, w" y( n* H m* a5 i* H <entry key="simpleThreadScope" value-ref="simpleThreadScope"/># T5 I/ n, ~" N" J7 W$ @4 }+ a
</map>; F$ p7 Z. o' t& U& D2 _
</property>7 C' x! T% \' t) Q _! U9 |' u
</bean># _+ G* {) _8 O, V$ Y- t
<!--<bean id="bean3" class="com.javazx.po.Bean3" scope="myScope"/>-->
! |! A6 S' Z* ~* S& X. L<bean id="bean3" class="com.javazx.po.Bean3" scope="simpleThreadScope"/># e$ n, I& p/ E/ A
: L. r7 T7 M" X4 y5 D5 ]- g) m! b0 Q& ?' Q8 C
github地址:https://github.com/itmrchen/springPro
5 P& c+ U; I6 X2 G. A# z$ I
4 I t7 b: R# ~+ p: s+ N, T
' g3 c9 {+ a' D# _3 D
7 z( N* r' E# |/ }2 r4 O3 {7 b |
|