Spring boot convert web.xml listener

匿名 (未验证) 提交于 2019-12-03 02:52:02

问题:

I'm trying to convert my project to Spring Boot project (executable jar file with Jetty embedded). All works with a standard example but I want migrate my old web.xml to Spring Boot.

I migrated Servlet and Filters but I don't understand how migrate filters as this:

<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> 

I created my @SpringBootApplication class and I wrote inside all the configuration:

@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; } 

Someone can explain me how Listeners should be converted?

回答1:

For RequestContext read this

 @Bean     @ConditionalOnMissingBean(RequestContextListener.class)     public RequestContextListener requestContextListener() {         return new RequestContextListener();     } 

For the other listener is register automatically when you use spring-boot as this link implies.

For your own listeners.

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");         }     } 

Finally there is a link in which you can find some information about listeners and SpringApplication class. Read section



回答2:

Spring Boot will automatically register any @Beans of the following types with the servlet container:

  • ServletContextAttributeListener
  • ServletRequestListener
  • ServletRequestAttributeListener
  • HttpSessionAttributeListener
  • HttpSessionListener
  • ServletContextListener

For example, to register GravityWebSocketDeployer which is a ServletContextListener add a @Bean method to your configuration class:

@Bean public GravityWebSocketDeployer gravityWebSocketDeployer() {     return new GravityWebSocketDeployer(); } 


回答3:

Also Spring Boot will automatically register any @Bean extend of HttpServlet;

   @Bean    public ServletRegistrationBean axisServletRegistrationBean() {       ServletRegistrationBean registration = new ServletRegistrationBean(new AxisServlet(), "/services/*");       registration.addUrlMappings("*.jws");       return registration;    } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!