Skip to content

Instantly share code, notes, and snippets.

@mdread
Created July 25, 2012 12:44
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mdread/3175976 to your computer and use it in GitHub Desktop.
Save mdread/3175976 to your computer and use it in GitHub Desktop.
get access to the Spring application context from a non-managed bean
import org.springframework.context.ApplicationContext;
public class AppContext {
private static ApplicationContext context;
public static void setApplicationContext(ApplicationContext applicationContext) {
context = applicationContext;
}
public static ApplicationContext getApplicationContext() {
return context;
}
}
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class ApplicationContextProvider implements ApplicationContextAware {
public void setApplicationContext(ApplicationContext ctx) throws BeansException {
AppContext.setApplicationContext(ctx);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment