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

  • Adding beans to the root ApplicationContex ?

    Hellu,

    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:
    Code:
    <context-param>
    	<param-name>contextConfigLocation</param-name>
    	<param-value>classpath:spring/default/index-*.xml</param-value>
    </context-param>
    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):
    Code:
    <bean id="bv-web-app" class="org.springframework.context.support.ClassPathXmlApplicationContext">
     <constructor-arg>
       <list>
    	<value>classpath*:spring/default/index-*.xml</value>
    	<value>classpath*:spring/${app.environment}/index-*.xml</value>
       </list>
     </constructor-arg>
    </bean>
    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?
    Ed

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

    Too bad.....
    Tanks
    Ed

    Comment


    • #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?

      Comment


      • #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.

        Comment


        • #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.

          Comment


          • #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: http://blog.interface21.com/main/200...g-application/

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

            Cheers,
            Ed

            Comment

            Working...
            X