Announcement Announcement Module
No announcement yet.
Adding beans to the root ApplicationContex ? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Adding beans to the root ApplicationContex ?


    I have several spring components with their own spring configuration.
    I use these components in my web app and as such need to load all their spring config files. I can do that in my web.xml as follows:
    The problem with this is that I like load different spring config files, depending on the environment I am running in (set as JVM param).
    Normally I do this like this (contained in the spring config, referenced in the web.xml):
    <bean id="bv-web-app" class="">
    But, I can't do that in this case as I am using the DispatcherServlet that inherits the root Context but of course can't find the beans loaded above, as they are contained in a child of the root context.

    So what I want: add these beans to the root context instead as to load them in a new context and add this as a child.
    How can I do that ?

    Please some advice?

  • #2
    So I have to assume this is simple not possible with Spring ?

    Too bad.....


    • #3
      I for one don't understand what you need. Each DispatcherServlet has its own context (with separately configured context), and each one has to be added to web.xml - there is no way for a servlet container to magically create new servlets for you depending on what is on the classpath. Do you see why I am confused?


      • #4
        Thanx for ur answer.

        Let me try again:
        I want to create the root context in a spring config file (instead of the spring config that forms the root context) , as I need spring's PropertyPlaceholderConfigurer to find and load all spring config.

        If you do this in the root spring config, as above, the created context is a child of the root contex, and as such unreachable of the dispatcher context that is also a child of the root context.

        I know how to do it myself by making ur own context class that overrides an existing one... But I am suprised that something like this doesn't exist.

        Let me put it another way:
        If I could use ${...} property place holders in my web.xml just like you can in the spring config files, I wouldn't ask you the above, but this isn't possible ofcourse....

        I hope you understand this.


        • #5
          I'm still confused, but I'm starting to think this has nothing to do with DispatcherServlet. As a point of clarification: creating an ApplicationContext in a bean definition does not end up with it as the child context of the context it was created in. You might be expecting some magic behaviour from the configuration that you aren't seeing?

          If I understand what you want to do, I would suggest that you look into the javadocs of ContextLoader. If you define your "bv-web-app" in a beanRefContext.xml then refer to it with the parentContextKey servlet context param, then it will itself form the parent of the "root" context loaded by the ContextLoaderListener. All this happens independent of any DispatcherServlet instances you might also have.


          • #6
            Thanks Dave,

            Thanks for the hint, this was new to me.
            With your help I could search a bit precise and found the exact solution of what I want here:

            I haven't tried it yet but I am confinsed it will work..