BAEL-3832: added setters and getters and changed the testcases accordingly
This commit is contained in:
parent
f82ce12686
commit
cf216b69d6
|
@ -1,14 +1,21 @@
|
|||
package com.baeldung.ioccontainer.bean;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
|
||||
public class CustomBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
|
||||
public static boolean isBeanInstantiated = false;
|
||||
private static boolean isBeanFactoryPostProcessorRegistered = false;
|
||||
|
||||
@Override
|
||||
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
|
||||
isBeanInstantiated = true;
|
||||
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory){
|
||||
setBeanFactoryPostProcessorRegistered(true);
|
||||
}
|
||||
|
||||
public static boolean isBeanFactoryPostProcessorRegistered() {
|
||||
return isBeanFactoryPostProcessorRegistered;
|
||||
}
|
||||
|
||||
public static void setBeanFactoryPostProcessorRegistered(boolean isBeanFactoryPostProcessorRegistered) {
|
||||
CustomBeanFactoryPostProcessor.isBeanFactoryPostProcessorRegistered = isBeanFactoryPostProcessorRegistered;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,14 +1,21 @@
|
|||
package com.baeldung.ioccontainer.bean;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||
|
||||
public class CustomBeanPostProcessor implements BeanPostProcessor {
|
||||
public static boolean isBeanInstantiated = false;
|
||||
private static boolean isBeanPostProcessorRegistered = false;
|
||||
|
||||
@Override
|
||||
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
|
||||
isBeanInstantiated = true;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,9 +1,17 @@
|
|||
package com.baeldung.ioccontainer.bean;
|
||||
|
||||
public class Student {
|
||||
public static boolean isBeanInstantiated = false;
|
||||
private static boolean isBeanInstantiated = false;
|
||||
|
||||
public void postConstruct() {
|
||||
isBeanInstantiated = true;
|
||||
setBeanInstantiated(true);
|
||||
}
|
||||
|
||||
public static boolean isBeanInstantiated() {
|
||||
return isBeanInstantiated;
|
||||
}
|
||||
|
||||
public static void setBeanInstantiated(boolean isBeanInstantiated) {
|
||||
Student.isBeanInstantiated = isBeanInstantiated;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,9 +23,9 @@ public class IOCContainerAppUnitTest {
|
|||
@BeforeEach
|
||||
@AfterEach
|
||||
public void resetInstantiationFlag() {
|
||||
Student.isBeanInstantiated = false;
|
||||
CustomBeanPostProcessor.isBeanInstantiated = false;
|
||||
CustomBeanFactoryPostProcessor.isBeanInstantiated = false;
|
||||
Student.setBeanInstantiated(false);
|
||||
CustomBeanPostProcessor.setBeanPostProcessorRegistered(false);
|
||||
CustomBeanFactoryPostProcessor.setBeanFactoryPostProcessorRegistered(false);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -33,24 +33,23 @@ public class IOCContainerAppUnitTest {
|
|||
Resource res = new ClassPathResource("ioc-container-difference-example.xml");
|
||||
BeanFactory factory = new XmlBeanFactory(res);
|
||||
|
||||
assertFalse(Student.isBeanInstantiated);
|
||||
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);
|
||||
assertTrue(Student.isBeanInstantiated());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAppContInitialized_thenStudentInitialized() {
|
||||
ApplicationContext context = new ClassPathXmlApplicationContext("ioc-container-difference-example.xml");
|
||||
|
||||
assertTrue(Student.isBeanInstantiated);
|
||||
assertTrue(Student.isBeanInstantiated());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -58,16 +57,8 @@ public class IOCContainerAppUnitTest {
|
|||
Resource res = new ClassPathResource("ioc-container-difference-example.xml");
|
||||
ConfigurableListableBeanFactory factory = new XmlBeanFactory(res);
|
||||
|
||||
assertFalse(CustomBeanFactoryPostProcessor.isBeanInstantiated);
|
||||
assertFalse(CustomBeanPostProcessor.isBeanInstantiated);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAppContInitialized_thenBFPostProcessorAndBPostProcessorRegisteredAutomatically() {
|
||||
ApplicationContext context = new ClassPathXmlApplicationContext("ioc-container-difference-example.xml");
|
||||
|
||||
assertTrue(CustomBeanFactoryPostProcessor.isBeanInstantiated);
|
||||
assertTrue(CustomBeanPostProcessor.isBeanInstantiated);
|
||||
assertFalse(CustomBeanFactoryPostProcessor.isBeanFactoryPostProcessorRegistered());
|
||||
assertFalse(CustomBeanPostProcessor.isBeanPostProcessorRegistered());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -77,12 +68,19 @@ public class IOCContainerAppUnitTest {
|
|||
|
||||
CustomBeanFactoryPostProcessor beanFactoryPostProcessor = new CustomBeanFactoryPostProcessor();
|
||||
beanFactoryPostProcessor.postProcessBeanFactory(factory);
|
||||
assertTrue(CustomBeanFactoryPostProcessor.isBeanInstantiated);
|
||||
assertTrue(CustomBeanFactoryPostProcessor.isBeanFactoryPostProcessorRegistered());
|
||||
|
||||
CustomBeanPostProcessor beanPostProcessor = new CustomBeanPostProcessor();
|
||||
factory.addBeanPostProcessor(beanPostProcessor);
|
||||
Student student = (Student) factory.getBean("student");
|
||||
assertTrue(CustomBeanPostProcessor.isBeanInstantiated);
|
||||
assertTrue(CustomBeanPostProcessor.isBeanPostProcessorRegistered());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAppContInitialized_thenBFPostProcessorAndBPostProcessorRegisteredAutomatically() {
|
||||
ApplicationContext context = new ClassPathXmlApplicationContext("ioc-container-difference-example.xml");
|
||||
|
||||
assertTrue(CustomBeanFactoryPostProcessor.isBeanFactoryPostProcessorRegistered());
|
||||
assertTrue(CustomBeanPostProcessor.isBeanPostProcessorRegistered());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue