Announcement Announcement Module
Collapse
No announcement yet.
FactoryBean in Spring 3 + Configuration Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • FactoryBean in Spring 3 + Configuration

    Hi,
    i moved from Spring Java Config project towards Spring 3.
    in Spring Java Config, when u wanted to return an object of a factory bean,
    for example SessionFactory, use extended 'ConfigurationSupport' class and called "getObject(factoryBean, SessionFactory.class)", for example:

    class MyConfigClass extends ConfigurationSupport {

    @Bean
    public SessionFactory sessionFactory() {
    AnnotationSessionFactoryBean factoryBean = new ...();
    factoryBean.setDataSource(dataSource());
    ...
    return getObject(factoryBean, SessionFactory.class);
    }
    }


    How can i do similar thing in spring 3 ?
    (BTW, i have tried factoryBean.getObject(), and it doesn't work)

    many thanks !

  • #2
    Does this help?

    Regards,
    Rob Winch

    Comment


    • #3
      How see an example for a FactoryBean

      thanks to your help,
      but i dont see an example for returning a FactoryBean, or enable creating
      an object from a FactoryBean like in the old "java config" as in the example i gave above:

      Comment


      • #4
        I guess I misunderstood your problem. When you say getObject() doesn't work, what is the behavior (i.e. is there an exception, is the SessionFactory null, etc)?

        One thing to double check is to ensure afterPropertiesSet() is called on the factory prior to calling getObject(). This is easy to overlook since when the Object is created by Spring it calls this method for you. Below is a working example of this:

        Code:
        @Configuration
        public class ConfigExample {
        	@Bean
        	public SessionFactory sessionFactory() throws Exception  {
        		AnnotationSessionFactoryBean factory = new AnnotationSessionFactoryBean();
        		factory.setDataSource(dataSource());
        		// more configuration...
        		factory.afterPropertiesSet();
        		return factory.getObject();
        	}
        }
        HTH,

        Rob Winch
        Last edited by Rob Winch; May 20th, 2010, 06:48 PM. Reason: forgot code tags

        Comment


        • #5
          it worked !

          many thanks.

          i personally think that spring should provide some utility method which does staff, including the wiring and calling the right initialization methods.

          Comment


          • #6
            rwinch,
            I like your approach. I am trying to do something like this to augment functionality in Spring Web Flow, essentially to load flow definitions at run time from an external source (i.e. not configured into the webflow xml). The SWF project uses a FactoryBean to create a FlowDefinitionRegistry bean.
            Are you able to clarify a few questions?

            1) What happens if SWF generates the bean via the factory, and my custom java configuration code also does this? Does the order of instantiation determine which bean is the one left in the Bean Registry?
            2) Is it possible to trigger re-configurations of factory beans? I want to basically recreate a singleton bean using an event trigger.

            It's worth noting that both the FactoryBean and the BeanDefinition exist already in the SWF project; what I'm seeking to do to configure the factory myself, both at startup and on demand.

            Thanks in advance.

            Comment


            • #7
              I'm not sure that I follow your post entirely, so bare with me if it sounds as though I did not understand your goals. It seems as though you would like to configure a webflow externally. This post is primarily how to create a bean that implements InitializingBean using the Java Config. In summary, Spring does not call the afterPropertiesSet method so you are required to do this manually before returning the bean.

              I have answered the specific questions of your post below, but I think your best bet is to search the webflow forums and see if there is a posting that already answers your question (i.e. maybe this one?). If you cannot find the answer, I would post a question to the webflow forum.

              Originally posted by a_fitz View Post
              1) What happens if SWF generates the bean via the factory, and my custom java configuration code also does this? Does the order of instantiation determine which bean is the one left in the Bean Registry?
              Both beans will exist, the one that is used depends on how the beans were wired.

              Originally posted by a_fitz View Post
              2) Is it possible to trigger re-configurations of factory beans? I want to basically recreate a singleton bean using an event trigger.
              You can do this by performing a refresh on the applicationcontext, but this can be a fairly costly operation (time intensive).

              Originally posted by a_fitz View Post
              It's worth noting that both the FactoryBean and the BeanDefinition exist already in the SWF project; what I'm seeking to do to configure the factory myself, both at startup and on demand.
              Reconfiguring the factory after startup will most likely not do anything since the bean definitions are cached for performance reasons.

              Regards,
              Rob

              Comment


              • #8
                Rob,
                thanks very much for the reply. You have helped to cement some ideas in my mind. I had looked at the post you linked to from the Webflow forum; unfortunately the suggestions in that post make use of classes and interfaces that no longer exist in SWF2.
                I have also looked at the ApplicationContext refresh as a possible avenue; I think this will fit for a staging system but not in production; it appears that JMX is the ideal avenue for updating online beans in a production system although I am yet to find a clear description of an implementation using recent (non-deprecated) spring + SWF releases.

                thanks again.

                Comment

                Working...
                X