Spring 引导转换 web.xml 侦听器
Spring boot convert web.xml listener
我正在尝试将我的项目转换为 Spring 引导项目(嵌入 Jetty 的可执行 jar 文件)。
所有都适用于标准示例,但我想将我的旧 web.xml 迁移到 Spring Boot.
我迁移了 Servlet 和过滤器,但我不明白如何迁移过滤器:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<listener>
<listener-class>org.granite.config.GraniteConfigListener</listener-class>
</listener>
<listener>
<listener-class>org.granite.gravity.websocket.GravityWebSocketDeployer</listener-class>
</listener>
我创建了我的@SpringBootApplication class,我在里面写了所有配置:
@Bean
@Order(1)
public FilterRegistrationBean springSecurityFilterChain() {
FilterRegistrationBean filterRegBean = new FilterRegistrationBean();
DelegatingFilterProxy delegatingFilterProxy = new DelegatingFilterProxy();
filterRegBean.setFilter(delegatingFilterProxy);
List<String> urlPatterns = new ArrayList<String>();
urlPatterns.add("/*");
filterRegBean.setUrlPatterns(urlPatterns);
return filterRegBean;
}
谁能告诉我应该如何转换 Listeners?
对于 RequestContext 读取 this
@Bean
@ConditionalOnMissingBean(RequestContextListener.class)
public RequestContextListener requestContextListener() {
return new RequestContextListener();
}
当您使用 spring-boot 时,其他侦听器会自动注册,正如 this link 所暗示的那样。
为您自己的听众。
public class MyAdditionListeners extends SpringBootServletInitializer {
protected final Log logger = LogFactory.getLog(getClass());
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
WebApplicationContext rootAppContext = createRootApplicationContext(servletContext);
if (rootAppContext != null) {
servletContext.addListener(new YourListenerHere());
}
else {
this.logger.debug("No ContextLoaderListener registered, as "
+ "createRootApplicationContext() did not "
+ "return an application context");
}
}
最后有一个link,您可以在其中找到有关侦听器和SpringApplication class 的一些信息。阅读部分
Spring Boot 将自动向 servlet 容器注册以下任何 @Beans
类型:
- ServletContextAttributeListener
- ServletRequestListener
- ServletRequestAttributeListener
- HttpSessionAttributeListener
- HttpSessionListener
- ServletContextListener
例如,要注册 GravityWebSocketDeployer
这是一个 ServletContextListener
添加一个 @Bean
方法到您的配置 class:
@Bean
public GravityWebSocketDeployer gravityWebSocketDeployer() {
return new GravityWebSocketDeployer();
}
另外Spring Boot 将自动注册 HttpServlet 的任何 @Bean 扩展;
@Bean
public ServletRegistrationBean axisServletRegistrationBean() {
ServletRegistrationBean registration = new ServletRegistrationBean(new AxisServlet(), "/services/*");
registration.addUrlMappings("*.jws");
return registration;
}
我正在尝试将我的项目转换为 Spring 引导项目(嵌入 Jetty 的可执行 jar 文件)。 所有都适用于标准示例,但我想将我的旧 web.xml 迁移到 Spring Boot.
我迁移了 Servlet 和过滤器,但我不明白如何迁移过滤器:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<listener>
<listener-class>org.granite.config.GraniteConfigListener</listener-class>
</listener>
<listener>
<listener-class>org.granite.gravity.websocket.GravityWebSocketDeployer</listener-class>
</listener>
我创建了我的@SpringBootApplication class,我在里面写了所有配置:
@Bean
@Order(1)
public FilterRegistrationBean springSecurityFilterChain() {
FilterRegistrationBean filterRegBean = new FilterRegistrationBean();
DelegatingFilterProxy delegatingFilterProxy = new DelegatingFilterProxy();
filterRegBean.setFilter(delegatingFilterProxy);
List<String> urlPatterns = new ArrayList<String>();
urlPatterns.add("/*");
filterRegBean.setUrlPatterns(urlPatterns);
return filterRegBean;
}
谁能告诉我应该如何转换 Listeners?
对于 RequestContext 读取 this
@Bean
@ConditionalOnMissingBean(RequestContextListener.class)
public RequestContextListener requestContextListener() {
return new RequestContextListener();
}
当您使用 spring-boot 时,其他侦听器会自动注册,正如 this link 所暗示的那样。
为您自己的听众。
public class MyAdditionListeners extends SpringBootServletInitializer {
protected final Log logger = LogFactory.getLog(getClass());
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
WebApplicationContext rootAppContext = createRootApplicationContext(servletContext);
if (rootAppContext != null) {
servletContext.addListener(new YourListenerHere());
}
else {
this.logger.debug("No ContextLoaderListener registered, as "
+ "createRootApplicationContext() did not "
+ "return an application context");
}
}
最后有一个link,您可以在其中找到有关侦听器和SpringApplication class 的一些信息。阅读部分
Spring Boot 将自动向 servlet 容器注册以下任何 @Beans
类型:
- ServletContextAttributeListener
- ServletRequestListener
- ServletRequestAttributeListener
- HttpSessionAttributeListener
- HttpSessionListener
- ServletContextListener
例如,要注册 GravityWebSocketDeployer
这是一个 ServletContextListener
添加一个 @Bean
方法到您的配置 class:
@Bean
public GravityWebSocketDeployer gravityWebSocketDeployer() {
return new GravityWebSocketDeployer();
}
另外Spring Boot 将自动注册 HttpServlet 的任何 @Bean 扩展;
@Bean
public ServletRegistrationBean axisServletRegistrationBean() {
ServletRegistrationBean registration = new ServletRegistrationBean(new AxisServlet(), "/services/*");
registration.addUrlMappings("*.jws");
return registration;
}