Skip to content

Instantly share code, notes, and snippets.

@BorzdeG
Created September 25, 2015 10:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BorzdeG/5399aa418397606c4895 to your computer and use it in GitHub Desktop.
Save BorzdeG/5399aa418397606c4895 to your computer and use it in GitHub Desktop.
get Application Context in not spring beans
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.convert.ConversionService;
import org.springframework.stereotype.Component;
@Component
public class ApplicationContextProvider implements ApplicationContextAware {
@SuppressWarnings("unused") private static final Logger LOG = LoggerFactory.getLogger(ApplicationContextProvider.class.getName());
private static ApplicationContext applicationContext = null;
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
public static ConversionService getConversionService() {
return applicationContext.getBean("conversionService", ConversionService.class);
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
LOG.trace("applicationContext: {}", applicationContext);
ApplicationContextProvider.applicationContext = applicationContext;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment