Spring 获取 Context

一、Spring中获取ApplicationContext

1. Spring范围内

在Spring范围内,可使用@Autowired直接注入

1
2
@Autowired
ApplicationContext applicationContext;

2.Spring范围外

2.1 ApplicationContextAware

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Component
public class ApplicationContextHolder implements ApplicationContextAware {

private static ApplicationContext applicationContext;

@Override
public void setApplicationContext(ApplicationContext context) {
ApplicationContextHolder.applicationContext = context;
}

public static ApplicationContext getApplicationContext(){
return applicationContext;
}
}

2.2 通过Request获取

1
2
3
4
5
6
7
8
9
/** web.xml中 DispatcherServlet 的 servlet-name  **/
private static final String SPRING_MVC_SERVLET_NAME = "appServlet";

/** org.springframework.web.servlet.FrameworkServlet.CONTEXT.appServlet **/
private static final String APPLICATION_CONTEXT_KEY = FrameworkServlet.SERVLET_CONTEXT_PREFIX +SPRING_MVC_SERVLET_NAME;


ApplicationContext applicationContext = (ApplicationContext) httpServletRequest.getSession().getServletContext().getAttribute(APPLICATION_CONTEXT_KEY);

2.3 RequestContextUtils

1
ApplicationContext applicationContext = (ApplicationContext) RequestContextUtils.findWebApplicationContext(httpServletRequest);

2.4 ContextLoader

1
ApplicationContext applicationContext = (ApplicationContext) ContextLoader.getCurrentWebApplicationContext();

二、Spring 中获取Request/Response

1.RequestContextHolder

1
2
3
4
HttpServletRequest httpServletRequest = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

HttpServletResponse httpServletResponse = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();