package com.chinaztt.mes.common.util;
|
|
import org.springframework.beans.BeansException;
|
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContextAware;
|
import org.springframework.stereotype.Component;
|
|
/**
|
* @Author: zhangxy
|
* @Date: 2021-05-24 16:29
|
*/
|
@Component
|
public class ApplicationContextHolder implements ApplicationContextAware {
|
private static ApplicationContext applicationContext;
|
|
@Override
|
public void setApplicationContext(ApplicationContext ctx) throws BeansException {
|
applicationContext = ctx;
|
}
|
|
/**
|
* Get application context from everywhere
|
*
|
* @return
|
*/
|
public static ApplicationContext getApplicationContext() {
|
return applicationContext;
|
}
|
|
/**
|
* Get bean by class
|
*
|
* @param clazz
|
* @param <T>
|
* @return
|
*/
|
public static <T> T getBean(Class<T> clazz) {
|
return applicationContext.getBean(clazz);
|
}
|
|
/**
|
* Get bean by class name
|
*
|
* @param name
|
* @param <T>
|
* @return
|
*/
|
@SuppressWarnings("unchecked")
|
public static <T> T getBean(String name) {
|
return (T) applicationContext.getBean(name);
|
}
|
}
|