Announcement Announcement Module
Collapse
No announcement yet.
factory- and init-method Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • factory- and init-method

    I'd like to specifiy a bean by using the factory-bean / factory-method syntax. The factory-bean which provides the method however requires custom initialization as expressed by the init-method before the factory-method returns sane results.

    Is there a way to express this requirement? Currently the method is called before initialization ...

  • #2
    As I understand your problem, you have a method that needs to be called on the factory before your factory method returns a sane result. As far as init- and factory- method goes, the init method is executed on the object created by the factory not the factory itself, so these constructs wont match your use case.

    However, you can overcome this by simply implementing the FactoryBean interface to create a custom Spring factory for your object. This will take say 4/5 lines of code aside from the method declarations so it shouldn't be too much of a hassle. You can find many examples of FactoryBean implementations in the Spring codebase itself.

    Rob

    Comment


    • #3
      Not exactly - I got a bean foo which has a certain method getFooData() - this method returns null if the init() method of the foo bean has not been called before.

      Now I'd like another bean bar to get the result of getFooData() as dependency. This works but init() has not been called before the getFooData() dependency is resolved on behalf of bar. Being lazy does not help either.

      I could call init() manually from within getFooData() if the object state is invalid but not even the other dependencies of foo are satisfied when getFooData() is invoked. It seems as if the method invoking approaches I tried (MethodInvokingFactoryBean and the factory-bean / factory-method notation in my xml application context) do not wait for dependency injection or bean lifecycle calls.

      Comment


      • #4
        Can you post your configuration as you have it now so I can check it over. From what you are saying it should be possible to have the init method of your factory bean, foo, invoked before it is used get the bar bean.

        Rob

        Comment


        • #5
          I made a small example consisting of little more than the mentioned configuration and was not able to replicate the behaviour. This probably means (dispite the fact that i doublechecked before posting here) that it's a JFace or local configuration issue rather than a Core bug.

          I'll investigate further into this problem and come back here as soon as I got results.

          Comment


          • #6
            My mistake - the observed behaviour had nothing to do at all with Spring.

            Comment

            Working...
            X