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?
Spring MVC web applications use the web. xml file as a deployment descriptor file. Also, it defines mappings between URL paths and the servlets in the web. xml file.
Basic. The task of the DispatcherServlet is to send request to the specific Spring MVC controller. ContextLoaderListener reads the Spring configuration file (with value given against contextConfigLocation in web.xml ), parses it and loads the singleton bean defined in that config file.
In Spring MVC all incoming requests go through a single servlet is called Dispatcher Servlet (front controller). The front controller is a design pattern in web application development. A single servlet receives all the request and transfers them to all other components of the application.
Create a new RequestContext for the given request, using the request attributes for Errors retrieval. This only works with InternalResourceViews, as Errors instances are part of the model and not normally exposed as request attributes. It will typically be used within JSPs or custom tags.
Spring Boot will automatically register any @Beans
of the following types with the servlet container:
For example, to register GravityWebSocketDeployer
which is a ServletContextListener
add a @Bean
method to your configuration class:
@Bean
public GravityWebSocketDeployer gravityWebSocketDeployer() {
return new GravityWebSocketDeployer();
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With