该用户从未签到
|
spring容器对象
/ T! S2 p' R# `+ I" N( c9 I3 V BeanFactory
X W6 j" r" Q! [0 D //相对路径
! ^5 d. r4 v0 O+ P XmlBeanFactory-->Resouce--->ClassPathResouce
1 w3 o8 w8 A* }' O% V Resource resouce = new ClassPathResource("applicationContext.xml");
, r2 { @% M) P- c# I7 f$ V, ]5 T BeanFactory beanFactory = new XmlBeanFactory(resouce);
6 u2 c+ x% u& l/ L2 O* I Bean bean = (Bean)beanFactory.getBean("bean");6 u+ s9 V8 A6 \& w9 c; Q, T5 K; u
bean.show();7 N' h+ b, @7 s# ~+ A
//绝对路径7 i, G/ v- Y5 r, L' `: Y
XmlBeanFactory-->Resouce-FileSytemResouce/ j9 A% I3 x: Z4 ~$ E4 u
Resource resouce = new FileSystemResource("d:\\applicationContext.xml");9 M" e4 R* I( H2 h
BeanFactory beanFactory = new XmlBeanFactory(resouce);* l4 J% Q( Y$ ~% f' r5 q
Bean bean =(Bean)beanFactory.getBean("bean");6 N7 ^4 j9 C% X: }4 ?
bean.show();/ I- S2 {( |/ [
3 A& b( j6 c/ \, r" C2 i+ i
7 h2 }) `( _. r( X& d ApplicationContext: Y7 u3 g6 j. }7 z. O" q
ClassPathXmlApplicationContext
6 W2 ^. z# p! G4 [, q( } ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");. Q9 i5 D2 s$ x
Bean bean = (Bean)ac.getBean("bean");
4 D! }3 A* g7 ]- G o bean.show();: k8 Z, Y- m8 w
9 _1 q) r, }) T: ?4 |5 G. \ FileSystemXmlApplicationContext " @' V" L t9 m9 q
ApplicationContext ac = new FileSystemXmlApplicationContext("d:\\applicationContext.xml");% ?5 \- L3 v" v
Bean bean =(Bean)ac.getBean("bean");9 h- O' {6 p: V
bean.show();- @8 X. o' A- i, J# i
Spring提供classpath访问器 可以直接访问8 Z! k+ ]$ B0 T. x9 Z: k
ApplicationContext ac = new FileSystemXmlApplicationContext("classpath:applicationContext.xml");$ Q( h* F4 W7 V: n+ T
Bean bean =(Bean)ac.getBean("bean");0 [: r9 x) i0 [' @% V
bean.show(); % K/ |+ J4 z* m; P. H1 _: j7 B
FileSystemXmlApplicationContext支持国际化,功能更强大
+ [5 C4 ]6 m* U9 U( m1 e% g
! {. S! o: r3 b2 Bpring容器bean对象生命周期5 Z, i- z2 O% n. ~- w; p
(1)bean创建
6 Y) z3 f) l! e (a) lazy-init="false"==lazy-init="default" 5 O# v; X G" o8 |& b5 ~0 I1 c+ M
创建spring容器对象,就创建spring容器里bean对象
4 l' s4 \$ m- C# @6 B
- B8 V+ W- c8 E! z (b)lazy-init="true"
/ e% E: C" I7 O/ V: C 调用getBean spring容器才去创建bean实例
9 r% Z# E5 ~% N1 h" d; ?- |
0 v* d7 q! N9 i) {/ Q 注意:timer定时器不能使用lazy-init="true"
& y/ V% S1 F0 g+ G (a)、(b)设置在<bean lazy-init="true"></bean>
8 @8 @" n* R) [8 y1 i) v. P2 A* b. m' w* Q7 [9 ?
设置在<beans default-lazy-init="true"></beans>- ?1 }2 f" _' ]; ]5 d% A
default-lazy-init="true"设置一个全局的对象创建延时2 I2 x+ d: d% q
+ Z9 p4 ~' _4 d+ J. ]% B, d. t" L( e, C
Spring Bean对象模式& S ^7 i. o' Z" i" v: L, I
spring默认采用singleton模式
3 u8 A0 W! x" t% G1 ]+ j8 \3 i6 | singletonstruts1); G1 \) z* O) H! B4 w! u3 d
单态:每次只产生一个实例
: i: H d3 @# @( V) p7 O( m5 Q/ U prototypestruts2)/ ^! a5 T. Q; e* w* T7 W
原型:可以产生多个对象
) M, V7 H, ^; `# l$ ^' f; `) T X8 c6 R# G: ` U% t
web框架使用:
/ U) [, k; C4 X scope="session": 一次会话 / N( ?' y, b4 ]2 P& B
session:多次请求与响应叫会话( P, }9 s3 ^2 D3 g* }* n' E
scope="request: 一次请求
9 x9 ?' o+ X5 B) v- C0 ?( \! ` $ ^; l: z. s) l [7 {7 O* M# i
Spring Bean初始化和销毁7 p0 k% d6 r7 z* D% v
初始化:init-method="init" bean对象init方法名
5 q5 t1 B: x5 F1 A6 f$ G lazy-init="true"也延时了init-method
( f4 y8 a e3 a4 I" S4 N U destroy-method="destroy" bean对象destroy方法名# `. G1 b( u2 S) r+ E
完成资源释放8 X- E1 a. q- k& J {
destroy-method必须是singleton模式一起使用! n1 m @% V3 `3 B
- I+ |, {2 b% t1 P
e! W/ y* c" |% a9 J' I |
|