Skip to content

Instantly share code, notes, and snippets.

@andrewmcnamara
Forked from mdread/AppContext.java
Created February 22, 2023 06:26
Show Gist options
  • Save andrewmcnamara/a11664080dcdea433114a3189bd5e7f2 to your computer and use it in GitHub Desktop.
Save andrewmcnamara/a11664080dcdea433114a3189bd5e7f2 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