Announcement Announcement Module
No announcement yet.
How to define a bean at runtime in any ApplicationContext Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to define a bean at runtime in any ApplicationContext


    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,

  • #2
    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:
    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:

        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;
            return myServletBean;
    Comments are welcome!