|
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");
> }
>}
>
>
>
|