Results 1 to 6 of 6

Thread: factory- and init-method

  1. #1
    Join Date
    Dec 2004
    Posts
    11

    Default 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. #2
    Join Date
    Aug 2004
    Location
    Southampton, UK
    Posts
    826

    Default

    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

  3. #3
    Join Date
    Dec 2004
    Posts
    11

    Default

    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.

  4. #4
    Join Date
    Aug 2004
    Location
    Southampton, UK
    Posts
    826

    Default

    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

  5. #5
    Join Date
    Dec 2004
    Posts
    11

    Default

    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.

  6. #6
    Join Date
    Dec 2004
    Posts
    11

    Default

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

Similar Threads

  1. Order of Bean definitions matters?
    By cfuser in forum Container
    Replies: 2
    Last Post: Oct 21st, 2005, 10:29 AM
  2. Unit testing with JOTM and JtaTransactionManager
    By lalle in forum Architecture
    Replies: 1
    Last Post: Oct 15th, 2005, 09:05 AM
  3. Replies: 4
    Last Post: Aug 17th, 2005, 04:42 AM
  4. Replies: 1
    Last Post: Jul 28th, 2005, 05:08 PM
  5. Stack Overflow
    By rayho222 in forum Container
    Replies: 6
    Last Post: May 17th, 2005, 03:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •