137 lines
4.1 KiB
Java
137 lines
4.1 KiB
Java
/************************************************************
|
|
*
|
|
* @packageName : io.company.localhost.utils
|
|
* @fileName : ContextUtil.java
|
|
* @author : 조인제
|
|
* @date : 24.12.06
|
|
* @description :
|
|
*
|
|
* ===========================================================
|
|
* DATE AUTHOR NOTE
|
|
* -----------------------------------------------------------
|
|
* 24.12.06 조인제 최초 생성
|
|
*
|
|
*************************************************************/
|
|
package io.company.localhost.utils;
|
|
|
|
import org.springframework.beans.BeansException;
|
|
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
|
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
|
import org.springframework.beans.factory.support.DefaultSingletonBeanRegistry;
|
|
import org.springframework.context.ApplicationContext;
|
|
import org.springframework.context.ConfigurableApplicationContext;
|
|
|
|
import io.company.localhost.common.context.ApplicationContextProvider;
|
|
import lombok.experimental.UtilityClass;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
@Slf4j
|
|
@UtilityClass
|
|
public class ContextUtil {
|
|
|
|
/****************************************************************************************************
|
|
*
|
|
* Application Context에 등록된 Object 가져오는 method AND Application Context에 등록된 Object
|
|
* destroy & remove method
|
|
*
|
|
****************************************************************************************************/
|
|
|
|
private static ApplicationContext getContext() {
|
|
return ApplicationContextProvider.getContext();
|
|
}
|
|
|
|
/**
|
|
* get bean from context
|
|
* @param Class - requiredType
|
|
* @return T - requiredType 객체
|
|
*/
|
|
public static <T> T getBean(Class<T> requiredType) {
|
|
if (getContext() == null) {
|
|
return null;
|
|
}
|
|
|
|
try {
|
|
return getContext().getBean(requiredType);
|
|
} catch (BeansException e) {
|
|
log.error("BeanUtil[getBean] Exception [{}]{}", e.getMessage(), e.toString());
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* get bean from context
|
|
* @param String - 등록 이름
|
|
* @param Class - requiredType
|
|
* @return T - requiredType 객체
|
|
*/
|
|
public static <T> T getBean(String beanName, Class<T> requiredType) {
|
|
if (getContext() == null) {
|
|
return null;
|
|
}
|
|
|
|
try {
|
|
return getContext().getBean(beanName, requiredType);
|
|
} catch (BeansException e) {
|
|
log.error("BeanUtil[getBean] Exception [{}]{}", e.getMessage(), e.toString());
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* get bean from context
|
|
* @param String - 등록 이름s
|
|
* @return Object - 해당 이름으로 등록된 Object
|
|
*/
|
|
public static Object getBean(String beanName) {
|
|
if (getContext() == null) {
|
|
return null;
|
|
}
|
|
|
|
try {
|
|
return getContext().getBean(beanName);
|
|
} catch (BeansException e) {
|
|
log.error("BeanUtil[getBean] Exception [{}]{}", e.getMessage(), e.toString());
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* destroy & remove bean from context
|
|
* @param String - bean name
|
|
*/
|
|
public static void destroySingleton(String beanName) {
|
|
ConfigurableListableBeanFactory factory = ((ConfigurableApplicationContext) getContext()).getBeanFactory();
|
|
|
|
DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) factory;
|
|
|
|
boolean isExistBean = beanFactory.containsSingleton(beanName);
|
|
log.debug("isExistBean {}", isExistBean);
|
|
|
|
beanFactory.destroySingleton(beanName);
|
|
beanFactory.removeBeanDefinition(beanName);
|
|
|
|
isExistBean = beanFactory.containsSingleton(beanName);
|
|
log.debug("isExistBean {}", isExistBean);
|
|
}
|
|
|
|
@Deprecated
|
|
public static void destroySingleton2(String beanName) {
|
|
DefaultSingletonBeanRegistry registry = (DefaultSingletonBeanRegistry) getContext()
|
|
.getAutowireCapableBeanFactory();
|
|
|
|
boolean isExistBean = registry.containsSingleton(beanName);
|
|
log.debug("isExistBean {}", isExistBean);
|
|
|
|
registry.destroySingleton(beanName);
|
|
|
|
isExistBean = registry.containsSingleton(beanName);
|
|
log.debug("isExistBean {}", isExistBean);
|
|
|
|
//
|
|
ConfigurableListableBeanFactory factory = ((ConfigurableApplicationContext) getContext()).getBeanFactory();
|
|
DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) factory;
|
|
beanFactory.removeBeanDefinition(beanName);
|
|
}
|
|
|
|
}
|