com.github.dc.invoke.helper.ApplicationContextHelper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dc-invoke Show documentation
Show all versions of dc-invoke Show documentation
dc-invoke project for Spring Boot
package com.github.dc.invoke.helper;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
/**
* springboot applicationContext 操作帮助类
* @author wpyuan
*/
@Component("dcInvokeApplicationContextHelper")
public class ApplicationContextHelper implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
ApplicationContextHelper.applicationContext = applicationContext;
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
public static Object getBean(String name) throws BeansException {
try {
return applicationContext.getBean(name);
} catch (NoSuchBeanDefinitionException e) {
return null;
}
}
public static T getBean(Class c){
try {
return applicationContext.getBean(c);
} catch (NoSuchBeanDefinitionException e) {
return null;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy