该用户从未签到
|
spring容器对象: r6 J/ Q& v) O+ f5 w& ]- `9 G) G6 j
BeanFactory% X U; s+ P7 o# x @' |
//相对路径, Q Z, ~. m& s' X! H
XmlBeanFactory-->Resouce--->ClassPathResouce$ s1 R9 y: U B0 r* c- l; ]# X
Resource resouce = new ClassPathResource("applicationContext.xml");9 E% Z* j* [2 J
BeanFactory beanFactory = new XmlBeanFactory(resouce);4 g' t* P4 o# S# s2 }
Bean bean = (Bean)beanFactory.getBean("bean");$ {! |) S* B9 V. }5 d
bean.show();. B) Y( I0 N5 s4 B/ k
//绝对路径0 Q( W, [3 z$ b) l
XmlBeanFactory-->Resouce-FileSytemResouce2 q9 e8 b& X! ~6 }) Y2 x% P1 U+ {4 y
Resource resouce = new FileSystemResource("d:\\applicationContext.xml");2 I, Y# o! M2 z! R: V: T
BeanFactory beanFactory = new XmlBeanFactory(resouce);
5 W; d) z% C1 T. { Bean bean =(Bean)beanFactory.getBean("bean");
- _9 y- t" a9 J( e bean.show();& Z' c! u4 e# Z1 n- r" _: x
( n9 C0 e' q4 `. H/ T9 X8 q0 V
6 z6 ^, H5 j; F: c: l& r ApplicationContext( o I- B0 M u* n0 K5 a
ClassPathXmlApplicationContext! T; u6 d% F6 R4 u; T! v3 Q
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");) `; A8 R; M! ` x+ Q9 X
Bean bean = (Bean)ac.getBean("bean");7 J* U) d6 z' H" S/ v- j( H; C
bean.show();3 M) G" v; i' S- f: J+ ?$ t A
% p% {/ w L8 J+ [ FileSystemXmlApplicationContext
! T+ x+ s8 R$ U' h ApplicationContext ac = new FileSystemXmlApplicationContext("d:\\applicationContext.xml");
/ U: q! O; g2 w% L& M' A6 _& ~ Bean bean =(Bean)ac.getBean("bean");3 h. h P! R4 X1 f$ A# U
bean.show();
' j, U& V2 c- _' t, S Spring提供classpath访问器 可以直接访问( B- m' W& _: x2 F& e5 }
ApplicationContext ac = new FileSystemXmlApplicationContext("classpath:applicationContext.xml");
6 r" q8 F$ U$ ]* n2 u! e1 U1 I ?& G Bean bean =(Bean)ac.getBean("bean");- `1 |; K7 d X+ q# {% w8 F- n
bean.show();
3 r9 T- z$ `7 M2 s FileSystemXmlApplicationContext支持国际化,功能更强大. [, N. [+ I u7 P! K6 r
5 p% y* P8 @- o9 f
pring容器bean对象生命周期0 p& s0 {# i' N5 v
(1)bean创建$ ?& t# E: j& l3 }3 S
(a) lazy-init="false"==lazy-init="default" % o' S0 Q' ^9 K. g# z% W
创建spring容器对象,就创建spring容器里bean对象
4 I9 u3 e$ i3 T1 Y q6 G) F2 b( M3 |: z4 d4 c4 a
(b)lazy-init="true" " z4 G- D3 @* D
调用getBean spring容器才去创建bean实例4 [ p- t: n4 ^& G! D
5 `% k% i+ x0 ^ 注意:timer定时器不能使用lazy-init="true"
* j7 L4 ?& w( r) L! J/ k. R/ R (a)、(b)设置在<bean lazy-init="true"></bean>3 B1 A( L* d% Y( [; {1 W$ s( M; |
0 I' H3 {' S7 z; Q5 |
设置在<beans default-lazy-init="true"></beans>0 Q) S9 [& X! e7 t: ] R$ [
default-lazy-init="true"设置一个全局的对象创建延时
' J7 I! U. d6 B% x
; T( s; X2 F, }0 xSpring Bean对象模式/ L6 |& E! o h8 K2 |$ q8 |
spring默认采用singleton模式+ D. n5 K0 ^5 S- [: U
singletonstruts1)
7 r1 w8 P* a0 C: p6 s8 [. E 单态:每次只产生一个实例
) e7 q) l3 u& j, H) Q; H prototypestruts2)' p9 t+ ^7 X. Z* @
原型:可以产生多个对象
1 T; t$ J. Z5 N o/ s4 _! [' _5 S d% C) \* o4 v: g: v. J
web框架使用:
+ _; M; ? k) r7 ]5 N scope="session": 一次会话 9 D ~4 S" p: n& N; `
session:多次请求与响应叫会话
. B4 F% u+ E8 J( y scope="request: 一次请求
0 w: Z! W# X s1 V* U* ?4 h
' U, r% u5 m; C* ^$ K. G1 M% HSpring Bean初始化和销毁
8 h( X7 H- ?3 V! Z3 e+ F初始化:init-method="init" bean对象init方法名# M' v# g, U% k; g
lazy-init="true"也延时了init-method
* m0 z z: c4 Y+ j" h. c) q) X+ D* d destroy-method="destroy" bean对象destroy方法名9 {/ Z5 |, n% C# k. [
完成资源释放8 C3 E5 F6 v6 u0 i/ E- K
destroy-method必须是singleton模式一起使用
2 x& g ?6 X9 I3 v6 g9 O% z5 \# [$ P
' n7 [: H: E. S! f6 Y* G2 @# G
) G" U% x' m7 z: W. z' x. x' A5 m |
|