ApplicationContext 종류
ApplicationContext 생성하기
ApplicationContext context = new ClassPathXmlApplicationContext("config.xml");
스프링 IOC 컨테이너에 적재되어 있는 BEAN 추출 스프링 3.xx 버전대 이전까지.
ApplicationContext applicationcontext = new ClassPathXmlApplicationContext("beans/eims.xml");
NjfcIoLayoutRepository bb = applicationcontext.getBean(NjfcIoLayoutRepository.class);
List<Map<String, Object>> list = bb.select(new HashMap());
스프링 부트 환경에서 추출
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class ApplicationContextProvider implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext ctx) throws BeansException {
applicationContext = ctx;
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
}
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.springframework.context.ApplicationContext;
public class DynamicBean {
public void dynamic_invoke() throws NoSuchMethodException, SecurityException, ClassNotFoundException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
String param1="", param2="", param3="";
//1.스프링 IOC 컨테이너에 적재 되어 있는 Bean 추출을 위해 ApplicationContext를 생성.
ApplicationContext applicationContext =
ApplicationContextProvider.getApplicationContext();
//2.Class Full package 문자열로 Class 생성. (확장자 제외)
Class clazz = Class.forName("org.example.springboot.web.Hello");
//3.컨테이너에서 clazz Type으로 Bean 추출.
Object service = applicationContext.getBean(clazz);
//4.메소드 생성
Method method = clazz.getMethod("afterAppr", String.class, String.class, String.class);
//5.메소드 실행.
method.invoke(service, param1, param2, param3);
}
}