|
From: Colin S. <col...@ex...> - 2004-01-05 20:35:42
|
I've had a couple of discussions now with people where I've tried to=20
explain why setApplicationContext comes after afterPropertiesSet (and=20
after any custom initializing method you define), and frankly, I think=20
it just doesn't make sense except for the fact that it's that way right n=
ow.
Most beans should of course not be using the application context, but if=20
they need it, people are not going to understand the rationale as to why=20
that property is set after the initializing method is called, and that=20
they must instead treat setApplicationContext itself as an initializer=20
method.
I don't know if anybody thinks it's worth changing the order, but if=20
not, this thing is going to hit new users on the head on a regular=20
basis... At a minimum, if no code is changed, we need to update the=20
JavaDoc for ApplicationContextAware to explain the order. I can do that..=
.
Regards,
Colin
j=C3=BCrgen h=C3=B6ller [werk3AT] wrote:
>Actually, this is intended behavior, although it may be debatable whethe=
r it is appropriate. setApplicationContext is not really meant to be comb=
ined with an init-method. The latter is for non-Spring-aware beans, while=
the former is the strongest dependency a bean can have on Spring. You sh=
ould *not* design your beans to depend on that initialization order.
>
>As a solution, you could put your initialization code in your setApplica=
tionContext implementation, or in an initApplicationContext() method that=
gets triggered by setApplicationContext. Have a look at the ApplicationO=
bjectSupport convenience base class, it provides such a method out of the=
box. Of course, extending ApplicationObjectSupport is not an option if y=
ou already have a different natural base class.
>
>Juergen
>
>
>-----Original Message-----
>From: Keith Donald [mailto:kd...@cs...]
>Sent: Thursday, October 30, 2003 4:22 PM
>To: spr...@li...
>Subject: RE: [Springframework-user] setApplicationContext not being
>called
>
>
>Juergen,
>
>Wanted to update you on this issue post M2. setApplicationContext is be=
ing called now on all my ApplicationContextAware beans, thanks. The only=
issue I have remaining is it seems the bean init-method method ("initial=
ize()" in my case) is called by the container before setApplicationContex=
t. My initialize() method does stuff that requires the context - for exa=
mple, it looks up messages for initializing view components. So I would =
really need setApplicationContext() called before initialize() to prevent=
NullPointerExceptions.
>
>Thanks,
>Keith
>
>2003-10-30 10:16:58,099 DEBUG [com.csi.cogids.console.QueryNavigator] - =
<initialize called>
>2003-10-30 10:16:58,193 DEBUG [com.csi.cogids.console.qQueryNavigator] -=
<setApplicationContext called>
>
>Keith Donald
>Senior Software Engineer
>kd...@cs...
>321-676-2923 x403
>
>
>
>
>-----Original Message-----
>From: spr...@li... [mailto:springfra=
mew...@li...] On Behalf Of j=C3=BCrgen h=C3=B6=
ller [werk3AT]
>Sent: Thursday, October 23, 2003 2:14 AM
>To: spr...@li...
>Subject: Re: [Springframework-user] setApplicationContext not being call=
ed
>
>
>Keith,
>=20
>This was caused by the fact that ApplicationContextAware was being proce=
ssed after the underlying bean factory finished its work, on demand in ge=
tBean calls to the application context. I've completely reworked this for=
1.0 M2; now, the bean factory has a BeanPostProcessor hook that is also =
internally used for processing ApplicationContextAware beans, to be appli=
ed when the underlying bean factory creates any kind of bean.
>=20
>So as of 1.0 M2, to be released tomorrow, this should work properly in a=
ll cases. If there should be any remaining issues, please report them aga=
inst 1.0 M2. If you're eager, you can also try a CVS snapshot today.
>=20
>Juergen
>=20
>=20
>
> -----Urspr=C3=BCngliche Nachricht-----=20
> Von: Keith Donald [mailto:kd...@cs...]=20
> Gesendet: Di 21.10.2003 19:50=20
> An: spr...@li...=20
> Cc:=20
> Betreff: [Springframework-user] setApplicationContext not being called
>=09
>=09
>
> Forgive me if this issue has already been addressd. It appears
> setApplicationContext(ApplicationContext) is not being called on my
> ApplicationContextAware prototype beans that are not directly instantia=
ted
> by a call to beanFactory.getBean(beanName), but rather are wired "child=
"
> beans instantiated as a result of a <bean ref> references from a parent
> prototype.
>=09
> To give you an example of what I mean:
>=09
> // parent prototype
> <bean id=3D"sessionVisualizerPage"
> class=3D"com.csi.cogids.console.SessionVisualizerPage"
> singleton=3D"false">
> <property name=3D"queryNavigator"><ref
> bean=3D"queryNavigator"/></property>
> </bean>
>=09
> // child prototype
> <bean id=3D"queryNavigator"
> class=3D"com.csi.cogids.console.query.QueryNavigator"
> singleton=3D"false"
> init-method=3D"initialize">
> <property name=3D"newQueryAction"><ref
> bean=3D"newQueryAction"/></property>
> <property name=3D"newGroupAction"><ref
> bean=3D"newGroupAction"/></property>
> </bean>
>=09
> A sessionVisualizerPage prototype gets instantiated when the page is lo=
aded
> in my application. The queryNavigator prototype also gets instantiated
> because of the <bean ref>. setApplicationContext() IS called on
> sessionVisualizerPage, but not on QueryNavigator, even though both are
> ApplicationContextAware for message lookups.
>=09
> Thanks,
> Keith
>=09
> Keith Donald
> Senior Software Engineer
> kd...@cs...
> 321-676-2923 x403
>=09
>
|