Created
June 13, 2012 16:29
-
-
Save dwelch2344/2925101 to your computer and use it in GitHub Desktop.
Get Spring Application Context anywhere
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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