Skip to content

Instantly share code, notes, and snippets.

@dwelch2344
Created June 13, 2012 16:29
Show Gist options
  • Save dwelch2344/2925101 to your computer and use it in GitHub Desktop.
Save dwelch2344/2925101 to your computer and use it in GitHub Desktop.
Get Spring Application Context anywhere
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.support.RequestContextUtils;
public class Blah {
public static void main(String[] args) {
ServletRequestAttributes attributes = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes());
HttpServletRequest request = attributes.getRequest();
WebApplicationContext ctx = RequestContextUtils.getWebApplicationContext(request);
Object foobar = ctx.getBean(Object.class);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment