|
From: Christophe V. <c.v...@pa...> - 2004-01-20 21:08:49
|
Hello,
I'm using MethodInvokingFactoryBean to retrieve an instance of an class using
another instance of another class. If I implement InitializingBean, the
afterPropertiesSet method never gets called.
But that is not only the case with InitializingBean, it shows the same
behaviour when implementing ApplicationListenener, OnApplicationEvent never
seems to get called.
Is this normal behaviour?
I checked the bug system for something on MethodInvokingFactoryBean, but I
couldn't find anything. Should I file a bug about this?
If this is normal behavior, what is the correct way to achieve what I need (an
object instantiated outside of the beanfactory, but that still will be able
to implement Spring specific interfaces and expect them to work.
Here is a small test setup:
---test/testThingy.xml---
<beans>
<bean id="thingyCreator" class="test.ThingyCreator" singleton="true"/>
<bean id="thingy"
class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="target"><ref local="thingyCreator"/></property>
<property name="targetMethod"><value>getThingy</value></property>
</bean>
<bean id="otherThingy" class="test.OtherThingy" />
</beans>
---test.ThingyCreator----
package test;
public class ThingyCreator {
private Thingy thingy = new Thingy();
public Thingy getThingy() {
return this.thingy;
}
}
---test.Thingy---
package test;
public class Thingy implements
org.springframework.beans.factory.InitializingBean {
public Thingy() {
System.err.println("thingy constructor");
}
public void afterPropertiesSet() throws Exception {
System.err.println("Thingy: I never get called");
}
}
---test.OtherThingy---
package test;
public class OtherThingy implements
org.springframework.beans.factory.InitializingBean {
public OtherThingy() {
System.err.println("OtherThingy constructor");
}
public void afterPropertiesSet() throws Exception {
System.err.println("OtherThingy: I get called");
}
}
---test.Main---
package test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) throws Exception {
ApplicationContext context = new
ClassPathXmlApplicationContext("/test/testThingy.xml");
//prints "I get called"
OtherThingy ot = (OtherThingy)context.getBean("otherThingy");
//doens't print anything, but I think it should
Thingy t = (Thingy)context.getBean("thingy");
}
}
--
Kind regards,
Christophe Vanfleteren
|
|
From: Colin S. <col...@ex...> - 2004-01-20 22:11:08
|
These are lifecycle methods which apply to beans created by the
container (spring) itself. If the container is not actually creating an
object, then it can't and shouldn't call lifecycle methods on it.
All that MethodInvokingFactoryBean is doing is calling a method on
another bean, but in this case it has nothing to do with the fact that
that method is then creating a new object. So with w/regards to the
test.Thingy class, it is only created via a 'new Thingy'()' call in
ThingyCreater, and you can not expect that any lifecycle methods will be
called on it by Spring.
Regards,
Colin
Christophe Vanfleteren wrote:
>Hello,
>
>I'm using MethodInvokingFactoryBean to retrieve an instance of an class using
>another instance of another class. If I implement InitializingBean, the
>afterPropertiesSet method never gets called.
>But that is not only the case with InitializingBean, it shows the same
>behaviour when implementing ApplicationListenener, OnApplicationEvent never
>seems to get called.
>
>Is this normal behaviour?
>
>I checked the bug system for something on MethodInvokingFactoryBean, but I
>couldn't find anything. Should I file a bug about this?
>
>If this is normal behavior, what is the correct way to achieve what I need (an
>object instantiated outside of the beanfactory, but that still will be able
>to implement Spring specific interfaces and expect them to work.
>
>Here is a small test setup:
>
>---test/testThingy.xml---
><beans>
> <bean id="thingyCreator" class="test.ThingyCreator" singleton="true"/>
> <bean id="thingy"
>class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
> <property name="target"><ref local="thingyCreator"/></property>
> <property name="targetMethod"><value>getThingy</value></property>
> </bean>
>
> <bean id="otherThingy" class="test.OtherThingy" />
></beans>
>
>---test.ThingyCreator----
>package test;
>public class ThingyCreator {
> private Thingy thingy = new Thingy();
>
> public Thingy getThingy() {
> return this.thingy;
> }
>}
>
>---test.Thingy---
>package test;
>
>public class Thingy implements
>org.springframework.beans.factory.InitializingBean {
>
> public Thingy() {
> System.err.println("thingy constructor");
> }
> public void afterPropertiesSet() throws Exception {
> System.err.println("Thingy: I never get called");
> }
>}
>
>---test.OtherThingy---
>package test;
>
>public class OtherThingy implements
>org.springframework.beans.factory.InitializingBean {
>
> public OtherThingy() {
> System.err.println("OtherThingy constructor");
> }
> public void afterPropertiesSet() throws Exception {
> System.err.println("OtherThingy: I get called");
> }
>}
>
>---test.Main---
>package test;
>import org.springframework.context.ApplicationContext;
>import org.springframework.context.support.ClassPathXmlApplicationContext;
>public class Main {
>
> public static void main(String[] args) throws Exception {
> ApplicationContext context = new
>ClassPathXmlApplicationContext("/test/testThingy.xml");
> //prints "I get called"
> OtherThingy ot = (OtherThingy)context.getBean("otherThingy");
> //doens't print anything, but I think it should
> Thingy t = (Thingy)context.getBean("thingy");
> }
>}
>
>
>
|
|
From: Christophe V. <c.v...@pa...> - 2004-01-20 23:56:10
|
On Tuesday 20 January 2004 23:11, Colin Sampaleanu wrote: > These are lifecycle methods which apply to beans created by the > container (spring) itself. If the container is not actually creating an > object, then it can't and shouldn't call lifecycle methods on it. > > All that MethodInvokingFactoryBean is doing is calling a method on > another bean, but in this case it has nothing to do with the fact that > that method is then creating a new object. So with w/regards to the > test.Thingy class, it is only created via a 'new Thingy'()' call in > ThingyCreater, and you can not expect that any lifecycle methods will be > called on it by Spring. > Thanks, that makes lots of sense. The reason I wanted to be able to do this is the following: I have an object, that is actually a loaded hibernate instance (and the only instance of its class). It is needed in many places in my application (actions, tablemodels, ... ; this is a standalone swing app), as it is the central object that holds all the data that will be manipulated and is in memory all the time. I now load this instance via a call on a Hibernate DAO class using the MethodFactoryBean, and give references to it for anything that needs it (practically every panel, action, ...). So far so good. It would have been handy to let this object handle certain ApplicationEvents, which is why I implemented this interface, but I now know that this won't work. (As a sidenote, ApplicationListener is very handy in swing apps, you can create very loose coupled components, without the need to create and register all sorts of custom Listeners, and it doesn't give any significant performance drawbacks because there are so few ApplicationListeners.) I suppose I could register an instance of another class, also let it implement ApplicationListener, and then let it the delegate the events to the hibernate instance, but now I'm wondering if there is a more direct way to do this (maybe by somehow making the hibernated object a "real" spring object). -- Kind regards, Christophe Vanfleteren |