Last active
September 25, 2019 10:40
-
-
Save cenkc/838240fa7e3e669cd58288f8eb6837c2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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