views:

283

answers:

3

I notice that there is getWebApplicationContext in org.springframework.web.servlet.mvc.AbstractController. This means that spring programmers can use getWebApplicationContext to access beans in the spring IoC container.

However, I never see people use this way to get beans in all the spring MVC tutorials. So here comes my question, in that case would a programmer want to get the WebApplicationContext?

+1  A: 

That's an odd question... are you asking why a method is in the API if none of the tutorials use it? Do you expect every API method to be in the tutorials?

The getWebApplicationContext() method is rarely used by application code, but it is used internally by Spring for some tasks.

skaffman
The reason I come up with this question is that I am working on a project that involves the behavior of Spring MVC programmers. I have to support ideally any spring MVC usage.
Winston Chen
One of Spring MVC's strengths is that there are a hundred different ways to do any given thing. You can't cover them all.
skaffman
+1  A: 

In some cases when you implement org.springframework.web.servlet.View a call to getWebApplicationContext can be useful to get access to Spring beans that can not (or should not) be passed along in the Model object.

sal
+1  A: 

You also might need it when implementing custom JSP tags to access Spring Beans e.g. But this is usually a method you try to avoid from application code.

Oliver Gierke
The ViewResolver can expose Spring beans to the JSP, no need to fetch the beanfactory yourself.
skaffman