切记:单元测试时需要需要保证有spring boot上下文
@Component
public class SpringBeanUtils implements ApplicationContextAware {
private static ApplicationContext applicationContext;
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringBeanUtils.applicationContext = applicationContext;
}
public static Object getBean(String name){
return getApplicationContext().getBean(name);
}
public static <T> Object getBean(Class<T> T){
return getApplicationContext().getBean(T);
}
}