Rud*_*koŭ 18 java spring spring-mvc
我将Spring插入到现有的Java EE Web应用程序中.我的web.xml中有以下行:
<listener>
<listener-class>com.MyContextListener</listener-class>
</listener>
Run Code Online (Sandbox Code Playgroud)
而继MyContextListener类?
public class MyContextListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent event) {
//...
}
}
Run Code Online (Sandbox Code Playgroud)
我该怎么做才能让MyContextListenerSpring管理?
编辑:
我的假设是:Spring应该创建所有servlet和所有Web应用程序基础结构,所以在contextInitialized方法中发生的一切都MyContextListener应该以某种方式由Spring处理.我怎么能通过实现一些接口来实现.如果我错了,请纠正我.谢谢!
好,
我们有一个类似的场景,配置一个现有的Jersey Web服务应用程序,使用Spring进行依赖注入.我们的Jersey webapp扩展了ContextLoaderListener,如下所示
public class XServletContextListener extends ContextLoaderListener {
...
@Override
public void contextInitialized(ServletContextEvent arg0) {
super.contextInitialized(arg0);
....
}
@Override
public void contextDestroyed(ServletContextEvent arg0) {
super.contextDestroyed(arg0);
....
}
}
Run Code Online (Sandbox Code Playgroud)
其中ContextLoaderListener的是
import org.springframework.web.context.ContextLoaderListener;
Run Code Online (Sandbox Code Playgroud)
我们在jersey-spring桥中包含了所有spring依赖项,包括applicationContext.xml,如下所示
<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>
Run Code Online (Sandbox Code Playgroud)
显然需要确保XServletContextListener包含在web.xml中,如下所示
<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>
Run Code Online (Sandbox Code Playgroud)
接下来是servlet及其init-param值和servlet映射.显然,您可以采用注释配置代替xml confib,在这种情况下,您需要使用WebListener注释.
我们使用各种注释,如
@Component for objects
@Service for services
@Repository for DAOs
@Controller for controllers/resources
@ContextConfiguration for tests
Run Code Online (Sandbox Code Playgroud)
一切都由Spring框架加载和自动装配.
我该怎么做才能让
MyContextListenerSpring管理?
这取决于您使用的配置方式.无论如何,你应该直接告诉Spring使用你声明的类.这可以通过以下方式完成:
@WebListener
public class MyContextListener implements ServletContextListener { ... }
Run Code Online (Sandbox Code Playgroud)
标有此批注的类(Servlet 3.0规范,8.1.4)必须实现其中一个接口
HttpSessionAttributeListener
HttpSessionListener
ServletContextAttributeListener
ServletContextListener (+)
ServletRequestAttributeListener
ServletRequestListener
HttpSessionIdListener
Run Code Online (Sandbox Code Playgroud)
实际上它确实如此.
就个人而言,我更喜欢基于元注释的方法,这使得我的配置更简洁,更简洁.
Spring应该创建所有servlet和所有Web应用程序基础结构,所以在
contextInitialized方法中发生的一切都MyContextListener应该以某种方式由Spring处理.
是的,如果您提供一些可以帮助它注册/配置/创建/管理实例的信息,Spring将为您完成.
信息可以是元信息(告诉如何创建实例的模板,如BeanDefinitions)或已完成的实例本身(通常,它以编程方式传递,反过来导致编写大量代码).
我怎么能通过实现一些接口来实现.
您正在实现一个接口,使您的侦听器成为一个侦听器(一个描述将在某些时间点调用的特定方法的类).Spring本身负责在这些时间点保证这样的调用,之前将对象放在现有的Web基础结构中.
| 归档时间: |
|
| 查看次数: |
8099 次 |
| 最近记录: |