|
From: Colin S. <col...@ex...> - 2003-11-20 23:32:23
|
I keep on running into the case where in defining a bean in a bean factory or context, I need to set a property to the value of a property from another bean, not the bean itself. How are people typically handling this limitation? For that matter, it would be very useful to be able to get a bean into the container via an expression. I am for example using the CCPP processing API from Sun. All the classes use a factory to get a bean instance. So if I need a ProfileFactory, I get one with this code, calling a static method: ProfileFactory pf = ProfileFactoryImpl.getInstance(); I have some beans in a context which need this passed in. I would handle this right now by defining a new class which is a FactoryBean, and returns the result of the above, but that's kludgy. Now handling any kind of generic code of this sort needs an expression evaluator, but I think this particular idiom (a static method call on a class) could probably be handled in a simpler fashion, built-in? What do you guys think? |
|
From: Colin S. <col...@ex...> - 2003-11-21 03:00:21
|
How about this for constructing beans via a static factory method
<bean id="whatever">
<static-factory method="a.b.c.d.MyClass.myFactory"/>
</bean>
which would cover static methods with no args. A second step would be:
<bean id="whatever">
<static-factory method="a.b.c.d.MyClass.myFactory">
<arg> ... </arg>
<arg> ... </arg>
</static-factory>
</bean>
where arg is basically the same thing as constructor-arg in the current
dtd...
Colin Sampaleanu wrote:
> I keep on running into the case where in defining a bean in a bean
> factory or context, I need to set a property to the value of a
> property from another bean, not the bean itself. How are people
> typically handling this limitation?
>
> For that matter, it would be very useful to be able to get a bean into
> the container via an expression. I am for example using the CCPP
> processing API from Sun. All the classes use a factory to get a bean
> instance. So if I need a ProfileFactory, I get one with this code,
> calling a static method:
> ProfileFactory pf = ProfileFactoryImpl.getInstance();
> I have some beans in a context which need this passed in. I would
> handle this right now by defining a new class which is a FactoryBean,
> and returns the result of the above, but that's kludgy. Now handling
> any kind of generic code of this sort needs an expression evaluator,
> but I think this particular idiom (a static method call on a class)
> could probably be handled in a simpler fashion, built-in?
>
> What do you guys think?
|
|
From: Rod J. <rod...@in...> - 2003-11-21 07:30:39
|
I really like this idea. No Spring dependency.
I would pull the factory method and class into different XML elements.
Or possibly use a generic factory bean that took the static factory class
and method and optionally args and meant that there was no need for new XML
or changes to existing code. In fact it would be trivial to implement right
now.
<bean id="whatever" class="GenericWhateverFactoryBean" >
<property name="staticFactory"><value>a.b.c.....
<property name="staticMethod"><value>myFactory
It could even work out the type.
Rod
----- Original Message -----
From: "Colin Sampaleanu" <col...@ex...>
To: <spr...@li...>
Sent: Friday, November 21, 2003 3:01 AM
Subject: Re: [Springframework-developer] In BeanFactory, ref to another
bean's properties, not to the other bean directly
> How about this for constructing beans via a static factory method
>
> <bean id="whatever">
> <static-factory method="a.b.c.d.MyClass.myFactory"/>
> </bean>
>
> which would cover static methods with no args. A second step would be:
>
> <bean id="whatever">
> <static-factory method="a.b.c.d.MyClass.myFactory">
> <arg> ... </arg>
> <arg> ... </arg>
> </static-factory>
> </bean>
>
> where arg is basically the same thing as constructor-arg in the current
> dtd...
>
>
> Colin Sampaleanu wrote:
>
> > I keep on running into the case where in defining a bean in a bean
> > factory or context, I need to set a property to the value of a
> > property from another bean, not the bean itself. How are people
> > typically handling this limitation?
> >
> > For that matter, it would be very useful to be able to get a bean into
> > the container via an expression. I am for example using the CCPP
> > processing API from Sun. All the classes use a factory to get a bean
> > instance. So if I need a ProfileFactory, I get one with this code,
> > calling a static method:
> > ProfileFactory pf = ProfileFactoryImpl.getInstance();
> > I have some beans in a context which need this passed in. I would
> > handle this right now by defining a new class which is a FactoryBean,
> > and returns the result of the above, but that's kludgy. Now handling
> > any kind of generic code of this sort needs an expression evaluator,
> > but I think this particular idiom (a static method call on a class)
> > could probably be handled in a simpler fashion, built-in?
> >
> > What do you guys think?
>
>
>
>
>
>
> -------------------------------------------------------
> This SF.net email is sponsored by: SF.net Giveback Program.
> Does SourceForge.net help you be more productive? Does it
> help you create better code? SHARE THE LOVE, and help us help
> YOU! Click Here: http://sourceforge.net/donate/
> _______________________________________________
> Springframework-developer mailing list
> Spr...@li...
> https://lists.sourceforge.net/lists/listinfo/springframework-developer
>
|
|
From: Colin S. <col...@ex...> - 2003-11-21 13:09:36
|
Actually I had the same thought with using a FactoryBean approach, while in the shower this morning. It's obviously simpler than modifying the bean definition and dtd (the class would probably take all of 20 lines). Can anybody think of a reason why it'd be preferable to have a top level element (ie 'static-factory') as per my initial proposal? If not, I'll do an implementation based on FactoryBean and check it in, since I need it anyways... Now I'm still curious if anybody has an answer to my first question, i.e., how are people getting around the need to get at a ref bean's properties to set as a property of a bean being defined, as opposed to setting that ref bean as a property itself. Rod Johnson wrote: >I really like this idea. No Spring dependency. > >I would pull the factory method and class into different XML elements. > >Or possibly use a generic factory bean that took the static factory class >and method and optionally args and meant that there was no need for new XML >or changes to existing code. In fact it would be trivial to implement right >now. > ><bean id="whatever" class="GenericWhateverFactoryBean" > > <property name="staticFactory"><value>a.b.c..... > <property name="staticMethod"><value>myFactory > >It could even work out the type. > >Rod > >----- Original Message ----- >From: "Colin Sampaleanu" <col...@ex...> >To: <spr...@li...> >Sent: Friday, November 21, 2003 3:01 AM >Subject: Re: [Springframework-developer] In BeanFactory, ref to another >bean's properties, not to the other bean directly > > > > >>How about this for constructing beans via a static factory method >> >><bean id="whatever"> >> <static-factory method="a.b.c.d.MyClass.myFactory"/> >></bean> >> >>which would cover static methods with no args. A second step would be: >> >><bean id="whatever"> >> <static-factory method="a.b.c.d.MyClass.myFactory"> >> <arg> ... </arg> >> <arg> ... </arg> >> </static-factory> >></bean> >> >>where arg is basically the same thing as constructor-arg in the current >>dtd... >> >> >>Colin Sampaleanu wrote: >> >> >> >>>I keep on running into the case where in defining a bean in a bean >>>factory or context, I need to set a property to the value of a >>>property from another bean, not the bean itself. How are people >>>typically handling this limitation? >>> >>>For that matter, it would be very useful to be able to get a bean into >>>the container via an expression. I am for example using the CCPP >>>processing API from Sun. All the classes use a factory to get a bean >>>instance. So if I need a ProfileFactory, I get one with this code, >>>calling a static method: >>> ProfileFactory pf = ProfileFactoryImpl.getInstance(); >>>I have some beans in a context which need this passed in. I would >>>handle this right now by defining a new class which is a FactoryBean, >>>and returns the result of the above, but that's kludgy. Now handling >>>any kind of generic code of this sort needs an expression evaluator, >>>but I think this particular idiom (a static method call on a class) >>>could probably be handled in a simpler fashion, built-in? >>> >>>What do you guys think? >>> >>> >> >> >> >> >>------------------------------------------------------- >>This SF.net email is sponsored by: SF.net Giveback Program. >>Does SourceForge.net help you be more productive? Does it >>help you create better code? SHARE THE LOVE, and help us help >>YOU! Click Here: http://sourceforge.net/donate/ >>_______________________________________________ >>Springframework-developer mailing list >>Spr...@li... >>https://lists.sourceforge.net/lists/listinfo/springframework-developer >> >> >> > > > > >------------------------------------------------------- >This SF.net email is sponsored by: SF.net Giveback Program. >Does SourceForge.net help you be more productive? Does it >help you create better code? SHARE THE LOVE, and help us help >YOU! Click Here: http://sourceforge.net/donate/ >_______________________________________________ >Springframework-developer mailing list >Spr...@li... >https://lists.sourceforge.net/lists/listinfo/springframework-developer > > |