熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

JavaWeb應用中獲取Spring的ApplicationContext

2013-11-23 20:42:12  來源: Java開源技術 

  ApplicationContext是Spring的容器環境通過ApplicationContext對象可以訪問所有配置的bean

  在Web開發開發中常常需要從JSP或者Servlet或者Action中獲取ApplicationContext對象這時候就無法使用new關鍵字通過查找配置文件來實例化ApplicationContext這個對象了Spring通過WebApplicationContextUtils可以方便實現您的需求下面看個例子

  Spring+Struts環境下

  配置webxml通過這個配置來獲取的

   <?xml version= encoding=UTF?>
<webapp xmlns=
                     xmlns:xsi=instance
                     xsi:schemaLocation=
        app__xsd
                     version=>
        <contextparam>
                <paramname>contextConfigLocation</paramname>
                <paramvalue>/WEBINF/applicationContextxml</paramvalue>
        </contextparam>

        <filter>
                <filtername>struts</filtername>
                <filterclass>orgapachestrutsdispatcherFilterDispatcher</filterclass>
        </filter>
        <filtermapping>
                <filtername>struts</filtername>
                <urlpattern>/*</urlpattern>
        </filtermapping>
        <listener>
                <listenerclass>orgsprntextContextLoaderListener</listenerclass>
        </listener>
        <servlet>
                <servletname>dispatcher</servletname>
                <servletclass>orgspringframeworkwebservletDispatcherServlet</servletclass>
                <loadonstartup></loadonstartup>
        </servlet>
        <servletmapping>
                <servletname>dispatcher</servletname>
                <urlpattern>*form</urlpattern>
        </servletmapping>
</webapp>

  在JSPServletAction中獲取ApplicationContext

   <%@ page import=lavasoftserviceTestService %>
<%@ page import=orgntextApplicationContext %>
<%@ page import=orgsprntextsupportWebApplicationContextUtils %>
<%@ page contentType=text/html;charset=UTF language=java %>
<html>
<head><title>Simple jsp page</title></head>
<body>
<%
//        ApplicationContext ctx = WebApplicationContextUtilsgetWebApplicationContext(requestgetSession()getServletContext());
        ApplicationContext ctx = WebApplicationContextUtilsgetWebApplicationContext(sessiongetServletContext());
        TestService service = (TestService) ctxgetBean(testService);
        String s = servicetest();
        outprint(s);
%>

</body>
</html>

  Spring+JSP的環境

  在此環境下webxml配置會有些變化

   <?xml version= encoding=UTF?>
<webapp xmlns=
                     xmlns:xsi=instance
                     xsi:schemaLocation=
        app__xsd
                     version=>

        <contextparam>
                <paramname>contextConfigLocation</paramname>
                <paramvalue>/WEBINF/applicationContextxml</paramvalue>
        </contextparam>
        <listener>
                <listenerclass>orgsprntextContextLoaderListener</listenerclass>
        </listener>
        <servlet>
                <servletname>dispatcher</servletname>
                <servletclass>orgspringframeworkwebservletDispatcherServlet</servletclass>
                <loadonstartup></loadonstartup>
        </servlet>
        <servletmapping>
                <servletname>dispatcher</servletname>
                <urlpattern>*form</urlpattern>
        </servletmapping>
</webapp>

  獲取的方式和上述完全一樣

  下面給出本例子的工程源碼參看附件

  下載附件

  testspringweb

  testspringweb


From:http://tw.wingwit.com/Article/program/Java/ky/201311/28929.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.