BAEL-3832: Difference between BeanFactory and ApplicationContext completed
This commit is contained in:
parent
505a0c9547
commit
2eded21258
|
@ -67,6 +67,12 @@
|
|||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>log4j</groupId>
|
||||
<artifactId>log4j</artifactId>
|
||||
<version>${log4j.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -83,6 +89,7 @@
|
|||
<maven.surefire.version>2.22.1</maven.surefire.version>
|
||||
<annotation-api.version>1.3.2</annotation-api.version>
|
||||
<spring.boot.version>2.2.2.RELEASE</spring.boot.version>
|
||||
<log4j.version>1.2.17</log4j.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,16 @@
|
|||
package com.baeldung.ioccontainer.bean;
|
||||
|
||||
import org.apache.log4j.LogManager;
|
||||
import org.apache.log4j.Logger;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
|
||||
public class CustomBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
|
||||
static final Logger LOGGER = LogManager.getLogger(CustomBeanPostProcessor.class.getName());
|
||||
|
||||
@Override
|
||||
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
|
||||
LOGGER.info("BeanFactoryPostProcessor is Registered");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package com.baeldung.ioccontainer.bean;
|
||||
|
||||
import org.apache.log4j.LogManager;
|
||||
import org.apache.log4j.Logger;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||
|
||||
public class CustomBeanPostProcessor implements BeanPostProcessor {
|
||||
|
||||
static final Logger LOGGER = LogManager.getLogger(CustomBeanPostProcessor.class.getName());
|
||||
|
||||
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
|
||||
LOGGER.info("BeanPostProcessor is Registered Before Initialization");
|
||||
return bean;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package com.baeldung.ioccontainer.bean;
|
||||
|
||||
import org.apache.log4j.LogManager;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
public class Student {
|
||||
static final Logger LOGGER = LogManager.getLogger(Student.class.getName());
|
||||
|
||||
public void postConstruct() {
|
||||
LOGGER.info("Student Bean is initialized");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,137 @@
|
|||
package com.baeldung.ioccontainer;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.log4j.AppenderSkeleton;
|
||||
import org.apache.log4j.Logger;
|
||||
import org.apache.log4j.spi.LoggingEvent;
|
||||
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 IOCContainerAppTest {
|
||||
|
||||
private LogAppender logAppender;
|
||||
private List<LoggingEvent> loggingEvents;
|
||||
|
||||
@BeforeEach
|
||||
public void initializeLogAppender() {
|
||||
logAppender = new LogAppender();
|
||||
Logger.getRootLogger()
|
||||
.addAppender(logAppender);
|
||||
loggingEvents = logAppender.events;
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
public void removeLogAppender() {
|
||||
Logger.getRootLogger()
|
||||
.removeAppender(logAppender);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenBFInitialized_thenNoStudentLogPrinted() {
|
||||
Resource res = new ClassPathResource("ioc-container-difference-example.xml");
|
||||
BeanFactory factory = new XmlBeanFactory(res);
|
||||
|
||||
String expected = "Student Bean is initialized";
|
||||
assertFalse(checkWhetherLoggerContains(expected));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenBFInitialized_thenStudentLogPrinted() {
|
||||
|
||||
Resource res = new ClassPathResource("ioc-container-difference-example.xml");
|
||||
BeanFactory factory = new XmlBeanFactory(res);
|
||||
Student student = (Student) factory.getBean("student");
|
||||
|
||||
String expected = "Student Bean is initialized";
|
||||
assertTrue(checkWhetherLoggerContains(expected));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAppContInitialized_thenStudentObjInitialized() {
|
||||
ApplicationContext context = new ClassPathXmlApplicationContext("ioc-container-difference-example.xml");
|
||||
|
||||
String expected = "Student Bean is initialized";
|
||||
assertTrue(checkWhetherLoggerContains(expected));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenBFInitialized_thenBFPProcessorAndBPProcessorNotRegAutomatically() {
|
||||
Resource res = new ClassPathResource("ioc-container-difference-example.xml");
|
||||
ConfigurableListableBeanFactory factory = new XmlBeanFactory(res);
|
||||
|
||||
String beanFactoryPostProcessorExpectedLog = "BeanFactoryPostProcessor is Registered";
|
||||
assertFalse(checkWhetherLoggerContains(beanFactoryPostProcessorExpectedLog));
|
||||
|
||||
String beanPostProcessorExpectedLog = "BeanPostProcessor is Registered Before Initialization";
|
||||
assertFalse(checkWhetherLoggerContains(beanPostProcessorExpectedLog));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAppContInitialized_thenBFPostProcessorAndBPostProcessorRegisteredAutomatically() {
|
||||
ApplicationContext context = new ClassPathXmlApplicationContext("ioc-container-difference-example.xml");
|
||||
|
||||
String beanFactoryPostProcessorExpectedLog = "BeanFactoryPostProcessor is Registered";
|
||||
assertTrue(checkWhetherLoggerContains(beanFactoryPostProcessorExpectedLog));
|
||||
|
||||
String beanPostProcessorExpectedLog = "BeanPostProcessor is Registered Before Initialization";
|
||||
assertTrue(checkWhetherLoggerContains(beanPostProcessorExpectedLog));
|
||||
}
|
||||
|
||||
@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);
|
||||
String beanFactoryPostProcessorExpectedLog = "BeanFactoryPostProcessor is Registered";
|
||||
assertTrue(checkWhetherLoggerContains(beanFactoryPostProcessorExpectedLog));
|
||||
|
||||
CustomBeanPostProcessor beanPostProcessor = new CustomBeanPostProcessor();
|
||||
factory.addBeanPostProcessor(beanPostProcessor);
|
||||
Student student = (Student) factory.getBean("student");
|
||||
String beanPostProcessorExpectedLog = "BeanPostProcessor is Registered Before Initialization";
|
||||
assertTrue(checkWhetherLoggerContains(beanPostProcessorExpectedLog));
|
||||
}
|
||||
|
||||
private boolean checkWhetherLoggerContains(String expectedLogMessge) {
|
||||
boolean isLogExist = loggingEvents.stream()
|
||||
.anyMatch(logEvent -> logEvent.getMessage()
|
||||
.equals(expectedLogMessge));
|
||||
return isLogExist;
|
||||
}
|
||||
|
||||
public static class LogAppender extends AppenderSkeleton {
|
||||
public List<LoggingEvent> events = new ArrayList<LoggingEvent>();
|
||||
|
||||
public void close() {
|
||||
}
|
||||
|
||||
public boolean requiresLayout() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void append(LoggingEvent event) {
|
||||
events.add(event);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
<?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