Alex Alex - 2 years ago 133
Java Question

How to allocate spring-context?

I am absolutely confused with application context in spring. If i use spring (simple spring) create a beans.xml and then invoke Application context from (for example) main() method.

ApplicationContext context = new FileSystemXmlApplicationContext

all works good. But i don't understand if i move file on directory above or in another directory(for example) it will be ok?

in spring-mvc there is context for each DispatcherServlet which i create and where i specify some beans, there is common context for all servlets, how to specify this? in web.xml?
in general, please explain me this moment (i read spring in action, i undesrstand almost all, but these tricky moment don't show here.

sorry for my bad english in advance

mxf mxf
Answer Source

From FileSystemXmlApplicationContext java doc:

Standalone XML application context, taking the context definition files from the file system or from URLs, interpreting plain paths as relative file system locations (e.g. "mydir/myfile.txt"). Useful for test harnesses as well as for standalone environments.

The key words here are context definition files, so you can pass paths to as many xml-files, as you want. Besides that, you can create an application context and pass it to the new one as a parent:

FileSystemXmlApplicationContext(String[] configLocations, ApplicationContext parent)

Thus you can easily create the needed hierarchy of contexts.

       ApplicationContext parentContext = new FileSystemXmlApplicationContext

       ApplicationContext childContext = new FileSystemXmlApplicationContext
            (new String[]{"C:/some/path/ChildBeans1.xml", "C:/some/path/ChildBeans2.xml"}, parentContext);

if i move file on directory above all in another directory(for example) it will be ok?

As long as your path to file is correct and reachable - it's Ok.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download