Issue
I'm trying to learn Spring Core and I'm styuding Spring in Action, Fourth Edition book.
in this book, I see:
AnnotationConfigApplicationContext—Loads a Spring application context from one or more Java-based configuration classes.
AnnotationConfigWebApplicationContext—Loads a Spring web application context from one or more Java-based configuration classes.
ClassPathXmlApplicationContext—Loads a context definition from one or more XML files located in the classpath, treating context-definition files as classpath resources.
FileSystemXmlApplicationContext—Loads a context definition from one or more XML files in the filesystem
XmlWebApplicationContext—Loads context definitions from one or more XML files contained in a web application
My question is about differences between the first two definitions and the last three definition. those first two definitions load a Spring Application Context and those last three definitions load a context definition.
what is the difference between Application Context and context definition?
and :
treating context-definition files as classpath resources
what does this sentence mean? what is context-definition file? why we should load them?
we loaded Application Context from configuration file. it's ok. but why we should load context definition when we have context-definition file?!
Solution
An ApplicationContext is an interface for a class that provides resources to your application through dependency injection.
AnnotationConfigApplicationContext, AnnotationConfigApplicationContext, ClassPathXmlApplicationContext, FileSystemXmlApplicationContext, and XmlWebApplicationContext are all implementations of that interface.
If you look carefully at the names of these classes, you will see that each takes two things into account:
- Where does the definition of the context come from?
- What type of application is the context being provided for?
So, the AnnotationConfig* contexts get the context definition (which beans should be provided) from annotations in the source code, while Xml contexts have the definition defined in XML, and the location of that XML differs, i.e., classpath, file system, web-inf resources, respectively.
The documentation that you quoted confuses these issues somewhat.
The Annotation-based contexts are described as loading a context, while what they are actually doing is introspecting annotations (which act as the context definition), and building an instance of one of the context classes.
The XML-based contexts are described as loading a context definition, which is correct (the xml file is the context definition), but incomplete description. They are loading an xml file that describes what beans to provide, and then building an instance of one of the context classes.
XML files were the original way to specify beans to be provided by the context, and as far as I have seen, are not used in modern applications. I'm sure that there are exceptions out there, but every app that I've seen (and I'm a consultant - I've seen quite a few) use annotations.
Answered By - GreyBeardedGeek
Answer Checked By - Timothy Miller (JavaFixing Admin)