[SPR-8240] Restructuring the "Context management and caching" section.

This commit is contained in:
Sam Brannen 2011-09-29 13:51:34 +00:00
parent bc6927e601
commit a4953e7c22
1 changed files with 97 additions and 72 deletions

View File

@ -1026,8 +1026,10 @@ public void testProcessRepeatedly() {
<para><classname>DelegatingSmartContextLoader</classname>: the <para><classname>DelegatingSmartContextLoader</classname>: the
default loader which delegates internally to an default loader which delegates internally to an
<classname>AnnotationConfigContextLoader</classname> or a <classname>AnnotationConfigContextLoader</classname> or a
<classname>GenericXmlContextLoader</classname> depending on <classname>GenericXmlContextLoader</classname> depending
the configuration declared for the test class.</para> either on the configuration declared for the test class or on
the presence of default locations or configuration
classes.</para>
</listitem> </listitem>
<listitem> <listitem>
@ -1101,15 +1103,20 @@ public class MyTest {
merely by declaring the merely by declaring the
<interfacename>@ContextConfiguration</interfacename> annotation at the <interfacename>@ContextConfiguration</interfacename> annotation at the
class level. If your test class does not explicitly declare class level. If your test class does not explicitly declare
application context resource <literal>locations</literal>, the application context resource <literal>locations</literal> or
configured <interfacename>ContextLoader</interfacename> determines how configuration <varname>classes</varname>, the configured
and whether to load a context from a default location. For example, <interfacename>ContextLoader</interfacename> determines how to load a
<classname>GenericXmlContextLoader</classname>, which is the default context from a default location or default configuration
<interfacename>ContextLoader</interfacename>, generates a default classes.</para>
location based on the name of the test class. If your class is named
<literal>com.example.MyTest</literal>, <section id="testcontext-ctx-management-xml">
<classname>GenericXmlContextLoader</classname> loads your application <title>XML-based configuration</title>
context from
<para>For example, <classname>GenericXmlContextLoader</classname>
generates a default location based on the name of the test class. If
your class is named <literal>com.example.MyTest</literal>,
<classname>GenericXmlContextLoader</classname> loads your
application context from
<literal>"classpath:/com/example/MyTest-context.xml"</literal>.</para> <literal>"classpath:/com/example/MyTest-context.xml"</literal>.</para>
<programlisting language="java">package com.example; <programlisting language="java">package com.example;
@ -1125,50 +1132,63 @@ public class MyTest {
explicitly configure the <literal>locations</literal> attribute of explicitly configure the <literal>locations</literal> attribute of
<interfacename>@ContextConfiguration</interfacename> with an array <interfacename>@ContextConfiguration</interfacename> with an array
that contains the resource locations of XML configuration metadata that contains the resource locations of XML configuration metadata
(assuming an XML-capable <interfacename>ContextLoader</interfacename> (assuming an XML-capable
has been configured, which is the default). A plain path, for example <interfacename>ContextLoader</interfacename> has been configured,
which is the default). A plain path, for example
<literal>"context.xml"</literal>, will be treated as a classpath <literal>"context.xml"</literal>, will be treated as a classpath
resource from the same package in which the test class is defined. A resource from the same package in which the test class is defined. A
path starting with a slash is treated as a fully qualified classpath path starting with a slash is treated as a fully qualified classpath
location, for example <literal>"/org/example/config.xml"</literal>. A location, for example <literal>"/org/example/config.xml"</literal>.
path which represents a URL (i.e., a path prefixed with A path which represents a URL (i.e., a path prefixed with
<literal>classpath:</literal>, <literal>file:</literal>, <literal>classpath:</literal>, <literal>file:</literal>,
<literal>http:</literal>, etc.) will be used <emphasis>as <literal>http:</literal>, etc.) will be used <emphasis>as
is</emphasis>. Alternatively, you can implement and configure your own is</emphasis>. Alternatively, you can implement and configure your
custom <interfacename>ContextLoader</interfacename>.</para> own custom <interfacename>ContextLoader</interfacename>.</para>
<programlisting language="java">@RunWith(SpringJUnit4ClassRunner.class) <programlisting language="java">@RunWith(SpringJUnit4ClassRunner.class)
<lineannotation>// ApplicationContext will be loaded from <literal>"/applicationContext.xml"</literal> and <literal>"/applicationContext-test.xml"</literal></lineannotation> <lineannotation>// ApplicationContext will be loaded from "/applicationContext.xml" and
<lineannotation>// in the root of the classpath</lineannotation> // "/applicationContext-test.xml" in the root of the classpath</lineannotation>
<emphasis role="bold">@ContextConfiguration({"/applicationContext.xml", "/applicationContext-test.xml"})</emphasis> <emphasis role="bold">@ContextConfiguration(locations={"/applicationContext.xml", "/applicationContext-test.xml"})</emphasis>
public class MyTest { public class MyTest {
<lineannotation>// class body...</lineannotation> <lineannotation>// class body...</lineannotation>
}</programlisting> }</programlisting>
<para><interfacename>@ContextConfiguration</interfacename> supports an <para><interfacename>@ContextConfiguration</interfacename> supports
alias for the <literal>locations</literal> attribute through the an alias for the <literal>locations</literal> attribute through the
standard <literal>value</literal> attribute. Thus, if you do not need standard <literal>value</literal> attribute. Thus, if you do not
to configure a custom <interfacename>ContextLoader</interfacename>, need to configure a custom
you can omit the declaration of the <literal>locations</literal> <interfacename>ContextLoader</interfacename>, you can omit the
attribute name and declare the resource locations by using the declaration of the <literal>locations</literal> attribute name and
shorthand format demonstrated in the following example.</para> declare the resource locations by using the shorthand format
demonstrated in the following example.</para>
<programlisting language="java">@RunWith(SpringJUnit4ClassRunner.class)
<emphasis role="bold">@ContextConfiguration({"/applicationContext.xml", "/applicationContext-test.xml"})</emphasis>
public class MyTest {
<lineannotation>// class body...</lineannotation>
}</programlisting>
</section>
<section id="testcontext-ctx-management-inheritance">
<title>Configuration inheritance</title>
<para><interfacename>@ContextConfiguration</interfacename> also <para><interfacename>@ContextConfiguration</interfacename> also
supports a boolean <literal>inheritLocations</literal> attribute that supports a boolean <literal>inheritLocations</literal> attribute
denotes whether resource locations from superclasses should be that denotes whether resource locations from superclasses should be
<emphasis>inherited</emphasis>. The default value is <emphasis>inherited</emphasis>. The default value is
<literal>true</literal>, which means that an annotated class inherits <literal>true</literal>, which means that an annotated class
the resource locations defined by an annotated superclass. inherits the resource locations defined by an annotated superclass.
Specifically, the resource locations for an annotated class are Specifically, the resource locations for an annotated class are
appended to the list of resource locations defined by an annotated appended to the list of resource locations defined by an annotated
superclass. Thus, subclasses have the option of superclass. Thus, subclasses have the option of
<emphasis>extending</emphasis> the list of resource locations. In the <emphasis>extending</emphasis> the list of resource locations. In
following example, the the following example, the
<interfacename>ApplicationContext</interfacename> for <interfacename>ApplicationContext</interfacename> for
<classname>ExtendedTest</classname> is loaded from "/base-context.xml" <classname>ExtendedTest</classname> is loaded from
<emphasis role="bold">and</emphasis> "/extended-context.xml", in that "/base-context.xml" <emphasis role="bold">and</emphasis>
order. Beans defined in "/extended-context.xml" may therefore override "/extended-context.xml", in that order. Beans defined in
those defined in "/base-context.xml".</para> "/extended-context.xml" may therefore override those defined in
"/base-context.xml".</para>
<programlisting language="java">@RunWith(SpringJUnit4ClassRunner.class) <programlisting language="java">@RunWith(SpringJUnit4ClassRunner.class)
<lineannotation>// ApplicationContext will be loaded from <literal>"/base-context.xml"</literal> in the root of the classpath</lineannotation> <lineannotation>// ApplicationContext will be loaded from <literal>"/base-context.xml"</literal> in the root of the classpath</lineannotation>
@ -1186,21 +1206,26 @@ public class ExtendedTest extends BaseTest {
<para>If <literal>inheritLocations</literal> is set to <para>If <literal>inheritLocations</literal> is set to
<literal>false</literal>, the resource locations for the annotated <literal>false</literal>, the resource locations for the annotated
class shadow and effectively replace any resource locations defined by class shadow and effectively replace any resource locations defined
a superclass.</para> by a superclass.</para>
</section>
<section id="testcontext-ctx-management-caching">
<title>Context caching</title>
<para>By default, once loaded, the configured <para>By default, once loaded, the configured
<interfacename>ApplicationContext</interfacename> is reused for each <interfacename>ApplicationContext</interfacename> is reused for each
test. Thus the setup cost is incurred only once (per test suite), and test. Thus the setup cost is incurred only once (per test suite),
subsequent test execution is much faster. In the unlikely case that a and subsequent test execution is much faster. In the unlikely case
test corrupts the application context and requires reloading — for that a test corrupts the application context and requires reloading
example, by modifying a bean definition or the state of an application — for example, by modifying a bean definition or the state of an
object — you can annotate your test class or test method with application object — you can annotate your test class or test method
<interfacename>@DirtiesContext</interfacename> (assuming with <interfacename>@DirtiesContext</interfacename> (assuming
<classname>DirtiesContextTestExecutionListener</classname> has been <classname>DirtiesContextTestExecutionListener</classname> has been
configured, which is the default). This instructs Spring to reload the configured, which is the default). This instructs Spring to reload
configuration and rebuild the application context before executing the the configuration and rebuild the application context before
next test.</para> executing the next test.</para>
</section>
</section> </section>
<section id="testcontext-fixture-di"> <section id="testcontext-fixture-di">