该用户从未签到
|
spring容器对象0 m# a/ ?8 {$ I1 z' v/ ]
BeanFactory
: M. Z7 G8 q. N7 g9 i% z8 L: _: w //相对路径
J. `- G/ q$ v1 l' s4 H& S XmlBeanFactory-->Resouce--->ClassPathResouce
# ^$ r2 Q0 }2 G& A4 G4 S Resource resouce = new ClassPathResource("applicationContext.xml");
1 D. _& ` @2 l BeanFactory beanFactory = new XmlBeanFactory(resouce);
; Y" v) b) Z2 X: K: T) U$ q+ |3 l Bean bean = (Bean)beanFactory.getBean("bean");& S2 z* h8 m8 [6 }
bean.show();
3 Y5 n$ \' y0 l //绝对路径! q+ `& R+ j5 t t
XmlBeanFactory-->Resouce-FileSytemResouce! U' @" E C% i6 f6 L' X
Resource resouce = new FileSystemResource("d:\\applicationContext.xml");
1 u6 h4 W( ~& [ BeanFactory beanFactory = new XmlBeanFactory(resouce);* h. w8 M) @- D# t, m2 X
Bean bean =(Bean)beanFactory.getBean("bean");' r, h! u6 O, l
bean.show();
: w7 W" Q- h& e) s# b- j
/ D& Z# M) U8 L% F4 ~/ l ( s* N1 y6 D/ [3 z6 e
ApplicationContext
0 g' F: {; j; }8 X8 T. B5 v! z- D ClassPathXmlApplicationContext
* B& R- p. M6 M2 _% s l ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
! b# J. N7 {( Y' z" R Bean bean = (Bean)ac.getBean("bean");
" ]- l u V/ P bean.show();9 T+ }& l; B B1 n1 m& v
7 D1 E, ~* Q% {
FileSystemXmlApplicationContext
7 t% `$ C3 |3 u. u2 W ApplicationContext ac = new FileSystemXmlApplicationContext("d:\\applicationContext.xml");3 \5 \0 K9 z6 E, `( k$ O
Bean bean =(Bean)ac.getBean("bean");( C+ S2 ?( l' [ _/ X$ ?
bean.show();
9 p, Q1 S5 v% P* @! H3 c Spring提供classpath访问器 可以直接访问" Q0 Z/ S3 F" Y, k
ApplicationContext ac = new FileSystemXmlApplicationContext("classpath:applicationContext.xml");
' r" c7 W/ ^$ O! X0 u: m Bean bean =(Bean)ac.getBean("bean");
2 w. I2 b* \& H+ M1 A( F& u; u bean.show();
. n$ v1 D. A y+ n; l( @ FileSystemXmlApplicationContext支持国际化,功能更强大* H: O& e* i+ |
% X# J2 s3 }) w! d7 \pring容器bean对象生命周期- ?5 B" m: L; C
(1)bean创建
# _! i4 p; t5 M$ Y; N8 Z (a) lazy-init="false"==lazy-init="default"
" C. S' J2 f) R v/ @4 T: u3 p' w 创建spring容器对象,就创建spring容器里bean对象) u/ m) j0 N9 o V" y0 U% |' z. V
" A0 o8 Z; Y: h3 E0 z
(b)lazy-init="true" ' b0 P( G, J& m0 U
调用getBean spring容器才去创建bean实例; [; Z0 b2 a( G" e0 N& E5 b
. W0 r9 I4 Z5 n+ p7 R0 w' J6 \ 注意:timer定时器不能使用lazy-init="true"
& A7 x" v6 [( M( m' M (a)、(b)设置在<bean lazy-init="true"></bean>% t( t. H6 X: s1 X
5 T9 T# a0 v0 `, M' o. I
设置在<beans default-lazy-init="true"></beans>
; l0 f% u$ k3 K0 f3 d+ |- l( D default-lazy-init="true"设置一个全局的对象创建延时
: _/ A# K- [, J: L# Z1 Y* x. s% R. w$ |& t2 g
Spring Bean对象模式
. I/ a: l0 N/ W+ m Lspring默认采用singleton模式
, f; ?, K$ n6 X' G% t, y singletonstruts1)! Q' D$ U1 q+ t+ @* H6 j/ X
单态:每次只产生一个实例
. j8 e# m" P7 i4 w% [- |0 p3 s prototypestruts2)2 D1 Z# _* }% X t
原型:可以产生多个对象
& \ s5 s2 a! b% y/ z% |0 h
: w) a% k& ?6 C u$ Y- f9 g web框架使用:
. D- N8 ~3 K/ g i: q scope="session": 一次会话
2 d, T& w: [7 K# j- Z# F session:多次请求与响应叫会话
( L! B: x' @0 r3 u0 r! _. U5 f* S/ A scope="request: 一次请求* Z! E% t. z8 ~
; e0 d8 G3 n T, b: i# PSpring Bean初始化和销毁7 }. d8 F6 b- R
初始化:init-method="init" bean对象init方法名4 x! O3 K( }6 l# d+ J
lazy-init="true"也延时了init-method
: A* l5 | C* P: j7 h0 S9 f9 ~# K destroy-method="destroy" bean对象destroy方法名
5 P' m1 x# Q, f: p% b( D 完成资源释放
" ]9 E# w- ?& A+ s, ]+ l' R destroy-method必须是singleton模式一起使用
, T& o6 L$ q* I# z6 ^, e9 q
1 R6 Y# i" R& [ q8 t+ c
& ]; d! B+ t3 | {/ @% L |
|