Home Tutorial Spring Spring3 Ioc ApplicationContextAware in spring

 
 

ApplicationContextAware in spring
Posted on: September 3, 2010 at 12:00 AM
Advertisement
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.springframework.context.support.ClassPathXmlApplicationContext@18a47e0: startup date [Fri Sep 03 16:15:55 GMT+05:30 2010]; root of
context hierarchy

Download this example code

Advertisement

Related Tags for ApplicationContextAware in spring :


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: September 3, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com