|
《spring自定义作用域实现》
. ^' m2 I% m2 B3 v+ O! s
) t! Q3 l: ~3 P部分java源码:MyScope.javapackage com.javazx.po;
8 Z$ _9 |8 j2 h1 m, m: ^5 A) }4 w
% b* D Y+ m1 M2 h4 rimport org.springframework.beans.factory.ObjectFactory;
; t3 V" K m' D1 n& y& K0 R1 nimport org.springframework.beans.factory.config.Scope;% K( v, e" E% `+ _+ P, s u
( v% [: K9 S! z' P- I
import java.util.Map;0 H1 D6 g4 y( M4 e- ^' r4 m
import java.util.Random;* M) n2 Q! _) x, j5 B
import java.util.concurrent.ConcurrentHashMap;
$ T5 c2 N% D- p" H3 }/ v) e7 h- C4 |0 `" H
/**4 d8 ]* ^' h2 Y( V
* @author: itmrchen
( j, s6 g1 t2 y" \7 g * @Description:双例模式-自定义作用域9 _* n/ y/ @! [/ u0 |; D
* @date 2019/7/30 8:599 X# M8 L. `& U
*/
9 ^7 z! v) K7 q5 Epublic class MyScope implements Scope {
{0 `- ?" o0 d* m4 n) _ Map<String, Object> map1 = new ConcurrentHashMap<String, Object>(16);1 K+ |( m. ]% K/ [
Map<String, Object> map2 = new ConcurrentHashMap<String, Object>(16);, O, U% f* g2 P) Z1 C2 p
+ I; |) @$ n1 I8 p
public Object get(String name, ObjectFactory<?> objectFactory) {
4 I% U* r6 Y: W2 t3 Q if (!map1.containsKey(name)) { N4 b- C8 Y# k3 [8 f
Object o = objectFactory.getObject();
# G% V' G9 n9 n- w1 I( A map1.put(name, o);! D1 j; a- g7 @+ W# P' C9 w3 m2 O
return o;
w4 Z) \$ a, s) O% x }! K2 [0 D$ Q6 _6 y2 w/ g
if (!map2.containsKey(name)) {6 r' E: h2 p- t
Object o = objectFactory.getObject();) q( _( H, t2 G0 w! v/ K# E
map2.put(name, o);
1 w5 o+ [8 x) l+ I* J return o;8 M5 H3 s O6 l; J& G. }, a2 a4 Z
}
* l, V) _7 d M4 r int i = new Random().nextInt(2);
9 ]" \( t2 ~- i/ d& H3 c) C& g" e$ x& D if (i == 0) {
5 ?! e8 u# ~7 W4 v5 J5 X: O. V- e return map1.get(name);
9 }' i, U5 Q/ b; j } else {
( m2 D1 n1 [+ T/ a; g) } return map2.get(name);
+ ?8 _8 Q! ^" h1 s( h. y. O5 S }' K9 o/ s# J- r0 ^& T" i. K! I
) z) j# W+ m+ k* o; r
}, |1 W5 Z- A" {4 U
* k: Q% @; p) W( _0 Q8 H
public Object remove(String name) {
S: R P$ p) R+ z$ [" K if (map2.containsKey(name)) {
9 @3 V# a' g" K/ }6 u# s6 y, g Object o = map2.get(name);$ y) m) g, m( z+ {( ]% j' w1 S
map2.remove(name);( u: k' {9 M9 o& a
return o;
) m# }7 I2 A- e" u }
4 _% N, o7 N5 ?5 l if (map1.containsKey(name)) {
% i9 l' k5 P4 w) t Object o = map1.get(name);
2 p+ J" v5 E: L0 q0 D, ]6 ^ map1.remove(name);
: L. v! ^% {) o( P return o;
' s) F' U R* d$ _& h4 S }
9 M! b9 W# j* j" }# H1 E: k) T return null;$ t( m+ h7 L9 E' F9 r9 B; t- Q% W
}
5 A5 s1 n$ Z7 e# X, E8 W1 ?# s/ R
8 h& y+ q. e. ? public void registerDestructionCallback(String s, Runnable runnable) {! l( M9 r7 c0 D. w: S8 V2 M
! j; A7 K" D, `
}
& m; J5 @& y. Z8 |2 q1 z2 p) v
5 n7 e: d9 u* n! Y! C1 H public Object resolveContextualObject(String s) {
) T7 N( h9 \* I B! n7 W3 I7 y return null;8 I8 O$ ^$ U$ T; S- f
}+ d8 V, U/ u1 \0 Y: M/ Z4 j$ B
9 u! m. N/ E. I# _- @
public String getConversationId() {! C! C3 k' y% c0 w6 L( z
return null;8 i/ v) X' {5 h" W* |! j
}
7 e' }; _2 g, l- ? _}
& K: U2 U" `+ n5 Z2 q9 Z. V& o% `8 \
spring.xml关键代码:
3 |$ C/ g* y: E
. i; E7 z: r# E+ p( B<!-- 自定义作用域 -->
8 _3 V: z" l7 b! |, b# [<bean id="myScope" class="com.javazx.po.MyScope"/>
- m# N, Q# J! T, B# c& g<bean id="simpleThreadScope" class="org.springframework.context.support.SimpleThreadScope"/>
/ e6 p6 _! s4 A5 |/ }<bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">9 |6 }8 f" j& C7 ~8 q
<property name="scopes">
7 G Q& T- \8 x' }0 l <map>
1 z# l, A) x N) I <entry key="myScope" value-ref="myScope"/>: ^! J' ]9 v* P6 b* J
<!-- spring自定义的作用域 不同线程创建新实例-->
/ F2 E( J( \6 b% x2 x" H* S <entry key="simpleThreadScope" value-ref="simpleThreadScope"/>( T! l* Z. Y8 e% c; _
</map>" V& m9 T4 O! g3 h8 B" i7 d/ @
</property>
7 i' S8 P# G3 p z: G$ R</bean>% \4 \) S# e1 {% G
<!--<bean id="bean3" class="com.javazx.po.Bean3" scope="myScope"/>-->
* T, Y. z4 A) V<bean id="bean3" class="com.javazx.po.Bean3" scope="simpleThreadScope"/>
4 C+ g0 g2 O; ? N
2 n( S: I" h" o' _1 L/ w( U! O: x9 Y9 n, ~
github地址:https://github.com/itmrchen/springPro2 h3 N# z+ D# ?
( B3 T7 T0 E% a( I
4 l5 G5 r3 D7 M* H6 d
; s3 z! }4 @2 U9 G8 P1 ?6 E! J% z |
|