In this tutorial you will see and example of using ApplicationContextAware. The ApplicationContextAware is used when an object required.
StudentBean.java
package com.roseindia.common;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class StudentBean implements ApplicationContextAware {
private ApplicationContext context;
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
context = applicationContext;
}
}
AppMain.java
package com.roseindia.common;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.ApplicationContext;
public class AppMain {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"context.xml");
StudentBean bean = (StudentBean) context.getBean("studentBean");
System.out.println(context);
}
}
context.xml
<beans xmlns="http://www.springframework.org/schema/beans"
|
|
org.springframework.context.support.ClassPathXmlApplicationContext@18a47e0:
startup date [Fri Sep 03 16:15:55 GMT+05:30 2010]; root of
context hierarchy |
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.