Reputation: 369
I have configuration in one file: applicationContext.xml. I have data source, session factory and two other beans.
I want change configuration like this:
applicationContext.xml -> parent
, which contains data source, session factorybean1.xml -> children
, which inherit from applicationContext and contains only info about bean1bean2.xml -> children
, which inherit from applicationContext and contains only info about bean2How can I do that? I found information about inherit beans, but I want three another files not one
Upvotes: 4
Views: 3934
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:
(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
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