提交 c5c32ec2 编写于 作者: S Sam Brannen

Refer to static nested classes, not static inner classes

Various parts of the reference manual as well as the Javadoc for
AnnotationConfigContextLoaderUtils improperly refer to "static inner
classes" even though this terminology does not exist in Java. The Java
Language Specification explicitly refers to such classes as "static
nested classes." An "inner class" must be non-static by definition.
上级 e97712b9
/* /*
* Copyright 2002-2014 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -41,7 +41,7 @@ public abstract class AnnotationConfigContextLoaderUtils { ...@@ -41,7 +41,7 @@ public abstract class AnnotationConfigContextLoaderUtils {
/** /**
* Detect the default configuration classes for the supplied test class. * Detect the default configuration classes for the supplied test class.
* <p>The returned class array will contain all static inner classes of * <p>The returned class array will contain all static nested classes of
* the supplied class that meet the requirements for {@code @Configuration} * the supplied class that meet the requirements for {@code @Configuration}
* class implementations as specified in the documentation for * class implementations as specified in the documentation for
* {@link Configuration @Configuration}. * {@link Configuration @Configuration}.
...@@ -78,7 +78,7 @@ public abstract class AnnotationConfigContextLoaderUtils { ...@@ -78,7 +78,7 @@ public abstract class AnnotationConfigContextLoaderUtils {
if (configClasses.isEmpty()) { if (configClasses.isEmpty()) {
if (logger.isInfoEnabled()) { if (logger.isInfoEnabled()) {
logger.info(String.format("Could not detect default configuration classes for test class [%s]: " + logger.info(String.format("Could not detect default configuration classes for test class [%s]: " +
"%s does not declare any static, non-private, non-final, inner classes " + "%s does not declare any static, non-private, non-final, nested classes " +
"annotated with @Configuration.", declaringClass.getName(), declaringClass.getSimpleName())); "annotated with @Configuration.", declaringClass.getName(), declaringClass.getSimpleName()));
} }
} }
......
...@@ -1791,15 +1791,15 @@ You use the `Class` property in one of two ways: ...@@ -1791,15 +1791,15 @@ You use the `Class` property in one of two ways:
**** ****
.Inner class names .Inner class names
If you want to configure a bean definition for a `static` nested class, you have to use If you want to configure a bean definition for a `static` nested class, you have to use
the __binary__ name of the inner class. the __binary__ name of the nested class.
For example, if you have a class called `Foo` in the `com.example` package, and this For example, if you have a class called `Foo` in the `com.example` package, and this
`Foo` class has a `static` inner class called `Bar`, the value of the `'class'` `Foo` class has a `static` nested class called `Bar`, the value of the `'class'`
attribute on a bean definition would be... attribute on a bean definition would be...
`com.example.Foo$Bar` `com.example.Foo$Bar`
Notice the use of the `$` character in the name to separate the inner class name from Notice the use of the `$` character in the name to separate the nested class name from
the outer class name. the outer class name.
**** ****
...@@ -20060,20 +20060,20 @@ the discussion of __`@Bean` Lite Mode__. ...@@ -20060,20 +20060,20 @@ the discussion of __`@Bean` Lite Mode__.
If you omit the `classes` attribute from the `@ContextConfiguration` annotation, the If you omit the `classes` attribute from the `@ContextConfiguration` annotation, the
TestContext framework will attempt to detect the presence of default configuration TestContext framework will attempt to detect the presence of default configuration
classes. Specifically, `AnnotationConfigContextLoader` and classes. Specifically, `AnnotationConfigContextLoader` and
`AnnotationConfigWebContextLoader` will detect all static inner classes of the test class `AnnotationConfigWebContextLoader` will detect all `static` nested classes of the test class
that meet the requirements for configuration class implementations as specified in the that meet the requirements for configuration class implementations as specified in the
`@Configuration` javadocs. In the following example, the `OrderServiceTest` class `@Configuration` javadocs. In the following example, the `OrderServiceTest` class
declares a static inner configuration class named `Config` that will be automatically declares a `static` nested configuration class named `Config` that will be automatically
used to load the `ApplicationContext` for the test class. Note that the name of the used to load the `ApplicationContext` for the test class. Note that the name of the
configuration class is arbitrary. In addition, a test class can contain more than one configuration class is arbitrary. In addition, a test class can contain more than one
static inner configuration class if desired. `static` nested configuration class if desired.
[source,java,indent=0] [source,java,indent=0]
[subs="verbatim,quotes"] [subs="verbatim,quotes"]
---- ----
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
// ApplicationContext will be loaded from the // ApplicationContext will be loaded from the
// static inner Config class // static nested Config class
**@ContextConfiguration** **@ContextConfiguration**
public class OrderServiceTest { public class OrderServiceTest {
...@@ -25092,7 +25092,7 @@ Querying and populating a number of domain objects: ...@@ -25092,7 +25092,7 @@ Querying and populating a number of domain objects:
If the last two snippets of code actually existed in the same application, it would make If the last two snippets of code actually existed in the same application, it would make
sense to remove the duplication present in the two `RowMapper` anonymous inner classes, sense to remove the duplication present in the two `RowMapper` anonymous inner classes,
and extract them out into a single class (typically a `static` inner class) that can and extract them out into a single class (typically a `static` nested class) that can
then be referenced by DAO methods as needed. For example, it may be better to write the then be referenced by DAO methods as needed. For example, it may be better to write the
last code snippet as follows: last code snippet as follows:
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册