@Component
public class SpringBeanFactory implements ApplicationContextAware {
private static ApplicationContext applicationContext; // Spring应用上下文环境
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringBeanFactory.applicationContext = applicationContext;
}
private static ApplicationContext getApplicationContext() {
return applicationContext;
}
public static <T> T getBean(Class<T> clazz) throws BeansException {
return (T) getApplicationContext().getBean(clazz);
}
public static <T> T getBean(String beanId, Class<T> clazz) {
return getApplicationContext().getBean(beanId, clazz);
}
}