我试图将我的项目转换为Spring Boot项目(嵌入了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类,并在所有配置中编写了:
@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;
}
有人可以解释我应该如何转换听众吗?
答案 0 :(得分:8)
Spring Boot将使用servlet容器自动注册以下任何类型的@Beans
:
例如,要注册GravityWebSocketDeployer
,ServletContextListener
为您的配置类添加@Bean
方法:
@Bean
public GravityWebSocketDeployer gravityWebSocketDeployer() {
return new GravityWebSocketDeployer();
}
答案 1 :(得分:4)
对于RequestContext,请阅读this
@Bean
@ConditionalOnMissingBean(RequestContextListener.class)
public RequestContextListener requestContextListener() {
return new RequestContextListener();
}
对于其他侦听器,当您使用spring-boot时会自动注册,因为this链接意味着。
对于你自己的听众。
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类的一些信息。阅读部分
答案 2 :(得分:0)
Spring Boot也会自动注册HttpServlet的任何@Bean扩展名;
@Bean
public ServletRegistrationBean axisServletRegistrationBean() {
ServletRegistrationBean registration = new ServletRegistrationBean(new AxisServlet(), "/services/*");
registration.addUrlMappings("*.jws");
return registration;
}