Spring Boot uses a servlet, filter, or listener method

Source: Internet
Author: User

According to the official documentation, there are two ways to use servlet, filter, or listener in your spring boot app.

One: Register a servlet, filter, or listener as a spring Bean.

Attention:

Because these three are related to containers, they are not delayed to initialize! So, in general, you can't inject other spring beans--but not that you can't get them, you can use some means to inject them before you call them!

Servlets, filter can use Servletregistrationbean, Filterregistrationbean to set initialization parameters and mapping paths.

By default, if you do not specify a filter's Dispatchertype, the forward, include, and request are matched. If async is enabled, it will also match async.

If you want to use only beans, and you don't want to install them in the container, registration.setenabled (false).

Second: Use classpath scanning, specifically using @servletcomponentscan on the @configuration class.

Attention:

Need to be used in conjunction with @webservlet, @WebFilter or @weblistener;

The default is to scan the current configuration file for the package and the child package.

Ps:

If it is a spring MVC project, and it is Java Config, it can be selected in the first way;

or set filter in the Webapplicationinitializer implementation class-There should be a way to add multiple filter, but only for dispatcherservlet!

Alternatively, you can get ServletContext in the Onstartup () method of the Webapplicationinitializer implementation class and register your own servlet, filter, or listener.

It is also necessary to add that this is a feature supported by the servlet 3.0+.

Spring Boot uses a servlet, filter, or listener method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.