专注于互联网--专注于架构

最新标签
网站地图
文章索引
Rss订阅

首页 »Java教程 » javaweb:JavaWeb应用中获取Spring的ApplicationContext »正文

javaweb:JavaWeb应用中获取Spring的ApplicationContext

来源: 发布时间:星期二, 2009年1月20日 浏览:15次 评论:0
  本文举例源代码或素材下载

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

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

  、Spring2.5+Struts2环境下

  1、配置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
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
           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->org.apache.struts2.dispatcher.FilterDispatcher</filter->
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <listener>
        <listener->org.springframework.web.context.ContextLoaderListener</listener->
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet->org.springframework.web.servlet.DispatcherServlet</servlet->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.form</url-pattern>
    </servlet-mapping>
</web-app>


  2、在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;char=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.pr(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
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
           version="2.5">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener->org.springframework.web.context.ContextLoaderListener</listener->
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet->org.springframework.web.servlet.DispatcherServlet</servlet->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.form</url-pattern>
    </servlet-mapping>
</web-app>




  获取方式和上述完全



0

相关文章

读者评论

发表评论

  • 昵称:
  • 内容: