Home  >  Spring

Spring BeanClassLoaderAware Example

By Arvind Rai, October 29, 2021
On this page we will learn Spring BeanClassLoaderAware interface. The BeanClassLoaderAware allows a bean to be aware of the bean class loader used by present BeanFactory to load bean classes.
The BeanClassLoaderAware has following method.
void setBeanClassLoader(ClassLoader classLoader) 
This is the callback method to supply bean class loader to a bean instance. This method is invoked after the population of normal bean properties but before an initialization callback.



Implement BeanClassLoaderAware Interface

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.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 

Reference

Interface BeanClassLoaderAware
POSTED BY
ARVIND RAI
ARVIND RAI










©2022 concretepage.com | Privacy Policy | Contact Us