ApplicationContextAware in spring


 

ApplicationContextAware in spring

In this tutorial you will learn about ApplicationContextAware Interface of Spring Framework.

In this tutorial you will learn about ApplicationContextAware Interface of Spring Framework.

ApplicationContextAware in spring

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"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  <bean id="studentBean" class="com.roseindia.common.StudentBean" />
</beans>

When you run this application it will display message as shown below:


org[email protected]18a47e0: startup date [Fri Sep 03 16:15:55 GMT+05:30 2010]; root of
context hierarchy

Download this example code

Ads