Clarify role of 'aware' callback interfaces

This commit is contained in:
Juergen Hoeller 2019-02-15 17:06:03 +01:00
parent ac0e5d8ec6
commit c2bd229d4c
2 changed files with 41 additions and 43 deletions

View File

@ -3746,16 +3746,16 @@ Thus, beans can programmatically manipulate the `ApplicationContext` that create
through the `ApplicationContext` interface or by casting the reference to a known through the `ApplicationContext` interface or by casting the reference to a known
subclass of this interface (such as `ConfigurableApplicationContext`, which exposes subclass of this interface (such as `ConfigurableApplicationContext`, which exposes
additional functionality). One use would be the programmatic retrieval of other beans. additional functionality). One use would be the programmatic retrieval of other beans.
Sometimes this capability is useful. However, in general, you should avoid it, because it Sometimes this capability is useful. However, in general, you should avoid it, because
couples the code to Spring and does not follow the Inversion of Control style, where it couples the code to Spring and does not follow the Inversion of Control style,
collaborators are provided to beans as properties. Other methods of the where collaborators are provided to beans as properties. Other methods of the
`ApplicationContext` provide access to file resources, publishing application events, and `ApplicationContext` provide access to file resources, publishing application events,
accessing a `MessageSource`. These additional features are described in and accessing a `MessageSource`. These additional features are described in
<<context-introduction>>. <<context-introduction>>.
As of Spring 2.5, autowiring is another alternative to obtain a reference to the As of Spring 2.5, autowiring is another alternative to obtain a reference to the
`ApplicationContext`. The "`traditional`" `constructor` and `byType` autowiring modes (as `ApplicationContext`. The "`traditional`" `constructor` and `byType` autowiring modes
described in <<beans-factory-autowire>>) can provide a dependency of type (as described in <<beans-factory-autowire>>) can provide a dependency of type
`ApplicationContext` for a constructor argument or a setter method parameter, `ApplicationContext` for a constructor argument or a setter method parameter,
respectively. For more flexibility, including the ability to autowire fields and respectively. For more flexibility, including the ability to autowire fields and
multiple parameter methods, use the new annotation-based autowiring features. If you do, multiple parameter methods, use the new annotation-based autowiring features. If you do,
@ -3789,11 +3789,10 @@ init-method.
[[aware-list]] [[aware-list]]
=== Other `Aware` Interfaces === Other `Aware` Interfaces
Besides `ApplicationContextAware` and `BeanNameAware` (discussed Besides `ApplicationContextAware` and `BeanNameAware` (discussed <<beans-factory-aware,earlier>>),
<<beans-factory-aware,earlier>>), Spring offers a range of `Aware` interfaces that let Spring offers a wide range of `Aware` callback interfaces that let beans indicate to the container
beans indicate to the container that they require a certain infrastructure dependency. As that they require a certain infrastructure dependency. As a general rule, the name indicates the
a general rule, the name is a good indication of the dependency type. The following table dependency type. The following table summarizes the most important `Aware` interfaces:
summarizes the most important `Aware` interfaces:
[[beans-factory-nature-aware-list]] [[beans-factory-nature-aware-list]]
.Aware interfaces .Aware interfaces
@ -3822,7 +3821,7 @@ summarizes the most important `Aware` interfaces:
| `BootstrapContextAware` | `BootstrapContextAware`
| Resource adapter `BootstrapContext` the container runs in. Typically available only in | Resource adapter `BootstrapContext` the container runs in. Typically available only in
JCA aware `ApplicationContext` instances. JCA-aware `ApplicationContext` instances.
| <<integration.adoc#cci, JCA CCI>> | <<integration.adoc#cci, JCA CCI>>
| `LoadTimeWeaverAware` | `LoadTimeWeaverAware`
@ -9336,19 +9335,18 @@ specific and appropriate to the actual application context type.
You can configure a bean deployed into the application context to implement the special You can configure a bean deployed into the application context to implement the special
callback interface, `ResourceLoaderAware`, to be automatically called back at callback interface, `ResourceLoaderAware`, to be automatically called back at
initialization time with the application context itself passed in as the initialization time with the application context itself passed in as the `ResourceLoader`.
`ResourceLoader`. You can also expose properties of type `Resource`, to be used to You can also expose properties of type `Resource`, to be used to access static resources.
access static resources. They are injected into it like any other properties. You They are injected into it like any other properties. You can specify those `Resource`
can specify those `Resource` properties as simple `String` paths and rely on a special properties as simple `String` paths and rely on automatic conversion from those text
JavaBean `PropertyEditor` (which is automatically registered by the context) to convert strings to actual `Resource` objects when the bean is deployed.
those text strings to actual `Resource` objects when the bean is deployed.
The location path or paths supplied to an `ApplicationContext` constructor are actually The location path or paths supplied to an `ApplicationContext` constructor are actually
resource strings and, in simple form, are treated appropriately according to the specific context resource strings and, in simple form, are treated appropriately according to the specific
implementation. For example `ClassPathXmlApplicationContext` treats a simple location path as a context implementation. For example `ClassPathXmlApplicationContext` treats a simple
classpath location. You can also use location paths (resource strings) with special location path as a classpath location. You can also use location paths (resource strings)
prefixes to force loading of definitions from the classpath or a URL, regardless of the with special prefixes to force loading of definitions from the classpath or a URL,
actual context type. regardless of the actual context type.

View File

@ -323,9 +323,9 @@ The following table summarizes the strategy for converting `String` objects to `
[[resources-resourceloaderaware]] [[resources-resourceloaderaware]]
== The `ResourceLoaderAware` interface == The `ResourceLoaderAware` interface
The `ResourceLoaderAware` interface is a special marker interface that identifies objects The `ResourceLoaderAware` interface is a special callback interface which identifies
that expect to be provided with a `ResourceLoader` reference. The following listing shows components that expect to be provided with a `ResourceLoader` reference. The following
the definition of the `ResourceLoaderAware` interface: listing shows the definition of the `ResourceLoaderAware` interface:
==== ====
[source,java,indent=0] [source,java,indent=0]
@ -338,29 +338,29 @@ the definition of the `ResourceLoaderAware` interface:
---- ----
==== ====
When a class implements `ResourceLoaderAware` and is deployed into an application When a class implements `ResourceLoaderAware` and is deployed into an application context
context (as a Spring-managed bean), it is recognized as `ResourceLoaderAware` by the (as a Spring-managed bean), it is recognized as `ResourceLoaderAware` by the application
application context. The application context then invokes context. The application context then invokes `setResourceLoader(ResourceLoader)`,
`setResourceLoader(ResourceLoader)`, supplying itself as the argument (remember, all supplying itself as the argument (remember, all application contexts in Spring implement
application contexts in Spring implement the `ResourceLoader` interface). the `ResourceLoader` interface).
Since an `ApplicationContext` is a `ResourceLoader`, the bean could also Since an `ApplicationContext` is a `ResourceLoader`, the bean could also implement the
implement the `ApplicationContextAware` interface and use the supplied application `ApplicationContextAware` interface and use the supplied application context directly to
context directly to load resources. However, in general, it is better to use the specialized load resources. However, in general, it is better to use the specialized `ResourceLoader`
`ResourceLoader` interface if that is all you need. The code would be coupled only interface if that is all you need. The code would be coupled only to the resource loading
to the resource loading interface (which can be considered a utility interface) and not to interface (which can be considered a utility interface) and not to the whole Spring
the whole Spring `ApplicationContext` interface. `ApplicationContext` interface.
As of Spring 2.5, you can rely upon autowiring of the `ResourceLoader` as an alternative As of Spring 2.5, you can rely upon autowiring of the `ResourceLoader` as an alternative
to implementing the `ResourceLoaderAware` interface. The "`traditional`" `constructor` and to implementing the `ResourceLoaderAware` interface. The "`traditional`" `constructor` and
`byType` autowiring modes (as described in <<beans-factory-autowire>>) are now capable `byType` autowiring modes (as described in <<beans-factory-autowire>>) are now capable of
of providing a dependency of type `ResourceLoader` for either a constructor argument or a providing a dependency of type `ResourceLoader` for either a constructor argument or a
setter method parameter, respectively. For more flexibility (including the ability to setter method parameter, respectively. For more flexibility (including the ability to
autowire fields and multiple parameter methods), consider using the annotation-based autowire fields and multiple parameter methods), consider using the annotation-based
autowiring features. In that case, the `ResourceLoader` is autowired into a field, autowiring features. In that case, the `ResourceLoader` is autowired into a field,
constructor argument, or method parameter that expects the `ResourceLoader` type as constructor argument, or method parameter that expects the `ResourceLoader` type as long
long as the field, constructor, or method in question carries the `@Autowired` as the field, constructor, or method in question carries the `@Autowired` annotation.
annotation. For more information, see <<beans-autowired-annotation>>. For more information, see <<beans-autowired-annotation>>.