java普通类获取ApplicationContext最简单方法
解决方法:
public static ApplicationContext getRequiredWebApplicationContext() {
ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
if (attributes == null) {
throw new RuntimeException("非web请求,非法调用!");
} else {
HttpServletRequest request = attributes.getRequest();
ApplicationContext ac = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext());
return ac;
}
}