Skip to content

Instantly share code, notes, and snippets.

@cenkc
Last active September 25, 2019 10:40
Show Gist options
  • Save cenkc/838240fa7e3e669cd58288f8eb6837c2 to your computer and use it in GitHub Desktop.
Save cenkc/838240fa7e3e669cd58288f8eb6837c2 to your computer and use it in GitHub Desktop.
package com.cenkc.util;
import org.springframework.stereotype.Service;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
/**
* Created by Cenk Canarslan on 2019-09-06
*/
@Service
public class BeanUtil {
// also check
// https://dzone.com/articles/autowiring-spring-beans-into-classes-not-managed-by-spring
private static WebApplicationContext context;
public static <T> T getBean(Class<T> beanClazz) {
if (context == null) {
context = ContextLoader.getCurrentWebApplicationContext();
}
return context.getBean(beanClazz);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment