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. html
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. java
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? spring
You are confusing few things. First of all we are talking about ApplicationContextAware class, right? It has only one method: api
setApplicationContext(ApplicationContext applicationContext)
Which you usually implement like this: app
public class MyFancyBean implements ApplicationContextAware { private ApplicationContext applicationContext; void setApplicationContext(ApplicationContext applicationContext) { this.applicationContext = applicationContext; } public void businessMethod() { //use applicationContext somehow } }
However you rarely need to accessApplicationContextdirectly. Typically you start it once and let beans populate themselves automatically. ide
I need to use the applicationContext.xml in my java class to use the spring beans in it. fetch
Here you go: this
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
Note that you don't have to mention files already included inapplicationContext.xml. Now you can simply fetch one bean by name or type: spa
ctx.getBean("someName")
Note that there are tons of ways to start Spring - usingContextLoaderListener,@Configurationclass, etc. code