博客统计信息

51cto博客之星
用户名:leizhimin
文章数:583
评论数:1875
访问量:5112594
无忧币:14589
博客积分:15350
博客等级:10
注册日期:2006-11-01

JavaWeb应用中获取Spring的ApplicationContext
2009-01-15 11:07:29
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://lavasoft.blog.51cto.com/62575/126570
JavaWeb应用中获取Spring的ApplicationContext
 
ApplicationContext是Spring的容器环境,通过ApplicationContext对象可以访问所有配置的bean。
 
在Web开发开发中,常常需要从JSP或者Servlet或者Action中获取ApplicationContext对象,这时候,就无法使用new关键字通过查找配置文件来实例化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
        [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>
 
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;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>
 
二、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>
 
获取的方式和上述完全一样。
 
下面给出本例子的工程源码,参看附件。

本文出自 “熔 岩” 博客,请务必保留此出处http://lavasoft.blog.51cto.com/62575/126570

分享至
更多
一键收藏,随时查看,分享好友!
0人
了这篇文章

附件下载:
  testspringweb2
  testspringweb
类别:J2EE技术圈()┆阅读()┆评论() ┆ 推送到技术圈返回首页

文章评论

 
2009-01-15 11:53:47
熔岩出品,必属精品~~~

2009-01-15 11:53:56
熔岩出品,必属精品~~~

2009-01-15 11:54:40
晕,发了两个遍,倒~~~~

2009-01-15 16:20:48
雷哥,地位直逼暴雪了,呵呵!!
暴雪出品,必属精品

2009-02-03 11:59:34
简单明了,这就是说配置Spring的ApplicationContext只跟SeverletContext有关,与Struts2无关。在Struts2中也是通过同样的SeverletContext来取得ApplicationContext的。对吧?

2009-02-05 09:18:39
最近博客一直登陆不上,也没提示密码错误,但也不会保存密码。
---------------------

struts2+spring的web应用中,spring容器的初始化是通过监听器或者servlet来初始化的。 具体配置在web.xml中。

不管怎么初始化,ApplicationContext的初始化底层方式是一样的。

 

发表评论            

【技术门诊】专家解析:软考重点难点及应试技巧
昵  称:
登录  快速注册
验证码:

请点击后输入验证码博客过2级,无需填写验证码

内  容: