Add support for @WebFilter, @WebListener, @WebServlet

This commit adds a new annotation, @ServletComponentScan, that can be
used to enable scanning for @WebFilter, @WebListener, and @WebServlet
annotated classes. Registration beans will be automatically created for
any classes that are found, with the configuration derived from the
annotation.
This commit is contained in:
Andy Wilkinson
2015-08-17 16:45:44 +01:00
parent 07ec9bb849
commit 003268fb4e
18 changed files with 1441 additions and 16 deletions

View File

@@ -348,15 +348,23 @@ Spring Boot. The definitive list comes from searching the source code for
[[howto-add-a-servlet-filter-or-servletcontextlistener]]
=== Add a Servlet, Filter or ServletContextListener to an application
`Servlet`, `Filter`, `ServletContextListener` and the other listeners supported by the
Servlet spec can be added to your application as `@Bean` definitions. Be very careful that
they don't cause eager initialization of too many other beans because they have to be
installed in the container very early in the application lifecycle (e.g. it's not a good
idea to have them depend on your `DataSource` or JPA configuration). You can work around
restrictions like that by initializing them lazily when first used instead of on
initialization.
[[howto-add-a-servlet-filter-or-listener]]
=== Add a Servlet, Filter or Listener to an application
There are two ways to add `Servlet`, `Filter`, `ServletContextListener` and the other
listeners supported by the Servlet spec to your application. You can either provide
Spring beans for them, or enable scanning for Servlet components.
[[howto-add-a-servlet-filter-or-listener-as-spring-bean]]
==== Add a Servlet, Filter or Listener using a Spring bean
To add a `Servlet`, `Filter`, or Servlet `*Listener` provide a `@Bean` definition for it.
This can be very useful when you want to inject configuration or dependencies. However,
you must be very careful that they don't cause eager initialization of too many other
beans because they have to be installed in the container very early in the application
lifecycle (e.g. it's not a good idea to have them depend on your `DataSource` or JPA
configuration). You can work around restrictions like that by initializing them lazily
when first used instead of on initialization.
In the case of `Filters` and `Servlets` you can also add mappings and init parameters by
adding a `FilterRegistrationBean` or `ServletRegistrationBean` instead of or as well as
@@ -365,7 +373,7 @@ the underlying component.
[[howto-disable-registration-of-a-servlet-or-filter]]
=== Disable registration of a Servlet or Filter
===== Disable registration of a Servlet or Filter
As <<howto-add-a-servlet-filter-or-servletcontextlistener,described above>> any `Servlet`
or `Filter` beans will be registered with the servlet container automatically. To disable
registration of a particular `Filter` or `Servlet` bean create a registration bean for it
@@ -382,6 +390,15 @@ and mark it as disabled. For example:
----
[[howto-add-a-servlet-filter-or-listener-using-scanning]]
==== Add Servlets, Filters, and Listeners using classpath scanning
`@WebServlet`, `@WebFilter`, and `@WebListener` annotated classes can be automatically
registered with an embedded servlet container by annotating a `@Configuration` class
with `@ServletComponentScan` and specifying the package(s) containing the components
that you want to register. By default, `@ServletComponentScan` will scan from the package
of the annotated class.
[[howto-change-the-http-port]]
=== Change the HTTP port
@@ -1215,7 +1232,7 @@ using the "logging.config" property.
[[howto-configure-logback-for-loggin]]
=== Configure Logback for logging
If you put a `logback.xml` in the root of your classpath it will be picked up from
there
there
(or `logback-spring.xml` to take advantage of the templating features provided by Boot). Spring Boot provides a default base configuration that you can include if you just
want to set levels, e.g.

View File

@@ -1585,12 +1585,18 @@ instance. By default the embedded server will listen for HTTP requests on port `
[[boot-features-embedded-container-servlets-and-filters]]
==== Servlets and Filters
[[boot-features-embedded-container-servlets-filters-listeners]]
==== Servlets, Filters, and listeners
When using an embedded servlet container you can register Servlets, Filters and all the
listeners from the Servlet spec (e.g. `HttpSessionListener`) directly as
Spring beans. This can be particularly convenient if you want to refer to a value from
your `application.properties` during configuration.
listeners from the Servlet spec (e.g. `HttpSessionListener`) either by using Spring beans
or by scanning for Servlet components.
[[boot-features-embedded-container-servlets-filters-listeners-beans]]
===== Registering Servlets, Filters, and listeners as Spring beans
Any `Servlet`, `Filter` or Servlet `*Listener` instance that is a Spring bean will be
registered with the embedded container. This can be particularly convenient if you want to
refer to a value from your `application.properties` during configuration.
By default, if the context contains only a single Servlet it will be mapped to `/`. In the
case of multiple Servlet beans the bean name will be used as a path prefix. Filters will
@@ -1603,6 +1609,16 @@ the `ServletContextInitializer` interface.
[[boot-features-embedded-container-servlets-filters-listeners-scanning]]
===== Scanning for Servlets, Filters, and listeners
When using an embedded container, automatic registration of `@WebServlet`, `@WebFilter`,
and `@WebListener` annotated classes can be enabled using `@ServletComponentScan`.
TIP: `@ServletComponentScan` will have no effect in a standalone container, where the
container's built-in discovery mechanisms will be used instead.
[[boot-features-embedded-container-application-context]]
==== The EmbeddedWebApplicationContext
Under the hood Spring Boot uses a new type of `ApplicationContext` for embedded servlet