Skip to content

Instantly share code, notes, and snippets.

@schroedermatt
Created January 7, 2020 22:04
Show Gist options
  • Save schroedermatt/f895a75c121245dcc1ce9280a46eb913 to your computer and use it in GitHub Desktop.
Save schroedermatt/f895a75c121245dcc1ce9280a46eb913 to your computer and use it in GitHub Desktop.
Spring utility class to bridge between Spring and Non-Spring things.
@Component
public class SpringContext implements ApplicationContextAware {
private static ApplicationContext context;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
context = applicationContext;
}
public static <T> T getBean(Class<T> beanClass) {
return context.getBean(beanClass);
}
public static <T> T getBean(String beanName, Class<T> beanClass) {
return (T)context.getBean(beanName);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment