Results 1 to 2 of 2

Thread: How to define a bean at runtime in any ApplicationContext

  1. #1
    Join Date
    Nov 2004
    Location
    Zurich [CH]
    Posts
    14

    Default How to define a bean at runtime in any ApplicationContext

    Hello,

    I using a "beanservlet" which has to be mapped in my webserver, but I don't want to define a "SimpleUrlHandlerMapping" in my "remote-servlet.xml" file which defines the "beanservlet". My "beanservlet" implements the interface "InitializingBean", so I wanna define a "handlerMapping" bean of type "SimpleUrlHandlerMapping" at runtime in method "afterPropertiesSet". But when I'm using the "StaticApplicationContext" I've got the problem that the parent "ApplicationContext" which is an "XmlWebApplicationContext" do not have access to this defined bean. Now I'd like to move these bean definitions from the "StaticApplicationContext" to its parent the "XmlWebApplicationContext". This problem lies nearly to the problem of cglib, which has to load a at runtime defined class in an existing classloader. I've tried to adapt this idea, but with no success. I've also tried to register the singleton bean directly by using the method "registerSingleton" of interface "ConfigurableListableBeanFactory". Afterwards it was visible, but when I tried to execute the method "initApplicationContext" of "SimpleUrlHandlerMapping" I got the exception that the singleton is not running in a ApplicationContext.

    Does anybody has a good idea to solve this problem?

    Thanks & cheers,
    Martin

  2. #2
    Join Date
    Nov 2004
    Location
    Zurich [CH]
    Posts
    14

    Default

    Now I've found a solution, which matchs to my needs. At the end of method afterPropertiesSet of the beanservlet I prepare the SimpleUrlHandlerMapping by creating an instance of it without Spring and register it as a singleton on the desired ApplicationContext. This looks like the following:
    Code:
    AbstractRefreshableWebApplicationContext parentAppContext 
            = (AbstractRefreshableWebApplicationContext) myApplicationContextWhichWasUsedToCreateTheCurrentBean;
    ConfigurableListableBeanFactory configurableBeanFactory = parentAppContext.getBeanFactory();
    configurableBeanFactory.registerSingleton("handlerMappingForBean" + myBeanName, myUrlMappingBean);
    An important thing is, that the beanservlet implements the interface FactoryBean. So you have to possibility to interact, when the bean initialization is finished. Before I return the object on method getObject I do some finally work like telling "myUrlMappingBean" to which ApplicationContext it belongs. This has to be done at this point to prevent a circular dependency. The code looks like the following:


    Code:
        private boolean myUrlMappingBeanInitialized = false
    
        public Object getObject() throws Exception {
            /**
             * If the url mapping is not already allocated to the application, this
             * will be done here. This allocation will be done only once.
             */
            if (myUrlMappingBean != null && !myUrlMappingBeanInitialized) {
                myUrlMappingBeanInitialized = true;
                m_urlMapping.setApplicationContext(myApplicationContextWhichWasUsedToCreateTheCurrentBean);
            }
    
            return myServletBean;
        }
    Comments are welcome!

    Cheers,
    Martin

Similar Threads

  1. Order of Bean definitions matters?
    By cfuser in forum Container
    Replies: 2
    Last Post: Oct 21st, 2005, 10:29 AM
  2. Spring container fails with no exception
    By naor in forum Container
    Replies: 9
    Last Post: Oct 1st, 2005, 03:39 PM
  3. EHCaching Hibernate
    By dencamel in forum Data
    Replies: 3
    Last Post: Sep 6th, 2005, 09:03 PM
  4. could not satisfy dependencies
    By springuser in forum Container
    Replies: 4
    Last Post: Apr 26th, 2005, 01:15 PM
  5. Replies: 1
    Last Post: Apr 25th, 2005, 07:37 PM

Posting Permissions

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