开发者

Advantage of WebApplication Context in spring?

开发者 https://www.devze.com 2023-04-02 14:37 出处:网络
I need to integerate struts with spring.Basicallyi want to get the bean from Spring containerin my action class.Like if i want to get any dao/helper class in my action class i should get it with Sprin

I need to integerate struts with spring.Basically i want to get the bean from Spring containerin my action class.Like if i want to get any dao/helper class in my action class i should get it with Spring. I was going through a book which says the use of WebApplicationContext to get the bean in my action class.

My question is cant we use simple XMLBeanFactory or ClassPathXmlApplicationContext(with getBeanMethod) in my action class instead of WebApplicationContext. (As WebApplicationContext 开发者_C百科has just one extra method i.e getServletContext() which we dont need in the process of getting any model Bean).

My second question in the same application is if we further want to get any other bean in my businesshelper.java should i get it with WebApplicationContext or any other sublclass of ApplicationContext(like ClassPathXmlApplicationContext)


If you are using Spring in web application, WebApplicationContext is definitely a way to go. Not only it handles startup and shutdown for you, but it also nicely integrates with servlet environment. I.e. it automatically register itself in ServletContext under org.springframework.web.context.WebApplicationContext.ROOT key.

I am mentioning this because Struts probably uses this to locate web application context of your application. Otherwise how would Struts know this?

Regarding your second question: really go for WebApplicationContext. You can then (as a last resort) use WebApplicationContextUtils.getRequiredWebApplicationContext() to fetch beans.

ClassPathXmlApplicationContext is probably only useful when doing desktop development or in sandbox/tutorial projects.

0

精彩评论

暂无评论...
验证码 换一张
取 消