f. Bean Post Processors - kimxavi/spring_tutorial GitHub Wiki
BeanPostProcessor๋ ์์ ์ ์ธ์คํด์คํ ๋ก์ง์ ์ ๊ณตํ๋ ๊ฒ๊ณผ ๊ทธ ์ธ dependency-resolution ๋ก์ง์ ๊ตฌํํ๋ ์ฝ๋ฐฑ ๋ฉ์๋๋ฅผ ์ ์ํ๋ค. ์คํ๋ง ์ปจํ ์ด๋๊ฐ ์ธ์คํด์คํ๋ฅผ ๋๋ด๊ณ , ์ค์ ํ๊ณ , ํ๋ ์ด์์ BeanPostProcessor ๊ตฌํ์ ๋ํ bean์ ์ด๊ธฐํ ์ ๊ฐ์ ์ปค์คํ ๋ก์ง์ ๊ตฌํํ ์ ์๋ค. ๋ง์ BeanPostProcessor ๋ฅผ ์ค์ ํ ์ ์๊ณ Ordered ์ธํฐํ์ด์ค๋ฅผ implementsํ ์ธํ ์ ํ๋กํผํฐ์ ์ธํ ํจ์ผ๋ก์จ ์คํ ์์๋ฅผ ์ค ์ ์๋ค. BeanPostProcessors๋ bean ์ด๋ ๊ฐ์ฒด ์ธ์คํด์ค์์ ์ด์๋๋ค. ์ด๊ฒ์ ์คํ๋ง IoC ์ปจํ ์ด๋๊ฐ bean ์ธ์คํด์คํ ํ๊ณ ๋ค์์ BeanPostProcessor ์ธํฐํ์ด์ค๋ค์ด ๊ทธ ๋ค์ ์ผ์ ์คํํ๋ค.
ApplicationContext๋ BeanPostProcessor ์ธํฐํ์ด์ค๋ฅผ ์์๋ฐ์ bean์ ์๋์ผ๋ก ๊ฐ์งํ๋ค. ๊ทธ๋ฆฌ๊ณ ์ปจํ ์ด๋์์ bean์ด ์์ฑ๋ ๋ ์ ์ ํ๊ฒ ํธ์ถ๋๋ post-processors ๋ก์จ bean๋ค์ ๋ฑ๋กํ๋ค.
HelloWorld.java
package com.tutorialspoint;
public class HelloWorld {
private String message;
public void setMessage(String message){
this.message = message;
}
public void getMessage(){
System.out.println("Your Message : " + message);
}
public void init(){
System.out.println("Bean is going through init.");
}
public void destroy(){
System.out.println("Bean will destroy now.");
}
}
- ์ด๊ฒ์ ์์ฃผ ๊ฐ๋จํ BeanPostProcessor ๊ตฌํ์ด๋ค. ์ด ๊ฒ์ bean์ ์ด๋ฆ์ ์ด๋ ํ bean์ด๋ ์ด๊ธฐํ ์ ํ์ ํ๋ฆฐํธํด์ค๋ค.
InitHelloWorld.java
package com.tutorialspoint;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.BeansException;
public class InitHelloWorld implements BeanPostProcessor {
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("BeforeInitialization : " + beanName);
return bean; // you can return any other object as well
}
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("AfterInitialization : " + beanName);
return bean; // you can return any other object as well
}
}
MainApp.java
package com.tutorialspoint;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
AbstractApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
obj.getMessage();
context.registerShutdownHook();
}
}
Beans.xml
<?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="helloWorld" class="com.tutorialspoint.HelloWorld"
init-method="init" destroy-method="destroy">
<property name="message" value="Hello World!"/>
</bean>
<bean class="com.tutorialspoint.InitHelloWorld" />
</beans>
BeforeInitialization : helloWorld
Bean is going through init.
AfterInitialization : helloWorld
Your Message : Hello World!
Bean will destroy now.