Merge pull request #8880 from vikasrajput6035/BAEL-3832
Bael 3832 : Difference between BeanFactory and ApplicationContext - First draft completed
This commit is contained in:
commit
5f19bb8dda
|
@ -0,0 +1,21 @@
|
||||||
|
package com.baeldung.ioccontainer.bean;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
|
||||||
|
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||||
|
|
||||||
|
public class CustomBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
|
||||||
|
private static boolean isBeanFactoryPostProcessorRegistered = false;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory){
|
||||||
|
setBeanFactoryPostProcessorRegistered(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isBeanFactoryPostProcessorRegistered() {
|
||||||
|
return isBeanFactoryPostProcessorRegistered;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setBeanFactoryPostProcessorRegistered(boolean isBeanFactoryPostProcessorRegistered) {
|
||||||
|
CustomBeanFactoryPostProcessor.isBeanFactoryPostProcessorRegistered = isBeanFactoryPostProcessorRegistered;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,21 @@
|
||||||
|
package com.baeldung.ioccontainer.bean;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||||
|
|
||||||
|
public class CustomBeanPostProcessor implements BeanPostProcessor {
|
||||||
|
private static boolean isBeanPostProcessorRegistered = false;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object postProcessBeforeInitialization(Object bean, String beanName){
|
||||||
|
setBeanPostProcessorRegistered(true);
|
||||||
|
return bean;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isBeanPostProcessorRegistered() {
|
||||||
|
return isBeanPostProcessorRegistered;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setBeanPostProcessorRegistered(boolean isBeanPostProcessorRegistered) {
|
||||||
|
CustomBeanPostProcessor.isBeanPostProcessorRegistered = isBeanPostProcessorRegistered;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,17 @@
|
||||||
|
package com.baeldung.ioccontainer.bean;
|
||||||
|
|
||||||
|
public class Student {
|
||||||
|
private static boolean isBeanInstantiated = false;
|
||||||
|
|
||||||
|
public void postConstruct() {
|
||||||
|
setBeanInstantiated(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isBeanInstantiated() {
|
||||||
|
return isBeanInstantiated;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setBeanInstantiated(boolean isBeanInstantiated) {
|
||||||
|
Student.isBeanInstantiated = isBeanInstantiated;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,86 @@
|
||||||
|
package com.baeldung.ioccontainer;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.AfterEach;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.BeanFactory;
|
||||||
|
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||||
|
import org.springframework.beans.factory.xml.XmlBeanFactory;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||||
|
import org.springframework.core.io.ClassPathResource;
|
||||||
|
import org.springframework.core.io.Resource;
|
||||||
|
|
||||||
|
import com.baeldung.ioccontainer.bean.CustomBeanFactoryPostProcessor;
|
||||||
|
import com.baeldung.ioccontainer.bean.CustomBeanPostProcessor;
|
||||||
|
import com.baeldung.ioccontainer.bean.Student;
|
||||||
|
|
||||||
|
public class IOCContainerAppUnitTest {
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
@AfterEach
|
||||||
|
public void resetInstantiationFlag() {
|
||||||
|
Student.setBeanInstantiated(false);
|
||||||
|
CustomBeanPostProcessor.setBeanPostProcessorRegistered(false);
|
||||||
|
CustomBeanFactoryPostProcessor.setBeanFactoryPostProcessorRegistered(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenBFInitialized_thenStudentNotInitialized() {
|
||||||
|
Resource res = new ClassPathResource("ioc-container-difference-example.xml");
|
||||||
|
BeanFactory factory = new XmlBeanFactory(res);
|
||||||
|
|
||||||
|
assertFalse(Student.isBeanInstantiated());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenBFInitialized_thenStudentInitialized() {
|
||||||
|
Resource res = new ClassPathResource("ioc-container-difference-example.xml");
|
||||||
|
BeanFactory factory = new XmlBeanFactory(res);
|
||||||
|
Student student = (Student) factory.getBean("student");
|
||||||
|
|
||||||
|
assertTrue(Student.isBeanInstantiated());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenAppContInitialized_thenStudentInitialized() {
|
||||||
|
ApplicationContext context = new ClassPathXmlApplicationContext("ioc-container-difference-example.xml");
|
||||||
|
|
||||||
|
assertTrue(Student.isBeanInstantiated());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenBFInitialized_thenBFPProcessorAndBPProcessorNotRegAutomatically() {
|
||||||
|
Resource res = new ClassPathResource("ioc-container-difference-example.xml");
|
||||||
|
ConfigurableListableBeanFactory factory = new XmlBeanFactory(res);
|
||||||
|
|
||||||
|
assertFalse(CustomBeanFactoryPostProcessor.isBeanFactoryPostProcessorRegistered());
|
||||||
|
assertFalse(CustomBeanPostProcessor.isBeanPostProcessorRegistered());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenBFPostProcessorAndBPProcessorRegisteredManually_thenReturnTrue() {
|
||||||
|
Resource res = new ClassPathResource("ioc-container-difference-example.xml");
|
||||||
|
ConfigurableListableBeanFactory factory = new XmlBeanFactory(res);
|
||||||
|
|
||||||
|
CustomBeanFactoryPostProcessor beanFactoryPostProcessor = new CustomBeanFactoryPostProcessor();
|
||||||
|
beanFactoryPostProcessor.postProcessBeanFactory(factory);
|
||||||
|
assertTrue(CustomBeanFactoryPostProcessor.isBeanFactoryPostProcessorRegistered());
|
||||||
|
|
||||||
|
CustomBeanPostProcessor beanPostProcessor = new CustomBeanPostProcessor();
|
||||||
|
factory.addBeanPostProcessor(beanPostProcessor);
|
||||||
|
Student student = (Student) factory.getBean("student");
|
||||||
|
assertTrue(CustomBeanPostProcessor.isBeanPostProcessorRegistered());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenAppContInitialized_thenBFPostProcessorAndBPostProcessorRegisteredAutomatically() {
|
||||||
|
ApplicationContext context = new ClassPathXmlApplicationContext("ioc-container-difference-example.xml");
|
||||||
|
|
||||||
|
assertTrue(CustomBeanFactoryPostProcessor.isBeanFactoryPostProcessorRegistered());
|
||||||
|
assertTrue(CustomBeanPostProcessor.isBeanPostProcessorRegistered());
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,10 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<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="student" class="com.baeldung.ioccontainer.bean.Student" init-method="postConstruct"/>
|
||||||
|
<bean id="customBeanPostProcessor" class="com.baeldung.ioccontainer.bean.CustomBeanPostProcessor" />
|
||||||
|
<bean id="customBeanFactoryPostProcessor" class="com.baeldung.ioccontainer.bean.CustomBeanFactoryPostProcessor" />
|
||||||
|
</beans>
|
Loading…
Reference in New Issue