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;
|
package com.baeldung.ioccontainer.bean;
|
||||||
|
|
||||||
import org.springframework.beans.BeansException;
|
|
||||||
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
|
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
|
||||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||||
|
|
||||||
public class CustomBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
|
public class CustomBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
|
||||||
public static boolean isBeanInstantiated = false;
|
private static boolean isBeanFactoryPostProcessorRegistered = false;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
|
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory){
|
||||||
isBeanInstantiated = true;
|
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;
|
package com.baeldung.ioccontainer.bean;
|
||||||
|
|
||||||
import org.springframework.beans.BeansException;
|
|
||||||
import org.springframework.beans.factory.config.BeanPostProcessor;
|
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||||
|
|
||||||
public class CustomBeanPostProcessor implements BeanPostProcessor {
|
public class CustomBeanPostProcessor implements BeanPostProcessor {
|
||||||
public static boolean isBeanInstantiated = false;
|
private static boolean isBeanPostProcessorRegistered = false;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
|
public Object postProcessBeforeInitialization(Object bean, String beanName){
|
||||||
isBeanInstantiated = true;
|
setBeanPostProcessorRegistered(true);
|
||||||
return bean;
|
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;
|
package com.baeldung.ioccontainer.bean;
|
||||||
|
|
||||||
public class Student {
|
public class Student {
|
||||||
public static boolean isBeanInstantiated = false;
|
private static boolean isBeanInstantiated = false;
|
||||||
|
|
||||||
public void postConstruct() {
|
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
|
@BeforeEach
|
||||||
@AfterEach
|
@AfterEach
|
||||||
public void resetInstantiationFlag() {
|
public void resetInstantiationFlag() {
|
||||||
Student.isBeanInstantiated = false;
|
Student.setBeanInstantiated(false);
|
||||||
CustomBeanPostProcessor.isBeanInstantiated = false;
|
CustomBeanPostProcessor.setBeanPostProcessorRegistered(false);
|
||||||
CustomBeanFactoryPostProcessor.isBeanInstantiated = false;
|
CustomBeanFactoryPostProcessor.setBeanFactoryPostProcessorRegistered(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -33,24 +33,23 @@ public class IOCContainerAppUnitTest {
|
||||||
Resource res = new ClassPathResource("ioc-container-difference-example.xml");
|
Resource res = new ClassPathResource("ioc-container-difference-example.xml");
|
||||||
BeanFactory factory = new XmlBeanFactory(res);
|
BeanFactory factory = new XmlBeanFactory(res);
|
||||||
|
|
||||||
assertFalse(Student.isBeanInstantiated);
|
assertFalse(Student.isBeanInstantiated());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenBFInitialized_thenStudentInitialized() {
|
public void whenBFInitialized_thenStudentInitialized() {
|
||||||
|
|
||||||
Resource res = new ClassPathResource("ioc-container-difference-example.xml");
|
Resource res = new ClassPathResource("ioc-container-difference-example.xml");
|
||||||
BeanFactory factory = new XmlBeanFactory(res);
|
BeanFactory factory = new XmlBeanFactory(res);
|
||||||
Student student = (Student) factory.getBean("student");
|
Student student = (Student) factory.getBean("student");
|
||||||
|
|
||||||
assertTrue(Student.isBeanInstantiated);
|
assertTrue(Student.isBeanInstantiated());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenAppContInitialized_thenStudentInitialized() {
|
public void whenAppContInitialized_thenStudentInitialized() {
|
||||||
ApplicationContext context = new ClassPathXmlApplicationContext("ioc-container-difference-example.xml");
|
ApplicationContext context = new ClassPathXmlApplicationContext("ioc-container-difference-example.xml");
|
||||||
|
|
||||||
assertTrue(Student.isBeanInstantiated);
|
assertTrue(Student.isBeanInstantiated());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -58,16 +57,8 @@ public class IOCContainerAppUnitTest {
|
||||||
Resource res = new ClassPathResource("ioc-container-difference-example.xml");
|
Resource res = new ClassPathResource("ioc-container-difference-example.xml");
|
||||||
ConfigurableListableBeanFactory factory = new XmlBeanFactory(res);
|
ConfigurableListableBeanFactory factory = new XmlBeanFactory(res);
|
||||||
|
|
||||||
assertFalse(CustomBeanFactoryPostProcessor.isBeanInstantiated);
|
assertFalse(CustomBeanFactoryPostProcessor.isBeanFactoryPostProcessorRegistered());
|
||||||
assertFalse(CustomBeanPostProcessor.isBeanInstantiated);
|
assertFalse(CustomBeanPostProcessor.isBeanPostProcessorRegistered());
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenAppContInitialized_thenBFPostProcessorAndBPostProcessorRegisteredAutomatically() {
|
|
||||||
ApplicationContext context = new ClassPathXmlApplicationContext("ioc-container-difference-example.xml");
|
|
||||||
|
|
||||||
assertTrue(CustomBeanFactoryPostProcessor.isBeanInstantiated);
|
|
||||||
assertTrue(CustomBeanPostProcessor.isBeanInstantiated);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -77,12 +68,19 @@ public class IOCContainerAppUnitTest {
|
||||||
|
|
||||||
CustomBeanFactoryPostProcessor beanFactoryPostProcessor = new CustomBeanFactoryPostProcessor();
|
CustomBeanFactoryPostProcessor beanFactoryPostProcessor = new CustomBeanFactoryPostProcessor();
|
||||||
beanFactoryPostProcessor.postProcessBeanFactory(factory);
|
beanFactoryPostProcessor.postProcessBeanFactory(factory);
|
||||||
assertTrue(CustomBeanFactoryPostProcessor.isBeanInstantiated);
|
assertTrue(CustomBeanFactoryPostProcessor.isBeanFactoryPostProcessorRegistered());
|
||||||
|
|
||||||
CustomBeanPostProcessor beanPostProcessor = new CustomBeanPostProcessor();
|
CustomBeanPostProcessor beanPostProcessor = new CustomBeanPostProcessor();
|
||||||
factory.addBeanPostProcessor(beanPostProcessor);
|
factory.addBeanPostProcessor(beanPostProcessor);
|
||||||
Student student = (Student) factory.getBean("student");
|
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