Skip to content

Commit

Permalink
Document how to register annotated classes in a GenericWebApplication…
Browse files Browse the repository at this point in the history
…Context

Closes gh-27778
  • Loading branch information
sbrannen committed Jan 27, 2022
1 parent e32f94b commit 6647023
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
Expand Up @@ -46,7 +46,17 @@
*
* <p>This is essentially the equivalent of
* {@link org.springframework.context.annotation.AnnotationConfigApplicationContext
* AnnotationConfigApplicationContext} for a web environment.
* AnnotationConfigApplicationContext} for a web environment. However, in contrast to
* {@code AnnotationConfigApplicationContext}, this class does not extend
* {@link org.springframework.context.support.GenericApplicationContext
* GenericApplicationContext} and therefore does not provide some of the convenient
* {@code registerBean(...)} methods available in a {@code GenericApplicationContext}.
* If you wish to register annotated <em>component classes</em> with a
* {@code GenericApplicationContext} in a web environment, you may use a
* {@code GenericWebApplicationContext} with an
* {@link org.springframework.context.annotation.AnnotatedBeanDefinitionReader
* AnnotatedBeanDefinitionReader}. See the Javadoc for {@link GenericWebApplicationContext}
* for details and an example.
*
* <p>To make use of this application context, the
* {@linkplain ContextLoader#CONTEXT_CLASS_PARAM "contextClass"} context-param for
Expand Down Expand Up @@ -81,8 +91,10 @@
*
* @author Chris Beams
* @author Juergen Hoeller
* @author Sam Brannen
* @since 3.0
* @see org.springframework.context.annotation.AnnotationConfigApplicationContext
* @see org.springframework.web.context.support.GenericWebApplicationContext
*/
public class AnnotationConfigWebApplicationContext extends AbstractRefreshableWebApplicationContext
implements AnnotationConfigRegistry {
Expand Down
Expand Up @@ -53,13 +53,28 @@
* {@link org.springframework.context.support.AbstractApplicationContext AbstractApplicationContext},
* this class detects a {@link ThemeSource} bean in the context, with the name "themeSource".
*
* <p>If you wish to register annotated <em>component classes</em> with a
* {@code GenericWebApplicationContext}, you can use an
* {@link org.springframework.context.annotation.AnnotatedBeanDefinitionReader
* AnnotatedBeanDefinitionReader}, as demonstrated in the following example.
* Component classes include in particular
* {@link org.springframework.context.annotation.Configuration @Configuration}
* classes but also plain {@link org.springframework.stereotype.Component @Component}
* classes as well as JSR-330 compliant classes using {@code javax.inject} annotations.
*
* <pre class="code">
* GenericWebApplicationContext context = new GenericWebApplicationContext();
* AnnotatedBeanDefinitionReader reader = new AnnotatedBeanDefinitionReader(context);
* reader.register(AppConfig.class, UserController.class, UserRepository.class);</pre>
*
* <p>If you intend to implement a {@code WebApplicationContext} that reads bean definitions
* from configuration files, consider deriving from {@link AbstractRefreshableWebApplicationContext},
* reading the bean definitions in an implementation of the {@code loadBeanDefinitions}
* method.
*
* @author Juergen Hoeller
* @author Chris Beams
* @author Sam Brannen
* @since 1.2
*/
public class GenericWebApplicationContext extends GenericApplicationContext
Expand Down
4 changes: 4 additions & 0 deletions src/docs/asciidoc/core/core-beans.adoc
Expand Up @@ -7900,6 +7900,10 @@ init-param):
</web-app>
----

NOTE: For programmatic use cases, a `GenericWebApplicationContext` can be used as an
alternative to `AnnotationConfigWebApplicationContext`. See the
{api-spring-framework}/web/context/support/GenericWebApplicationContext.html[`GenericWebApplicationContext`]
javadoc for details.


[[beans-java-bean-annotation]]
Expand Down
5 changes: 5 additions & 0 deletions src/docs/asciidoc/web/webmvc.adoc
Expand Up @@ -84,6 +84,11 @@ NOTE: In addition to using the ServletContext API directly, you can also extend
`AbstractAnnotationConfigDispatcherServletInitializer` and override specific methods
(see the example under <<mvc-servlet-context-hierarchy>>).

NOTE: For programmatic use cases, a `GenericWebApplicationContext` can be used as an
alternative to `AnnotationConfigWebApplicationContext`. See the
{api-spring-framework}/web/context/support/GenericWebApplicationContext.html[`GenericWebApplicationContext`]
javadoc for details.

The following example of `web.xml` configuration registers and initializes the `DispatcherServlet`:

[source,xml,indent=0,subs="verbatim,quotes"]
Expand Down

0 comments on commit 6647023

Please sign in to comment.