本文介绍了使ServletContextListener弹簧感知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Spring插入到现有的Java EE Web应用程序中.我的web.xml中有以下几行:

I am plugging in Spring to existing Java EE web Application. I have following lines in my web.xml:

<listener>
    <listener-class>com.MyContextListener</listener-class>
</listener> 

然后是MyContextListener类吗?

public class MyContextListener implements ServletContextListener {

    public void contextInitialized(ServletContextEvent event) {
       //...
    }
}

我应该怎么做才能使MyContextListener由Spring管理?

What should I do to make MyContextListener be managed by Spring?

我的假设是:Spring应该创建所有servlet和所有Web应用程序基础结构,因此MyContextListenercontextInitialized方法中发生的所有事情都应该由Spring处理.通过实现我认为的某些接口,我该如何实现.如果我错了,请纠正我.谢谢!

My assumption is: Spring should create all servlets and all web app infrastructure so everything happened in contextInitialized method of MyContextListener should be somehow handled by Spring. How can I achieve, by implementing some interface I suppose. Correct me if I am wrong. Thanks!

推荐答案

好吧

我们有一个类似的方案,将退出的 Jersey 网络服务应用程序配置为使用 Spring 进行依赖项注入.我们的 Jersey 网络应用扩展了 ContextLoaderListener ,如下所示

We had a similar scenario of configuring an exiting Jersey web services app to use Spring for dependency injection. Our Jersey webapp had extended ContextLoaderListener as follow

public class XServletContextListener extends ContextLoaderListener {
    ... 
    @Override
    public void contextInitialized(ServletContextEvent arg0) {
        super.contextInitialized(arg0);
        ....
    }

    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
        super.contextDestroyed(arg0);
        ....
    }
} 

其中 ContextLoaderListener

import org.springframework.web.context.ContextLoaderListener;

我们包括了 jersey-spring 桥以及所有 spring 依赖项,包括 applicationContext.xml ,如下所示

We included the jersey-spring bridge with all spring dependencies including applicationContext.xml as follow

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <context:component-scan base-package="com.xxx.*" />
    ....
    ....
</beans>

显然,需要确保将 XServletContextListener 包含在 web.xml 中,如下所示

And obviously needed to make sure that XServletContextListener is included in the web.xml as follow

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
    <listener-class>com.xxx.**.XServletContextListener</listener-class>
</listener>

后跟 servlet 及其 init-param 值和 servlet 映射.您显然可以采用注释配置代替xml confib,在这种情况下,您将需要使用 WebListener 注释.

Followed by servlet and its init-param values and servlet mapping. You can obviously adopt annotation config in place of xml confib in which case you would need to use WebListener annotation.

我们使用了各种注释,例如

We use a variety of annotations such as

@Component for objects
@Service for services 
@Repository for DAOs
@Controller for controllers/resources 
@ContextConfiguration for tests

一切都由Spring框架加载并自动装配.

Everything is loaded and autowired by Spring framework.

这篇关于使ServletContextListener弹簧感知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 23:14