package com.prodaas.datatools.batchplatform.utils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
/**
* 使用该类创建直接与数据库交互的service对象
* 用例:ApplicationContext applicationContext = SpringHelper.getApplicationContext();
* StandardInfoService service = applicationContext.getBean(StandardInfoService.class);
* ArrayList<String[]> detailValues =(ArrayList)service.getRulesTable(f.getStdSortId());
*/
@Component
public class SpringHelper implements ApplicationContextAware {
private static ApplicationContext applicationContext = null;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
if(SpringHelper.applicationContext == null){
SpringHelper.applicationContext = applicationContext;
}
}
//获取applicationContext
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
//通过name获取 Bean.
public static Object getBean(String name){
return getApplicationContext().getBean(name);
}
//通过class获取Bean.
public static <T> T getBean(Class<T> clazz){
return getApplicationContext().getBean(clazz);
}
//通过name,以及Clazz返回指定的Bean
public static <T> T getBean(String name,Class<T> clazz){
return getApplicationContext().getBean(name, clazz);
}
}
springboot普通类调用spring托管的类,使用参照注释中用例
于 2018-11-01 16:20:31 首次发布