diff --git a/spring-web/src/main/java/org/springframework/web/context/support/AnnotationConfigWebApplicationContext.java b/spring-web/src/main/java/org/springframework/web/context/support/AnnotationConfigWebApplicationContext.java index 635a2c76c2..97253608df 100644 --- a/spring-web/src/main/java/org/springframework/web/context/support/AnnotationConfigWebApplicationContext.java +++ b/spring-web/src/main/java/org/springframework/web/context/support/AnnotationConfigWebApplicationContext.java @@ -46,7 +46,17 @@ import org.springframework.web.context.ContextLoader; * *

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 component classes 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. * *

To make use of this application context, the * {@linkplain ContextLoader#CONTEXT_CLASS_PARAM "contextClass"} context-param for @@ -81,8 +91,10 @@ import org.springframework.web.context.ContextLoader; * * @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 { diff --git a/spring-web/src/main/java/org/springframework/web/context/support/GenericWebApplicationContext.java b/spring-web/src/main/java/org/springframework/web/context/support/GenericWebApplicationContext.java index eb28a675e7..fed8f1d01c 100644 --- a/spring-web/src/main/java/org/springframework/web/context/support/GenericWebApplicationContext.java +++ b/spring-web/src/main/java/org/springframework/web/context/support/GenericWebApplicationContext.java @@ -53,6 +53,20 @@ import org.springframework.web.context.ServletContextAware; * {@link org.springframework.context.support.AbstractApplicationContext AbstractApplicationContext}, * this class detects a {@link ThemeSource} bean in the context, with the name "themeSource". * + *

If you wish to register annotated component classes 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. + * + *

+ * GenericWebApplicationContext context = new GenericWebApplicationContext();
+ * AnnotatedBeanDefinitionReader reader = new AnnotatedBeanDefinitionReader(context);
+ * reader.register(AppConfig.class, UserController.class, UserRepository.class);
+ * *

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} @@ -60,6 +74,7 @@ import org.springframework.web.context.ServletContextAware; * * @author Juergen Hoeller * @author Chris Beams + * @author Sam Brannen * @since 1.2 */ public class GenericWebApplicationContext extends GenericApplicationContext diff --git a/src/docs/asciidoc/core/core-beans.adoc b/src/docs/asciidoc/core/core-beans.adoc index 7213886737..95881a0d2c 100644 --- a/src/docs/asciidoc/core/core-beans.adoc +++ b/src/docs/asciidoc/core/core-beans.adoc @@ -7900,6 +7900,10 @@ init-param): ---- +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]] diff --git a/src/docs/asciidoc/web/webmvc.adoc b/src/docs/asciidoc/web/webmvc.adoc index 33b49bef56..7bca08dd9d 100644 --- a/src/docs/asciidoc/web/webmvc.adoc +++ b/src/docs/asciidoc/web/webmvc.adoc @@ -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 <>). +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"]