JavaWeb应用中获取Spring的ApplicationContext

本文介绍如何在Spring结合Struts2及仅使用Spring+JSP的Web应用环境中,通过WebApplicationContextUtils工具类获取ApplicationContext对象,以便在JSP、Servlet或Action中访问已配置的bean。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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>
 
获取的方式和上述完全一样。
 
下面给出本例子的工程源码。


本文转自 leizhimin 51CTO博客,原文链接:http://blog.51cto.com/lavasoft/126570,如需转载请自行联系原作者
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值