`
he_lux
  • 浏览: 103219 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

在servlet中使用Spring的容器功能

阅读更多
如果想要在自己所定义的Servlet类别中使用Spring的容器功能,可以使用 org.springframework.web.context.ContextLoaderListener,例如在web.xml中使用<listener>标签加以定义:
<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>

ContextLoaderListener预设会读取applicationContext.xml,您可以指定自己定义的文件,只要在<context-param>中指定"contextConfigLocation"参数,例如:
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/beans-config.xml,
        /WEB-INF/demo-service.xml
    </param-value>
</context-param>

接着您可以在自定义的Servlet中使用 org.springframework.web.context.support.WebApplicationContextUtils,从 ServletContext中取得org.springframework.web.context.WebApplicationContext,例如: 
WebApplicationContext ctx = 
    WebApplicationContextUtils.
        getRequiredWebApplicationContext(
                         this.getServletContext());

WebApplicationContext实体化了ApplicationContext配置文件,是Spring专为Servlet的Web应用程式设计的 ApplicationContext实作类别,在取得WebApplicationContext之后,您可以利用它来取得Bean定义档中定义的 Bean实例,例如:
Date date = (Date) ctx.getBean("dateBean");

在不支援Listener设定的容器上(例如Servlet 2.2以更早的版本),您可以使用org.springframework.web.context.ContextLoaderServlet来取代上面的ContextLoaderListener的设定,例如:
<servlet>
    <servlet-name>contextLoader</servlet-name>
    <servlet-class>
      org.springframework.web.context.ContextLoaderServlet
    </servlet-class>   
    <load-on-startup>1</load-on-startup>
</servlet>

综合以上的叙述,撰写一个简单的范例来示范完整的组态方式,假设您撰写了一个简单的Servlet程式,作用为提供简单的报时功能,如下所示:
TimeServlet.java
package onlyfun.caterpillar;
 
import java.io.*; 
import javax.servlet.*; 
import javax.servlet.http.*; 
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
 
public class TimeServlet extends HttpServlet { 
    public void doGet(HttpServletRequest req, 
                    HttpServletResponse res) 
                  throws ServletException, IOException { 
        WebApplicationContext ctx = 
                WebApplicationContextUtils.
                    getRequiredWebApplicationContext(
                        this.getServletContext());
        
        PrintWriter out = res.getWriter(); 
        out.println(ctx.getBean("dateBean")); 
    } 
}

这个Servlet中使用了WebApplicationContext来尝试取得dateBean,您可以在web.xml中定义ContextLoaderListener与Bean定义档的位置,例如:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
    → http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
  version="2.4">
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>    

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/beans-config.xml</param-value>
    </context-param>     
    
    <listener>
        <listener-class>
         org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
    
    <servlet>
        <servlet-name>time</servlet-name>
        <servlet-class>
            onlyfun.caterpillar.TimeServlet
        </servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>time</servlet-name>
        <url-pattern>/time.do</url-pattern>
    </servlet-mapping>      
</web-app>

在<context-param>的"contextConfigLocation"属性中,设定了Bean定义档的位置与名称,Bean定义档的内容如下所示:
beans-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC 
 "-//SPRING/DTD BEAN/EN" 
 "http://www.springframework.org/dtd/spring-beans.dtd"> 

<beans>   
    <bean id="dateBean" class="java.util.Date" singleton="false"/>
</beans>

您可以连接至TimeServlet,结果会显示连接上的时间。
分享到:
评论

相关推荐

    servlet与spring整合例子

    Spring 与Servlet 整合在servlet下利用Spring容器生成的对象

    Web项目中使用Spring, 使用 Spring 的器监听器 ContextLoaderListener.docx

    一、Web项目中使用Spring ...在 Web 项目中使用 Spring 框架,首先要解决在 web 层(这里指 Servlet)中获取到 Spring容器的问题。只要在 web 层获取到了 Spring 容器,便可从容器中获取到 Service 对象

    Spring整合Servlet练习

    Spring和Servlet的整合应用,在Spring的容器中添加Servlet类的应用。实现spring框架和Servlet的联合应用。

    SpringBoot之配置嵌入式Servlet容器.pdf

    SpringBoot配置嵌入式Servlet容器;注册Servlet三大组件;嵌入式Servlet容器自动配置原理以及启动原理

    体验Spring的IoC容器的优点(Eclipse中).doc

    在本实验中,我们将通过使用 Eclipse IDE 来体验 Spring 的 IoC 容器的优点。首先,我们需要创建一个接口 UserInfoInterface,该接口中定义了一个业务方法 doUserLogin,该方法用于用户登录验证。然后,我们需要创建...

    spring-boot-starter-netty:一个基于Netty实现的Spring Boot内置Servlet容器。a Spring Boot embedded servlet container project base on netty API (4.1.12.Final)

    这是一个基于netty API(4.1.12.Final)的Spring Boot嵌入式servlet容器项目。 该项目已发布到Maven中心存储库中,请参阅 。 Maven依赖 将以下依赖项添加到您的Maven项目中: &lt;!-- exludes embedded Tomcat --...

    SpringBoot 注册自己的Servlet(三种方式)(源代码)

    SpringBoot 注册自己的Servlet(三种方式)(源代码) ...目前使用Spring的,更多是在web方面,所以Spring更多的是依赖Servlet容器,哪怕是springboot擅长做独立可执行的微服务应用程序,其内部也包含一个嵌入式Jetty。

    Spring中依赖注入与容器

    Spring 框架(核心)IoC 容器 IoC:控制反转  DI:依赖注入  什么是依赖?一个组件(对象)的运行需要用到另一个组件(对象),称这种关系为依赖关系  举例:鱼依赖水,生命依赖空气、阳光、水、食物 组件依赖...

    Spring.html

    Spring IOC 控制反转:把创建对象的权利交给Spring 创建对象 1.... 2.... 3.... BeanFactory:使用这个工厂创建对象的方式都...在Servlet中使用WebApplicationContextUtils获取容器对象 5.使用容器对象去获取Service对象

    Spring中文帮助文档

    12.2.2. 在Spring容器中创建 SessionFactory 12.2.3. The HibernateTemplate 12.2.4. 不使用回调的基于Spring的DAO实现 12.2.5. 基于Hibernate3的原生API实现DAO 12.2.6. 编程式的事务划分 12.2.7. 声明式的...

    Spring技术内幕:深入解析Spring架构与设计原理[汇编].pdf

    * 依赖注入(Dependency Injection): Spring使用依赖注入来解耦合应用程序中的组件,提高系统的灵活性和可维护性。 * AOP(Aspect-Oriented Programming): Spring使用AOP来实现面向方面的编程,提高系统的可维护...

    SpringMVC 全注解实现 servlet3.0以上的容器支持.docx

    请求旅行的第一站是Spring的 DispatcherServlet ,和大多数Javaweb应用相同,Spring MVC通过一个单独的前端控制器过滤分发请求。当Web应用委托一个servlet...在Spring MVC中, DispatcherServlet 就是前端控制器。

    Spring中ApplicationContext加载机制

    这两者在功能上完全等同,只是一个是基于 Servlet2.3 版本中新引入的 Listener 接口实现,而另一个基于 Servlet 接口实现。开发中可根据目标 Web 容器的实际情况进行选择。 配置非常简单,在 web.xml 中增加相应的...

    JSP 开发之servlet中调用注入spring管理的dao

    我们用spring的依赖注入可以将dao注入到action中,然后我们就可以直接调用了dao中的方法了,可是servlet不是由spring容器管理,所以在servlet中不能注入dao类,也就不能用dao中的方法。 下面是实现方法: private ...

    java web的运行开发环境和servlet的开发部署

    3. Java Web 应用的开发和部署:包括使用 MyEclipse 工具来开发一个 web 应用,了解 Tomcat、JDK、浏览器在 JavaWeb 应用中各自的作用。 * MyEclipse 是一个集成了 Eclipse 和 Web 开发工具的开发环境,提供了一个...

    Spring5-01-Spring入门.doc

    * Spring Core:提供基础功能,包括 IOC 容器和 AOP 机制。 * Spring Context:提供应用程序上下文管理机制。 * Spring Web:提供 Web 应用开发支持,包括 Servlet、Spring MVC 等。 * Spring Data Access:提供数据...

    jersey-spring-1.18.jar

    com.sun.jersey.spi.spring.container.servlet.SpringServlet需要此包的支持,使用spring3.1.1开发web service project时,用于添加Spring...解决在web service project中使用spring IOC容器时获得对象为NULL的问题。

    spring mvc项目

    spring mvc maven项目,导入IDEA后无报错,需要在IDEA中...该项目使用servlet3.0规范,无web.xml,无spring.xml等配置文件,所有的配置均通过Java Config、注解搞定,项目中还集成了log4j2技术,以及前端html文件等。

    springweb3.0MVC注解(附实例)

    web.xml 中定义了一个名为 annomvc 的 Spring MVC 模块,按照 Spring MVC 的契约,需要在 WEB-INF/annomvc-servlet.xml 配置文件中定义 Spring MVC 模块的具体配置。annomvc-servlet.xml 的配置内容如下所示: ...

    spring-boot-starter-netty:Spring Boot的Netty容器

    我对Spring Boot还是很陌生,但是这种方法似乎适合servlet容器的自以为是的观点。 异步Servlet支持尚未完成。表现使用runTestApp Gradle任务以此处使用的相同配置启动服务器。 使用wrk使用以下参数运行测试: wrk -...

Global site tag (gtag.js) - Google Analytics