获取Spring的ApplicationContext上下文对象

x33g5p2x  于2022-03-09 转载在 Spring  
字(2.7k)|赞(0)|评价(0)|浏览(436)

1.继承ApplicationObjectSupport类

  1. @Component
  2. public class MyController extends ApplicationObjectSupport {
  3. //....
  4. }

2. 从容器中拿出来

  1. @Component
  2. public class MyController {
  3. @Autowired
  4. private ApplicationContext applicationContext;
  5. }

3. 继承ApplicationContextAware 实现静态调用

  1. import org.springframework.context.ApplicationContext;
  2. import org.springframework.context.ApplicationContextAware;
  3. import org.springframework.web.context.support.WebApplicationContextUtils;
  4. import javax.servlet.http.HttpServletRequest;
  5. import javax.servlet.http.HttpServletResponse;
  6. @Component
  7. public class SpringContextHolder implements ApplicationContextAware {
  8. private static ApplicationContext applicationContext;
  9. /**
  10. * 实现ApplicationContextAware接口的context注入函数, 将其存入静态变量.
  11. */
  12. @Override
  13. public void setApplicationContext(ApplicationContext applicationContext) {
  14. SpringContextHolder.applicationContext = applicationContext; // NOSONAR
  15. }
  16. /**
  17. * 取得存储在静态变量中的ApplicationContext.
  18. */
  19. public static ApplicationContext getApplicationContext() {
  20. checkApplicationContext();
  21. return applicationContext;
  22. }
  23. /**
  24. * 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
  25. */
  26. @SuppressWarnings("unchecked")
  27. public static <T> T getBean(String name) {
  28. checkApplicationContext();
  29. return (T) applicationContext.getBean(name);
  30. }
  31. /**
  32. * 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
  33. */
  34. @SuppressWarnings("unchecked")
  35. public static <T> T getBean(Class<T> clazz) {
  36. checkApplicationContext();
  37. return (T) applicationContext.getBeansOfType(clazz);
  38. }
  39. /**
  40. * 清除applicationContext静态变量.
  41. */
  42. public static void cleanApplicationContext() {
  43. applicationContext = null;
  44. }
  45. private static void checkApplicationContext() {
  46. if (applicationContext == null) {
  47. throw new IllegalStateException(
  48. "applicaitonContext未注入,请在applicationContext.xml中定义SpringContextHolder");
  49. }
  50. }
  51. public static void setHttpRequestResponseHolder(HttpServletRequest request, HttpServletResponse response){
  52. responseThreadLocal.set(response);
  53. ApplicationContext ap = WebApplicationContextUtils.getWebApplicationContext(null);
  54. }
  55. public static HttpServletResponse getHttpResponse(){
  56. return responseThreadLocal.get();
  57. }
  58. public static void clean(){
  59. responseThreadLocal.remove();
  60. }
  61. private static final ThreadLocal<HttpServletResponse> responseThreadLocal = new ThreadLocal();
  62. }

启动类里直接赋值

  1. public class SpringContextUtil {
  2. private static ApplicationContext applicationContext;
  3. //获取上下文
  4. public static ApplicationContext getApplicationContext() {
  5. return applicationContext;
  6. }
  7. //设置上下文
  8. public static void setApplicationContext(ApplicationContext applicationContext) {
  9. SpringContextUtil.applicationContext = applicationContext;
  10. }
  11. }
  1. @SpringBootApplication
  2. public class Application {
  3. public static void main(String[] args) {
  4. ApplicationContext app = SpringApplication.run(Application.class, args);
  5. SpringContextUtil.setApplicationContext(app);
  6. }

点赞 -收藏-关注-便于以后复习和收到最新内容有其他问题在评论区讨论-或者私信我-收到会在第一时间回复如有侵权,请私信联系我感谢,配合,希望我的努力对你有帮助^_^

相关文章