知识大全 JavaWeb应用中获取Spring的ApplicationContext

Posted 环境

篇首语:千淘万漉虽辛苦,吹尽狂沙始到金。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 JavaWeb应用中获取Spring的ApplicationContext相关的知识,希望对你有一定的参考价值。

JavaWeb应用中获取Spring的ApplicationContext  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!

  ApplicationContext是Spring的容器环境 通过ApplicationContext对象可以访问所有配置的bean

  在Web开发开发中 常常需要从JSP或者Servlet或者Action中获取ApplicationContext对象 这时候 就无法使用new关键字通过查找配置文件来实例化ApplicationContext这个对象了 Spring通过WebApplicationContextUtils可以方便实现您的需求 下面看个例子

  一 Spring +Struts 环境下

   配置web xml 通过这个配置来获取的

   <?xml version= encoding= UTF ?> <web app xmlns=                      xmlns:xsi= instance                      xsi:schemaLocation=          app_ _ xsd                      version= >         <context param>                 <param name>contextConfigLocation</param name>                 <param value>/WEB INF/applicationContext xml</param value>         </context param>         <filter>                 <filter name>struts </filter name>                 <filter class> apache struts dispatcher FilterDispatcher</filter class>         </filter>         <filter mapping>                 <filter name>struts </filter name>                 <url pattern>/*</url pattern>         </filter mapping>         <listener>                 <listener class> sprntext ContextLoaderListener</listener class>         </listener>         <servlet>                 <servlet name>dispatcher</servlet name>                 <servlet class> springframework web servlet DispatcherServlet</servlet class>                 <load on startup> </load on startup>         </servlet>         <servlet mapping>                 <servlet name>dispatcher</servlet name>                 <url pattern>* form</url pattern>         </servlet mapping> </web app>

   在JSP Servlet Action中获取ApplicationContext

   <%@ page import= lavasoft service TestService %> <%@ page import= ntext ApplicationContext %> <%@ page import= sprntext support WebApplicationContextUtils %> <%@ page contentType= text/;charset=UTF language= java %> <> <head><title>Simple jsp page</title></head> <body> <% //        ApplicationContext ctx = WebApplicationContextUtils getWebApplicationContext(request getSession() getServletContext());         ApplicationContext ctx = WebApplicationContextUtils getWebApplicationContext(session getServletContext());         TestService service = (TestService) ctx getBean( testService );         String s = service test();         out print(s); %> </body> </>

  二 Spring+JSP的环境

  在此环境下web xml配置会有些变化

   <?xml version= encoding= UTF ?> <web app xmlns=                      xmlns:xsi= instance                      xsi:schemaLocation=          app_ _ xsd                      version= >         <context param>                 <param name>contextConfigLocation</param name>                 <param value>/WEB INF/applicationContext xml</param value>         </context param>         <listener>                 <listener class> sprntext ContextLoaderListener</listener class>         </listener>         <servlet>                 <servlet name>dispatcher</servlet name>                 <servlet class> springframework web servlet DispatcherServlet</servlet class>                 <load on startup> </load on startup>         </servlet>         <servlet mapping>                 <servlet name>dispatcher</servlet name>                 <url pattern>* form</url pattern>         </servlet mapping> </web app>

  获取的方式和上述完全一样

  下面给出本例子的工程源码 参看附件

  下载附件

  testspringweb

cha138/Article/program/Java/ky/201311/28929

相关参考