如何在Java Web应用中获取Spring的ApplicationContext

开发 后端
ApplicationContext是Spring的容器环境,通过ApplicationContext对象可以访问所有配置的bean。本文中将讲述Java Web应用中获取Spring的ApplicationContext。

在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
    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-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
    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-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>

获取的方式和上述完全一样。

【编辑推荐】

  1. Spring中XML配置的12个技巧
  2. Spring MVC框架的高级配置
  3. Spring中的事务传播属性详解
责任编辑:杨鹏飞 来源: 51CTO博客
相关推荐

2016-01-25 12:25:58

UbuntuFedoraGitLab

2023-10-08 15:23:09

侧获取屏幕DPI鸿蒙

2010-06-08 12:54:16

UML技术

2023-03-01 13:54:53

Springpostion​继承

2021-08-30 09:56:59

Web安全攻击Java

2022-07-21 09:31:58

Actuator密码框架

2021-07-02 20:37:19

Python代码SRP

2009-06-02 10:02:50

eclipse jboeclipse jbojboss for e

2022-04-27 08:55:01

Spring外部化配置

2011-12-30 16:30:39

Java

2017-12-21 18:41:46

Java内存泄漏代码

2023-04-26 15:19:36

JavaScripMap数组

2009-06-17 17:20:14

BeanFactorySpring

2020-09-28 14:26:42

Shadow DOMWeb组件

2020-03-31 21:50:41

JavaScript前端技术

2022-07-15 09:01:15

React对象编程

2021-03-28 07:47:01

JavaWeb项目windows

2022-05-31 08:49:02

Flutter应用程序前端

2009-06-30 15:32:00

入侵检测Java Web

2009-07-10 17:54:15

Java中调用JythJython
点赞
收藏

51CTO技术栈公众号