Java 让ServletContextListener感知spring
我正在将Spring插入现有的JavaEEWeb应用程序。我的web.xml中有以下行:Java 让ServletContextListener感知spring,java,spring,spring-mvc,Java,Spring,Spring Mvc,我正在将Spring插入现有的JavaEEWeb应用程序。我的web.xml中有以下行: <listener> <listener-class>com.MyContextListener</listener-class> </listener> 我应该怎么做才能使MyContextListener由Spring管理 编辑: 我的假设是:Spring应该创建所有servlet和所有web应用程序基础结构,这样在MyContextList
<listener>
<listener-class>com.MyContextListener</listener-class>
</listener>
我应该怎么做才能使MyContextListener
由Spring管理
编辑:
我的假设是:Spring应该创建所有servlet和所有web应用程序基础结构,这样在
MyContextListener
的contextInitialized
方法中发生的一切都应该由Spring来处理。我如何实现,通过实现一些接口,我想。如果我错了,请纠正我。谢谢 如果在Spring Boot中使用Java配置,请使用@Bean
注释在何处创建MyContextListener的新实例。可以使用@WebListener注释类,也可以使用@Bean注释方法
我应该怎么做才能使MyContextListener
由Spring管理
这取决于您使用的配置方式。无论如何,您应该直接告诉Spring使用您声明的类。这可以通过以下方式实现:
@WebListener
public class MyContextListener implements ServletContextListener { ... }
标记有此注释()的类必须实现其中一个接口
HttpSessionAttributeListener
HttpSessionListener
ServletContextAttributeListener
ServletContextListener (+)
ServletRequestAttributeListener
ServletRequestListener
HttpSessionIdListener
事实上确实如此
就我个人而言,我更喜欢基于元注释的方法,它使我的配置更简短
Spring应该创建所有servlet和所有web应用程序基础架构,这样在MyContextListener
的contextInitialized
方法中发生的一切都应该由Spring来处理
是的,如果您提供一些信息来帮助Spring注册/配置/创建/管理实例,Spring将为您提供这些信息
这些信息可能是元信息(一个告诉如何创建实例的模板,如BeanDefinition
s)或已完成的实例本身(通常以编程方式传递,从而导致编写大量代码)
我如何实现,通过实现一些接口,我想
您正在实现一个接口,使您的侦听器成为一个侦听器(一个描述特定方法的类,该方法将在某些时间点被调用)。Spring本身负责保证在这些时间点进行此类调用,在之前将对象放置在现有的web基础设施中。
我们有一个类似的场景,配置一个退出的Jerseyweb服务应用程序,使用Spring进行依赖注入。我们的Jersey网络应用程序扩展了ContextLoaderListener,如下所示
public class XServletContextListener extends ContextLoaderListener {
...
@Override
public void contextInitialized(ServletContextEvent arg0) {
super.contextInitialized(arg0);
....
}
@Override
public void contextDestroyed(ServletContextEvent arg0) {
super.contextDestroyed(arg0);
....
}
}
<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>
<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>
ContextLoaderListener在哪里
我们包括了jersey spring桥,其中包含了所有的spring依赖项,包括applicationContext.xml,如下所示
public class XServletContextListener extends ContextLoaderListener {
...
@Override
public void contextInitialized(ServletContextEvent arg0) {
super.contextInitialized(arg0);
....
}
@Override
public void contextDestroyed(ServletContextEvent arg0) {
super.contextDestroyed(arg0);
....
}
}
<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>
<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>
所有内容都由Spring framework加载并自动连接 您想将SpringBean注入这个侦听器吗?为什么需要Spring知道这个侦听器?你能补充更多关于你需要达到的目标的信息吗?这就是你想要的吗?。添加ApplicationListener接口并注册bean将使您能够处理spring生命周期。如果你能提供更多关于你的目标的信息,那就太好了。让我know@MaciejWalkowiak,我们能不能不把SpringBean注入到那个监听器中呢?只是用一种很不礼貌的方式。我不推荐,但如果你一定要看:你为什么选择
jersey
?OP没有提到它只是一个web层,仅此而已(如您所愿,用SpringMVC替换它,它不会影响所提出的问题)。无论如何,这是我为一个使用jersey的项目开发的解决方案,因为我发现这个解决方案与被问的问题密切相关,所以我在这里分享了它。就像我说的,提到jersey没有什么区别,那么应用程序的原始侦听器呢?你把它们转换成SpringBean了吗?如果你使用@Component
而不是@WebListener
,那么你可以像在这个类中一样使用@Autowire
。