witek010
witek010

Reputation: 369

Spring context inheritance

I have configuration in one file: applicationContext.xml. I have data source, session factory and two other beans.

I want change configuration like this:

How can I do that? I found information about inherit beans, but I want three another files not one

Upvotes: 4

Views: 3934

Answers (2)

gnomie
gnomie

Reputation: 439

In principle an application context can have a parent context and simply inherits all beans from there. What is not completely trivial (unless it changed recently) is how to contruct the relationship.

This helped me:

http://blog.springsource.org/2007/06/11/using-a-shared-parent-application-context-in-a-multi-war-spring-application/

(I am in no affiliation with the author).

Another approach is to actually write code to do this. If you have a Web app scenario, you can extend Spring's ContextLoaderListener and overwrite loadParentContext:

public class ComponentParentContextContextLoaderListener extends ContextLoaderListener {

   @Override
   protected ApplicationContext loadParentContext(ServletContext servletContext) {
        // load parent context .e.g from class path / static member
        return ac;
    }

}

Upvotes: 0

Peter Bratton
Peter Bratton

Reputation: 6408

Have a look at the import tag in Spring. You can use it in bean1.xml and bean2.xml to import your top-level applicationContext.xml config file, so the two configurations will be separate when loaded.

Upvotes: 1

Related Questions