3

I have the following java classes

public class SecondClass
{
    //...
}

public class MyClass
{
    public void doSomething(SecondClass secondClass)
    {
        //...
    }
}

In blueprint I have something like the following

<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">

    <bean id="secondClass" class="foo.bar.SecondClass" />

    <bean id="myClass" class="foo.bar.MyClass" />

    <!-- How do I invoke myClass.doSomething(secondClass) ??? -->

</blueprint>

Someone knows how to call myClass.doSomething(secondClass) from inside Blueprint?

slux83
  • 686
  • 9
  • 20
  • 1
    Blueprint is used for dependency injection, not for executing code so I don't really understand what you mean be invoking the myClass.doSomething method from within blueprint. Could you please clarify a bit what you're trying to do? – Christina Apr 22 '15 at 14:19
  • Thanks for the comment. I know it is not usual, and it goes against the IoC philosophy. However, I'd like to have something like Spring has done by the MethodInvokingFactoryBean. I'm thinking that I could implement it. – slux83 Apr 22 '15 at 14:24

1 Answers1

3

If I understand correctly (not being very familiar with the MethodInvokingFactoryBean myself) what you need is a factory method, ie. something like the following:

   <bean id="myClass" class="foo.bar.MyClass" 
         factory-method="doSomething">   
       <argument ref="secondClass"/>    
   </bean>

You can find more details on how to use factories with blueprint in this guide (one of the most useful blueprint resources IMO)

Christina
  • 3,562
  • 3
  • 22
  • 32
  • I was thinking something like that, but I'm afraid the factory-method doSomething() should be static, shouldn't be? – slux83 Apr 22 '15 at 14:47
  • 1
    Yes, it should. Alternatively you can see if the instance factory method in the link I gave you is closer to your needs, – Christina Apr 22 '15 at 14:48
  • In the link it actually works as I was asking. The instance factory method invokes the method even if not static, on the instance you specify – slux83 Apr 22 '15 at 14:51