Home  >  Spring Core

Example of BeanClassLoaderAware in Spring

By Arvind Rai, April 20, 2013
On this page we will provide example of BeanClassLoaderAware in spring. If a bean in spring implements BeanClassLoaderAware then that bean has to implement a method that is setBeanClassLoader. And when that bean is loaded in spring container, the setBeanClassLoader is called. BeanClassLoaderAware belongs to the package org.springframework.beans.factory. BeanClassLoaderAware awares the bean for its ClassLoader.
A.java
package com.concretepage;
import org.springframework.beans.factory.BeanClassLoaderAware;
public class A  implements BeanClassLoaderAware{
	public A(){
		System.out.println("Bean A is Initialized.");
	}
	@Override
	public void setBeanClassLoader(ClassLoader classLoader) {
		System.out.println("parent class loader:"+classLoader.getParent());
	}
} 




app-conf.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-3.0.xsd ">
  
    <bean id="testA" class="com.concretepage.A"/>
      
</beans> 
SpringTest.java
package com.concretepage;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringTest {
	public static void main(String[] args) {
		AbstractApplicationContext  context = new ClassPathXmlApplicationContext("app-conf.xml");		
		context.registerShutdownHook();
	}
}  
Output
Bean A is Initialized.
parent class loader:sun.misc.Launcher$ExtClassLoader@4aad3ba4 
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us