JavaWeb應用中獲取Spring的ApplicationContext

JavaWeb應用中獲取Spring的ApplicationContext
 
ApplicationContext是Spring的容器環境,經過ApplicationContext對象能夠訪問全部配置的bean。
 
在Web開發開發中,經常須要從JSP或者Servlet或者Action中獲取ApplicationContext對象,這時候,就沒法使用new關鍵字經過查找配置文件來實例化ApplicationContext這個對象了。Spring經過WebApplicationContextUtils能夠方便實現您的需求。下面看個例子:
 
1、Spring2.5+Struts2環境下
 
一、配置web.xml,經過這個配置來獲取的。
<? xml version ="1.0" encoding ="UTF-8" ?>
< web-app xmlns ="http://java.sun.com/xml/ns/javaee"
                     xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
                     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
        [url]http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd[/url]"
                     version ="2.5" >
         < context-param >
                 < param-name >contextConfigLocation </ param-name >
                 < param-value >/WEB-INF/applicationContext.xml </ param-value >
         </ context-param >

         < filter >
                 < filter-name >struts2 </ filter-name >
                 < filter-class >org.apache.struts2.dispatcher.FilterDispatcher </ filter-class >
         </ filter >
         < filter-mapping >
                 < filter-name >struts2 </ filter-name >
                 < url-pattern >/* </ url-pattern >
         </ filter-mapping >
         < listener >
                 < listener-class >org.springframework.web.context.ContextLoaderListener </ listener-class >
         </ listener >
         < servlet >
                 < servlet-name >dispatcher </ servlet-name >
                 < servlet-class >org.springframework.web.servlet.DispatcherServlet </ servlet-class >
                 < load-on-startup >1 </ 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 ="org.springframework.context.ApplicationContext" % >
< %@ page import ="org.springframework.web.context.support.WebApplicationContextUtils" % >
< %@ page contentType ="text/html;charset=UTF-8" language ="java" % >
< html >
< 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>
</html>
 
2、Spring+JSP的環境
 
在此環境下web.xml配置會有些變化:
<? xml version ="1.0" encoding ="UTF-8" ?>
< web-app xmlns ="http://java.sun.com/xml/ns/javaee"
                     xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
                     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
        [url]http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd[/url]"
                     version ="2.5" >

         < context-param >
                 < param-name >contextConfigLocation </ param-name >
                 < param-value >/WEB-INF/applicationContext.xml </ param-value >
         </ context-param >
         < listener >
                 < listener-class >org.springframework.web.context.ContextLoaderListener </ listener-class >
         </ listener >
         < servlet >
                 < servlet-name >dispatcher </ servlet-name >
                 < servlet-class >org.springframework.web.servlet.DispatcherServlet </ servlet-class >
                 < load-on-startup >1 </ load-on-startup >
         </ servlet >
         < servlet-mapping >
                 < servlet-name >dispatcher </ servlet-name >
                 < url-pattern >*.form </ url-pattern >
         </ servlet-mapping >
</ web-app >
 
獲取的方式和上述徹底同樣。
 
下面給出本例子的工程源碼,參看附件。
相關文章
相關標籤/搜索