该用户从未签到
|
spring容器对象0 D; P8 p. }9 @9 Z8 y9 \
BeanFactory
6 m, F: R% @' d, Y% a) P* Y //相对路径
! o( Y5 @) f6 t2 \+ J' P XmlBeanFactory-->Resouce--->ClassPathResouce
% k2 H) i9 G/ Z5 l( w. p/ D Resource resouce = new ClassPathResource("applicationContext.xml");
$ ^; U. w% z a9 C# g9 Z BeanFactory beanFactory = new XmlBeanFactory(resouce);. G0 Z; {$ r0 s( T
Bean bean = (Bean)beanFactory.getBean("bean");
& d7 W3 V( C. {- B- U8 ^ bean.show();' z% p$ e+ t- k1 I* Z' c
//绝对路径. K- O8 B6 [; H: ]5 t9 k2 k
XmlBeanFactory-->Resouce-FileSytemResouce2 [# R' U" P+ u9 @4 G
Resource resouce = new FileSystemResource("d:\\applicationContext.xml");* h( T& G/ U- D. q
BeanFactory beanFactory = new XmlBeanFactory(resouce);- w* r, a# _$ A7 O# z
Bean bean =(Bean)beanFactory.getBean("bean");
) x b2 w- R5 c! J% W+ H: l bean.show();
; D' C+ ~+ l5 c9 _5 ? # e$ D. W: i1 V. k# `# u1 r
% `+ o& [' b' t7 T
ApplicationContext% n s! P# O- V$ s5 b8 V
ClassPathXmlApplicationContext: l9 e5 K! S" ~2 Z: M) F# r! j
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");3 k0 n, P6 f: M
Bean bean = (Bean)ac.getBean("bean");- B* p+ C5 V& P2 ~
bean.show();
1 N# A5 |' q& F1 e$ h, p
0 ]$ C6 f) u+ D FileSystemXmlApplicationContext
8 I# x0 a: i5 Q ApplicationContext ac = new FileSystemXmlApplicationContext("d:\\applicationContext.xml");* M9 m7 u/ Z3 o" E4 h3 w% t- z
Bean bean =(Bean)ac.getBean("bean");
[2 H6 C- u# ]5 U) q. U" h" D8 F2 a* u bean.show();2 [. W( ?( d2 b
Spring提供classpath访问器 可以直接访问
2 a. b/ e, e( o* B# q: G- b8 ^+ J ApplicationContext ac = new FileSystemXmlApplicationContext("classpath:applicationContext.xml");
& a% v# c8 e: { Bean bean =(Bean)ac.getBean("bean");
) w# } p7 j3 A% ]# K! a+ K6 l bean.show();
6 J1 R5 ]9 R a, }4 Q/ A/ Q( H FileSystemXmlApplicationContext支持国际化,功能更强大6 l, I1 F `2 m9 f- r
0 c( K4 B! t" s! z) k+ M
pring容器bean对象生命周期
0 L A) U P! v% i (1)bean创建
2 O5 z7 `4 R! X$ B* E" f1 ` (a) lazy-init="false"==lazy-init="default"
# |# o# K+ i! K" {, e6 A% Z 创建spring容器对象,就创建spring容器里bean对象
a, j6 o. \, ^# X$ k: x$ f' a( S1 {7 ?8 Y4 b" |+ k
(b)lazy-init="true"
/ U: ]! F0 C9 @! L' N% v) {* E 调用getBean spring容器才去创建bean实例" B' j6 y) m; g3 U" q# k
# J! @1 Y" I) r. e/ i
注意:timer定时器不能使用lazy-init="true"
0 t1 W( k8 W2 r, X) w (a)、(b)设置在<bean lazy-init="true"></bean>, j7 r; ?! u1 h6 P& P5 I5 a
[( D3 v* c. _ 设置在<beans default-lazy-init="true"></beans>
) X& v2 Z0 w" `+ r+ d5 A" Y& N default-lazy-init="true"设置一个全局的对象创建延时
- a; @. W" L) `; K; u
U; B. x$ {; j& `2 ISpring Bean对象模式
: @3 x$ h2 b3 g1 v( o, |8 J mspring默认采用singleton模式! T# A( q+ z' y5 ]/ n I
singletonstruts1)
& P& x' W3 L4 c: {2 N 单态:每次只产生一个实例4 }7 V2 U/ F+ |- S4 H4 A9 {
prototypestruts2)
0 G5 p2 A/ s4 S 原型:可以产生多个对象
: ?2 V( ^2 q0 T3 Q
$ p0 U+ L2 T0 o3 E/ d. [) y' j web框架使用:- x! |" K5 v( o
scope="session": 一次会话
~$ m. L* x3 d) P0 K session:多次请求与响应叫会话
h0 C5 u$ x% \& F' }( t scope="request: 一次请求
8 z; ~9 @. O7 K: o) l+ ] x6 W
8 r ~7 S7 D- h5 p" \Spring Bean初始化和销毁7 y5 ?' A* N2 y3 R/ W
初始化:init-method="init" bean对象init方法名
9 |$ J( t h0 Z; G, d0 Q lazy-init="true"也延时了init-method" X/ d3 X2 J( v, w! e% a
destroy-method="destroy" bean对象destroy方法名
2 V% G) M: W4 M' Q g/ x$ p 完成资源释放- j1 B5 b/ F) y
destroy-method必须是singleton模式一起使用8 ~& J8 E2 d' w% z
: A5 F# t. n9 O6 ~0 S# M6 x
1 `4 [7 a# o( U7 s' v
|
|