I need to know how applicatoncontextaware works. I have applicationContext.xml which has some import resource(another applicationContext). I need to use the applicationContext.xml in my java class to use the spring beans in it.
I came to know the applicationcontextaware class which is used to get the spring beans inside java class.Applicationaware has the set and getapplicationcontext() methods. getapplicationcontext() is defined as static.
How do the applicationcontextware loads the applicationContext.xml? whether do i need to give the location of applicationContext.xml so that applicationcontextaware loads? How can i use it in my java class?
You are confusing few things. First of all we are talking about
ApplicationContextAwareclass, right? It has only one method:Which you usually implement like this:
However you rarely need to access
ApplicationContextdirectly. Typically you start it once and let beans populate themselves automatically.Here you go:
Note that you don’t have to mention files already included in
applicationContext.xml. Now you can simply fetch one bean by name or type:Note that there are tons of ways to start Spring – using
ContextLoaderListener,@Configurationclass, etc.