Announcement Announcement Module
Collapse
No announcement yet.
Dynamically adding a bean to the ApplicationContext Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Dynamically adding a bean to the ApplicationContext

    Is it possible to dynamically add a bean to the application context? My application has to support registration of XTensions at run time. My limited knowledge of Spring says that only those beans are registered with application context which are defined in the context (XML) file. Suppose a new bean is available for service (may be on separate machine). How can I register this new bean with my application context?
    My requirement could be analogous to adding a new bean tag in the context file at run time.
    Thanks,
    Kapil

  • #2
    Please see this thread which shows how to use the ConfigurableApplicationContext:

    http://forum.springframework.org/sho...getBeanFactory

    Comment


    • #3
      I tried to register my bean using registerBeanDefinition API but received following error on calling the getBean API of the applicationContext.

      org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'testService' defined in null:
      Instantiation of bean failed; nested exception is java.lang.SecurityException: Can not make a java.lang.Class constructor accessible
      java.lang.SecurityException: Can not make a java.lang.Class constructor accessible

      Please help,
      Kapil

      Comment


      • #4
        Finally am able to dynamically register bean at runtime. I was making the mistake of passing object instead of Class object to the RootBeanDefinition constructor.

        The dynamic registration worked for the beans in the same JVM.
        Is it possible to register a bean running in separate JVM?

        Comment


        • #5
          it depends on what exactly are your requirements. If you have the 'connection' mechanism in place and you can work with the remote application context you can do also registration. However, Spring doesn't has support for clustered or distributed application context. There are some products out there that add such functionality but most of them are commercial.

          Comment


          • #6
            I was able to register bean running on different JVM using the same approach of using registerBeanDefinition() api and specifying the property values for serviceURL and interface in the RootBeanDefinition object.

            While creating rmi proxy, why can't a bean specify more than one interface in the serviceInterface property? Actually my class is implementing two interfaces and I want to typecast it on client side in one of the two interfaces depending on some condition.
            Thanks,
            Kapil

            Comment

            Working...
            X