Polish reference manual
This commit is contained in:
parent
8bc2bffa77
commit
c91c93c816
|
|
@ -30,7 +30,7 @@ For the currently recommended usage patterns for Hibernate see <<orm-hibernate>>
|
||||||
|
|
||||||
|
|
||||||
[[orm-hibernate-template]]
|
[[orm-hibernate-template]]
|
||||||
===== the HibernateTemplate
|
===== The HibernateTemplate
|
||||||
|
|
||||||
The basic programming model for templating looks as follows, for methods that can be
|
The basic programming model for templating looks as follows, for methods that can be
|
||||||
part of any custom data access object or business service. There are no restrictions on
|
part of any custom data access object or business service. There are no restrictions on
|
||||||
|
|
|
||||||
|
|
@ -82,6 +82,7 @@ image::images/container-magic.png[width=250]
|
||||||
|
|
||||||
[[beans-factory-metadata]]
|
[[beans-factory-metadata]]
|
||||||
=== Configuration metadata
|
=== Configuration metadata
|
||||||
|
|
||||||
As the preceding diagram shows, the Spring IoC container consumes a form of
|
As the preceding diagram shows, the Spring IoC container consumes a form of
|
||||||
__configuration metadata__; this configuration metadata represents how you as an
|
__configuration metadata__; this configuration metadata represents how you as an
|
||||||
application developer tell the Spring container to instantiate, configure, and assemble
|
application developer tell the Spring container to instantiate, configure, and assemble
|
||||||
|
|
@ -158,6 +159,7 @@ referring to collaborating objects is not shown in this example; see
|
||||||
|
|
||||||
[[beans-factory-instantiation]]
|
[[beans-factory-instantiation]]
|
||||||
=== Instantiating a container
|
=== Instantiating a container
|
||||||
|
|
||||||
Instantiating a Spring IoC container is straightforward. The location path or paths
|
Instantiating a Spring IoC container is straightforward. The location path or paths
|
||||||
supplied to an `ApplicationContext` constructor are actually resource strings that allow
|
supplied to an `ApplicationContext` constructor are actually resource strings that allow
|
||||||
the container to load configuration metadata from a variety of external resources such
|
the container to load configuration metadata from a variety of external resources such
|
||||||
|
|
@ -239,6 +241,7 @@ collaborating objects. For details of configuring an object's dependencies, see
|
||||||
|
|
||||||
[[beans-factory-xml-import]]
|
[[beans-factory-xml-import]]
|
||||||
==== Composing XML-based configuration metadata
|
==== Composing XML-based configuration metadata
|
||||||
|
|
||||||
It can be useful to have bean definitions span multiple XML files. Often each individual
|
It can be useful to have bean definitions span multiple XML files. Often each individual
|
||||||
XML configuration file represents a logical layer or module in your architecture.
|
XML configuration file represents a logical layer or module in your architecture.
|
||||||
|
|
||||||
|
|
@ -291,6 +294,7 @@ system properties at runtime.
|
||||||
|
|
||||||
[[beans-factory-client]]
|
[[beans-factory-client]]
|
||||||
=== Using the container
|
=== Using the container
|
||||||
|
|
||||||
The `ApplicationContext` is the interface for an advanced factory capable of maintaining
|
The `ApplicationContext` is the interface for an advanced factory capable of maintaining
|
||||||
a registry of different beans and their dependencies. Using the method `T getBean(String
|
a registry of different beans and their dependencies. Using the method `T getBean(String
|
||||||
name, Class<T> requiredType)` you can retrieve instances of your beans.
|
name, Class<T> requiredType)` you can retrieve instances of your beans.
|
||||||
|
|
@ -398,6 +402,7 @@ lead to concurrent access exceptions and/or inconsistent state in the bean conta
|
||||||
|
|
||||||
[[beans-beanname]]
|
[[beans-beanname]]
|
||||||
=== Naming beans
|
=== Naming beans
|
||||||
|
|
||||||
Every bean has one or more identifiers. These identifiers must be unique within the
|
Every bean has one or more identifiers. These identifiers must be unique within the
|
||||||
container that hosts the bean. A bean usually has only one identifier, but if it
|
container that hosts the bean. A bean usually has only one identifier, but if it
|
||||||
requires more than one, the extra ones can be considered aliases.
|
requires more than one, the extra ones can be considered aliases.
|
||||||
|
|
@ -435,6 +440,7 @@ by name.
|
||||||
|
|
||||||
[[beans-beanname-alias]]
|
[[beans-beanname-alias]]
|
||||||
==== Aliasing a bean outside the bean definition
|
==== Aliasing a bean outside the bean definition
|
||||||
|
|
||||||
In a bean definition itself, you can supply more than one name for the bean, by using a
|
In a bean definition itself, you can supply more than one name for the bean, by using a
|
||||||
combination of up to one name specified by the `id` attribute, and any number of other
|
combination of up to one name specified by the `id` attribute, and any number of other
|
||||||
names in the `name` attribute. These names can be equivalent aliases to the same bean,
|
names in the `name` attribute. These names can be equivalent aliases to the same bean,
|
||||||
|
|
@ -483,6 +489,7 @@ see <<beans-java-bean-annotation>> for details.
|
||||||
|
|
||||||
[[beans-factory-class]]
|
[[beans-factory-class]]
|
||||||
=== Instantiating beans
|
=== Instantiating beans
|
||||||
|
|
||||||
A bean definition essentially is a recipe for creating one or more objects. The
|
A bean definition essentially is a recipe for creating one or more objects. The
|
||||||
container looks at the recipe for a named bean when asked, and uses the configuration
|
container looks at the recipe for a named bean when asked, and uses the configuration
|
||||||
metadata encapsulated by that bean definition to create (or acquire) an actual object.
|
metadata encapsulated by that bean definition to create (or acquire) an actual object.
|
||||||
|
|
@ -521,6 +528,7 @@ the outer class name.
|
||||||
|
|
||||||
[[beans-factory-class-ctor]]
|
[[beans-factory-class-ctor]]
|
||||||
==== Instantiation with a constructor
|
==== Instantiation with a constructor
|
||||||
|
|
||||||
When you create a bean by the constructor approach, all normal classes are usable by and
|
When you create a bean by the constructor approach, all normal classes are usable by and
|
||||||
compatible with Spring. That is, the class being developed does not need to implement
|
compatible with Spring. That is, the class being developed does not need to implement
|
||||||
any specific interfaces or to be coded in a specific fashion. Simply specifying the bean
|
any specific interfaces or to be coded in a specific fashion. Simply specifying the bean
|
||||||
|
|
@ -552,6 +560,7 @@ and setting object instance properties after the object is constructed, see
|
||||||
|
|
||||||
[[beans-factory-class-static-factory-method]]
|
[[beans-factory-class-static-factory-method]]
|
||||||
==== Instantiation with a static factory method
|
==== Instantiation with a static factory method
|
||||||
|
|
||||||
When defining a bean that you create with a static factory method, you use the `class`
|
When defining a bean that you create with a static factory method, you use the `class`
|
||||||
attribute to specify the class containing the `static` factory method and an attribute
|
attribute to specify the class containing the `static` factory method and an attribute
|
||||||
named `factory-method` to specify the name of the factory method itself. You should be
|
named `factory-method` to specify the name of the factory method itself. You should be
|
||||||
|
|
@ -592,6 +601,7 @@ see <<beans-factory-properties-detailed,Dependencies and configuration in detail
|
||||||
|
|
||||||
[[beans-factory-class-instance-factory-method]]
|
[[beans-factory-class-instance-factory-method]]
|
||||||
==== Instantiation using an instance factory method
|
==== Instantiation using an instance factory method
|
||||||
|
|
||||||
Similar to instantiation through a <<beans-factory-class-static-factory-method,static
|
Similar to instantiation through a <<beans-factory-class-static-factory-method,static
|
||||||
factory method>>, instantiation with an instance factory method invokes a non-static
|
factory method>>, instantiation with an instance factory method invokes a non-static
|
||||||
method of an existing bean from the container to create a new bean. To use this
|
method of an existing bean from the container to create a new bean. To use this
|
||||||
|
|
@ -695,7 +705,8 @@ application where objects collaborate to achieve a goal.
|
||||||
|
|
||||||
|
|
||||||
[[beans-factory-collaborators]]
|
[[beans-factory-collaborators]]
|
||||||
=== Dependency injection
|
=== Dependency Injection
|
||||||
|
|
||||||
__Dependency injection__ (DI) is a process whereby objects define their dependencies,
|
__Dependency injection__ (DI) is a process whereby objects define their dependencies,
|
||||||
that is, the other objects they work with, only through constructor arguments, arguments
|
that is, the other objects they work with, only through constructor arguments, arguments
|
||||||
to a factory method, or properties that are set on the object instance after it is
|
to a factory method, or properties that are set on the object instance after it is
|
||||||
|
|
@ -717,6 +728,7 @@ dependency injection>> and <<beans-setter-injection,Setter-based dependency inje
|
||||||
|
|
||||||
[[beans-constructor-injection]]
|
[[beans-constructor-injection]]
|
||||||
==== Constructor-based dependency injection
|
==== Constructor-based dependency injection
|
||||||
|
|
||||||
__Constructor-based__ DI is accomplished by the container invoking a constructor with a
|
__Constructor-based__ DI is accomplished by the container invoking a constructor with a
|
||||||
number of arguments, each representing a dependency. Calling a `static` factory method
|
number of arguments, each representing a dependency. Calling a `static` factory method
|
||||||
with specific arguments to construct the bean is nearly equivalent, and this discussion
|
with specific arguments to construct the bean is nearly equivalent, and this discussion
|
||||||
|
|
@ -745,6 +757,7 @@ has no dependencies on container specific interfaces, base classes or annotation
|
||||||
|
|
||||||
[[beans-factory-ctor-arguments-resolution]]
|
[[beans-factory-ctor-arguments-resolution]]
|
||||||
===== Constructor argument resolution
|
===== Constructor argument resolution
|
||||||
|
|
||||||
Constructor argument resolution matching occurs using the argument's type. If no
|
Constructor argument resolution matching occurs using the argument's type. If no
|
||||||
potential ambiguity exists in the constructor arguments of a bean definition, then the
|
potential ambiguity exists in the constructor arguments of a bean definition, then the
|
||||||
order in which the constructor arguments are defined in a bean definition is the order
|
order in which the constructor arguments are defined in a bean definition is the order
|
||||||
|
|
@ -888,6 +901,7 @@ then have to look as follows:
|
||||||
|
|
||||||
[[beans-setter-injection]]
|
[[beans-setter-injection]]
|
||||||
==== Setter-based dependency injection
|
==== Setter-based dependency injection
|
||||||
|
|
||||||
__Setter-based__ DI is accomplished by the container calling setter methods on your
|
__Setter-based__ DI is accomplished by the container calling setter methods on your
|
||||||
beans after invoking a no-argument constructor or no-argument `static` factory method to
|
beans after invoking a no-argument constructor or no-argument `static` factory method to
|
||||||
instantiate your bean.
|
instantiate your bean.
|
||||||
|
|
@ -955,6 +969,7 @@ injection may be the only available form of DI.
|
||||||
|
|
||||||
[[beans-dependency-resolution]]
|
[[beans-dependency-resolution]]
|
||||||
==== Dependency resolution process
|
==== Dependency resolution process
|
||||||
|
|
||||||
The container performs bean dependency resolution as follows:
|
The container performs bean dependency resolution as follows:
|
||||||
|
|
||||||
* The `ApplicationContext` is created and initialized with configuration metadata that
|
* The `ApplicationContext` is created and initialized with configuration metadata that
|
||||||
|
|
@ -1027,6 +1042,7 @@ are invoked.
|
||||||
|
|
||||||
[[beans-some-examples]]
|
[[beans-some-examples]]
|
||||||
==== Examples of dependency injection
|
==== Examples of dependency injection
|
||||||
|
|
||||||
The following example uses XML-based configuration metadata for setter-based DI. A small
|
The following example uses XML-based configuration metadata for setter-based DI. A small
|
||||||
part of a Spring XML configuration file specifies some bean definitions:
|
part of a Spring XML configuration file specifies some bean definitions:
|
||||||
|
|
||||||
|
|
@ -1168,6 +1184,7 @@ details will not be discussed here.
|
||||||
|
|
||||||
[[beans-factory-properties-detailed]]
|
[[beans-factory-properties-detailed]]
|
||||||
=== Dependencies and configuration in detail
|
=== Dependencies and configuration in detail
|
||||||
|
|
||||||
As mentioned in the previous section, you can define bean properties and constructor
|
As mentioned in the previous section, you can define bean properties and constructor
|
||||||
arguments as references to other managed beans (collaborators), or as values defined
|
arguments as references to other managed beans (collaborators), or as values defined
|
||||||
inline. Spring's XML-based configuration metadata supports sub-element types within its
|
inline. Spring's XML-based configuration metadata supports sub-element types within its
|
||||||
|
|
@ -1300,6 +1317,7 @@ interceptor names prevents you from misspelling an interceptor id.
|
||||||
|
|
||||||
[[beans-ref-element]]
|
[[beans-ref-element]]
|
||||||
==== References to other beans (collaborators)
|
==== References to other beans (collaborators)
|
||||||
|
|
||||||
The `ref` element is the final element inside a `<constructor-arg/>` or `<property/>`
|
The `ref` element is the final element inside a `<constructor-arg/>` or `<property/>`
|
||||||
definition element. Here you set the value of the specified property of a bean to be a
|
definition element. Here you set the value of the specified property of a bean to be a
|
||||||
reference to another bean (a collaborator) managed by the container. The referenced bean
|
reference to another bean (a collaborator) managed by the container. The referenced bean
|
||||||
|
|
@ -1361,6 +1379,7 @@ your existing `ref local` references to `ref bean` when upgrading to the 4.0 sch
|
||||||
|
|
||||||
[[beans-inner-beans]]
|
[[beans-inner-beans]]
|
||||||
==== Inner beans
|
==== Inner beans
|
||||||
|
|
||||||
A `<bean/>` element inside the `<property/>` or `<constructor-arg/>` elements defines a
|
A `<bean/>` element inside the `<property/>` or `<constructor-arg/>` elements defines a
|
||||||
so-called __inner bean__.
|
so-called __inner bean__.
|
||||||
|
|
||||||
|
|
@ -1386,6 +1405,7 @@ beans into collaborating beans other than into the enclosing bean.
|
||||||
|
|
||||||
[[beans-collection-elements]]
|
[[beans-collection-elements]]
|
||||||
==== Collections
|
==== Collections
|
||||||
|
|
||||||
In the `<list/>`, `<set/>`, `<map/>`, and `<props/>` elements, you set the properties
|
In the `<list/>`, `<set/>`, `<map/>`, and `<props/>` elements, you set the properties
|
||||||
and arguments of the Java `Collection` types `List`, `Set`, `Map`, and `Properties`,
|
and arguments of the Java `Collection` types `List`, `Set`, `Map`, and `Properties`,
|
||||||
respectively.
|
respectively.
|
||||||
|
|
@ -1437,6 +1457,7 @@ following elements:__
|
||||||
|
|
||||||
[[beans-collection-elements-merging]]
|
[[beans-collection-elements-merging]]
|
||||||
===== Collection merging
|
===== Collection merging
|
||||||
|
|
||||||
The Spring container also supports the __merging__ of collections. An application
|
The Spring container also supports the __merging__ of collections. An application
|
||||||
developer can define a parent-style `<list/>`, `<map/>`, `<set/>` or `<props/>` element,
|
developer can define a parent-style `<list/>`, `<map/>`, `<set/>` or `<props/>` element,
|
||||||
and have child-style `<list/>`, `<map/>`, `<set/>` or `<props/>` elements inherit and
|
and have child-style `<list/>`, `<map/>`, `<set/>` or `<props/>` elements inherit and
|
||||||
|
|
@ -1503,6 +1524,7 @@ uses internally.
|
||||||
|
|
||||||
[[beans-collection-merge-limitations]]
|
[[beans-collection-merge-limitations]]
|
||||||
===== Limitations of collection merging
|
===== Limitations of collection merging
|
||||||
|
|
||||||
You cannot merge different collection types (such as a `Map` and a `List`), and if you
|
You cannot merge different collection types (such as a `Map` and a `List`), and if you
|
||||||
do attempt to do so an appropriate `Exception` is thrown. The `merge` attribute must be
|
do attempt to do so an appropriate `Exception` is thrown. The `merge` attribute must be
|
||||||
specified on the lower, inherited, child definition; specifying the `merge` attribute on
|
specified on the lower, inherited, child definition; specifying the `merge` attribute on
|
||||||
|
|
@ -1510,6 +1532,7 @@ a parent collection definition is redundant and will not result in the desired m
|
||||||
|
|
||||||
[[beans-collection-elements-strongly-typed]]
|
[[beans-collection-elements-strongly-typed]]
|
||||||
===== Strongly-typed collection
|
===== Strongly-typed collection
|
||||||
|
|
||||||
With the introduction of generic types in Java 5, you can use strongly typed collections.
|
With the introduction of generic types in Java 5, you can use strongly typed collections.
|
||||||
That is, it is possible to declare a `Collection` type such that it can only contain
|
That is, it is possible to declare a `Collection` type such that it can only contain
|
||||||
`String` elements (for example). If you are using Spring to dependency-inject a
|
`String` elements (for example). If you are using Spring to dependency-inject a
|
||||||
|
|
@ -1555,6 +1578,7 @@ various value elements as being of type `Float`, and the string values `9.99, 2.
|
||||||
|
|
||||||
[[beans-null-element]]
|
[[beans-null-element]]
|
||||||
==== Null and empty string values
|
==== Null and empty string values
|
||||||
|
|
||||||
Spring treats empty arguments for properties and the like as empty `Strings`. The
|
Spring treats empty arguments for properties and the like as empty `Strings`. The
|
||||||
following XML-based configuration metadata snippet sets the email property to the empty
|
following XML-based configuration metadata snippet sets the email property to the empty
|
||||||
`String` value ("").
|
`String` value ("").
|
||||||
|
|
@ -1598,6 +1622,7 @@ The above configuration is equivalent to the following Java code:
|
||||||
|
|
||||||
[[beans-p-namespace]]
|
[[beans-p-namespace]]
|
||||||
==== XML shortcut with the p-namespace
|
==== XML shortcut with the p-namespace
|
||||||
|
|
||||||
The p-namespace enables you to use the `bean` element's attributes, instead of nested
|
The p-namespace enables you to use the `bean` element's attributes, instead of nested
|
||||||
`<property/>` elements, to describe your property values and/or collaborating beans.
|
`<property/>` elements, to describe your property values and/or collaborating beans.
|
||||||
|
|
||||||
|
|
@ -1680,6 +1705,7 @@ three approaches at the same time.
|
||||||
|
|
||||||
[[beans-c-namespace]]
|
[[beans-c-namespace]]
|
||||||
==== XML shortcut with the c-namespace
|
==== XML shortcut with the c-namespace
|
||||||
|
|
||||||
Similar to the <<beans-p-namespace>>, the __c-namespace__, newly introduced in Spring
|
Similar to the <<beans-p-namespace>>, the __c-namespace__, newly introduced in Spring
|
||||||
3.1, allows usage of inlined attributes for configuring the constructor arguments rather
|
3.1, allows usage of inlined attributes for configuring the constructor arguments rather
|
||||||
then nested `constructor-arg` elements.
|
then nested `constructor-arg` elements.
|
||||||
|
|
@ -1741,6 +1767,7 @@ through-out your configuration.
|
||||||
|
|
||||||
[[beans-compound-property-names]]
|
[[beans-compound-property-names]]
|
||||||
==== Compound property names
|
==== Compound property names
|
||||||
|
|
||||||
You can use compound or nested property names when you set bean properties, as long as
|
You can use compound or nested property names when you set bean properties, as long as
|
||||||
all components of the path except the final property name are not `null`. Consider the
|
all components of the path except the final property name are not `null`. Consider the
|
||||||
following bean definition.
|
following bean definition.
|
||||||
|
|
@ -1807,6 +1834,7 @@ itself being destroyed. Thus `depends-on` can also control shutdown order.
|
||||||
|
|
||||||
[[beans-factory-lazy-init]]
|
[[beans-factory-lazy-init]]
|
||||||
=== Lazy-initialized beans
|
=== Lazy-initialized beans
|
||||||
|
|
||||||
By default, `ApplicationContext` implementations eagerly create and configure all
|
By default, `ApplicationContext` implementations eagerly create and configure all
|
||||||
<<beans-factory-scopes-singleton,singleton>> beans as part of the initialization
|
<<beans-factory-scopes-singleton,singleton>> beans as part of the initialization
|
||||||
process. Generally, this pre-instantiation is desirable, because errors in the
|
process. Generally, this pre-instantiation is desirable, because errors in the
|
||||||
|
|
@ -1850,6 +1878,7 @@ You can also control lazy-initialization at the container level by using the
|
||||||
|
|
||||||
[[beans-factory-autowire]]
|
[[beans-factory-autowire]]
|
||||||
=== Autowiring collaborators
|
=== Autowiring collaborators
|
||||||
|
|
||||||
The Spring container can __autowire__ relationships between collaborating beans. You can
|
The Spring container can __autowire__ relationships between collaborating beans. You can
|
||||||
allow Spring to resolve collaborators (other beans) automatically for your bean by
|
allow Spring to resolve collaborators (other beans) automatically for your bean by
|
||||||
inspecting the contents of the `ApplicationContext`. Autowiring has the following
|
inspecting the contents of the `ApplicationContext`. Autowiring has the following
|
||||||
|
|
@ -1913,6 +1942,7 @@ autowiring completes.
|
||||||
|
|
||||||
[[beans-autowired-exceptions]]
|
[[beans-autowired-exceptions]]
|
||||||
==== Limitations and disadvantages of autowiring
|
==== Limitations and disadvantages of autowiring
|
||||||
|
|
||||||
Autowiring works best when it is used consistently across a project. If autowiring is
|
Autowiring works best when it is used consistently across a project. If autowiring is
|
||||||
not used in general, it might be confusing to developers to use it to wire only one or
|
not used in general, it might be confusing to developers to use it to wire only one or
|
||||||
two bean definitions.
|
two bean definitions.
|
||||||
|
|
@ -1948,6 +1978,7 @@ In the latter scenario, you have several options:
|
||||||
|
|
||||||
[[beans-factory-autowire-candidate]]
|
[[beans-factory-autowire-candidate]]
|
||||||
==== Excluding a bean from autowiring
|
==== Excluding a bean from autowiring
|
||||||
|
|
||||||
On a per-bean basis, you can exclude a bean from autowiring. In Spring's XML format, set
|
On a per-bean basis, you can exclude a bean from autowiring. In Spring's XML format, set
|
||||||
the `autowire-candidate` attribute of the `<bean/>` element to `false`; the container
|
the `autowire-candidate` attribute of the `<bean/>` element to `false`; the container
|
||||||
makes that specific bean definition unavailable to the autowiring infrastructure
|
makes that specific bean definition unavailable to the autowiring infrastructure
|
||||||
|
|
@ -1968,8 +1999,10 @@ using autowiring. Rather, the bean itself is not a candidate for autowiring othe
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
[[beans-factory-method-injection]]
|
[[beans-factory-method-injection]]
|
||||||
=== Method injection
|
=== Method injection
|
||||||
|
|
||||||
In most application scenarios, most beans in the container are
|
In most application scenarios, most beans in the container are
|
||||||
<<beans-factory-scopes-singleton,singletons>>. When a singleton bean needs to
|
<<beans-factory-scopes-singleton,singletons>>. When a singleton bean needs to
|
||||||
collaborate with another singleton bean, or a non-singleton bean needs to collaborate
|
collaborate with another singleton bean, or a non-singleton bean needs to collaborate
|
||||||
|
|
@ -2033,6 +2066,7 @@ https://spring.io/blog/2004/08/06/method-injection/[this blog entry].
|
||||||
|
|
||||||
[[beans-factory-lookup-method-injection]]
|
[[beans-factory-lookup-method-injection]]
|
||||||
==== Lookup method injection
|
==== Lookup method injection
|
||||||
|
|
||||||
Lookup method injection is the ability of the container to override methods on
|
Lookup method injection is the ability of the container to override methods on
|
||||||
__container managed beans__, to return the lookup result for another named bean in the
|
__container managed beans__, to return the lookup result for another named bean in the
|
||||||
container. The lookup typically involves a prototype bean as in the scenario described
|
container. The lookup typically involves a prototype bean as in the scenario described
|
||||||
|
|
@ -2128,6 +2162,7 @@ these classes for additional information.
|
||||||
|
|
||||||
[[beans-factory-arbitrary-method-replacement]]
|
[[beans-factory-arbitrary-method-replacement]]
|
||||||
==== Arbitrary method replacement
|
==== Arbitrary method replacement
|
||||||
|
|
||||||
A less useful form of method injection than lookup method injection is the ability to
|
A less useful form of method injection than lookup method injection is the ability to
|
||||||
replace arbitrary methods in a managed bean with another method implementation. Users
|
replace arbitrary methods in a managed bean with another method implementation. Users
|
||||||
may safely skip the rest of this section until the functionality is actually needed.
|
may safely skip the rest of this section until the functionality is actually needed.
|
||||||
|
|
@ -2272,6 +2307,7 @@ For instructions on how to register this or any other custom scope, see
|
||||||
|
|
||||||
[[beans-factory-scopes-singleton]]
|
[[beans-factory-scopes-singleton]]
|
||||||
=== The singleton scope
|
=== The singleton scope
|
||||||
|
|
||||||
Only one __shared__ instance of a singleton bean is managed, and all requests for beans
|
Only one __shared__ instance of a singleton bean is managed, and all requests for beans
|
||||||
with an id or ids matching that bean definition result in that one specific bean
|
with an id or ids matching that bean definition result in that one specific bean
|
||||||
instance being returned by the Spring container.
|
instance being returned by the Spring container.
|
||||||
|
|
@ -2306,6 +2342,7 @@ in Spring__. To define a bean as a singleton in XML, you would write, for exampl
|
||||||
|
|
||||||
[[beans-factory-scopes-prototype]]
|
[[beans-factory-scopes-prototype]]
|
||||||
=== The prototype scope
|
=== The prototype scope
|
||||||
|
|
||||||
The non-singleton, prototype scope of bean deployment results in the __creation of a new
|
The non-singleton, prototype scope of bean deployment results in the __creation of a new
|
||||||
bean instance__ every time a request for that specific bean is made. That is, the bean
|
bean instance__ every time a request for that specific bean is made. That is, the bean
|
||||||
is injected into another bean or you request it through a `getBean()` method call on the
|
is injected into another bean or you request it through a `getBean()` method call on the
|
||||||
|
|
@ -2347,6 +2384,7 @@ container, see <<beans-factory-lifecycle>>.)
|
||||||
|
|
||||||
[[beans-factory-scopes-sing-prot-interaction]]
|
[[beans-factory-scopes-sing-prot-interaction]]
|
||||||
=== Singleton beans with prototype-bean dependencies
|
=== Singleton beans with prototype-bean dependencies
|
||||||
|
|
||||||
When you use singleton-scoped beans with dependencies on prototype beans, be aware that
|
When you use singleton-scoped beans with dependencies on prototype beans, be aware that
|
||||||
__dependencies are resolved at instantiation time__. Thus if you dependency-inject a
|
__dependencies are resolved at instantiation time__. Thus if you dependency-inject a
|
||||||
prototype-scoped bean into a singleton-scoped bean, a new prototype bean is instantiated
|
prototype-scoped bean into a singleton-scoped bean, a new prototype bean is instantiated
|
||||||
|
|
@ -2364,6 +2402,7 @@ runtime more than once, see <<beans-factory-method-injection>>
|
||||||
|
|
||||||
[[beans-factory-scopes-other]]
|
[[beans-factory-scopes-other]]
|
||||||
=== Request, session, and global session scopes
|
=== Request, session, and global session scopes
|
||||||
|
|
||||||
The `request`, `session`, and `global session` scopes are __only__ available if you use
|
The `request`, `session`, and `global session` scopes are __only__ available if you use
|
||||||
a web-aware Spring `ApplicationContext` implementation (such as
|
a web-aware Spring `ApplicationContext` implementation (such as
|
||||||
`XmlWebApplicationContext`). If you use these scopes with regular Spring IoC containers
|
`XmlWebApplicationContext`). If you use these scopes with regular Spring IoC containers
|
||||||
|
|
@ -2373,22 +2412,23 @@ complaining about an unknown bean scope.
|
||||||
|
|
||||||
[[beans-factory-scopes-other-web-configuration]]
|
[[beans-factory-scopes-other-web-configuration]]
|
||||||
==== Initial web configuration
|
==== Initial web configuration
|
||||||
|
|
||||||
To support the scoping of beans at the `request`, `session`, and `global session` levels
|
To support the scoping of beans at the `request`, `session`, and `global session` levels
|
||||||
(web-scoped beans), some minor initial configuration is required before you define your
|
(web-scoped beans), some minor initial configuration is required before you define your
|
||||||
beans. (This initial setup is __not__ required for the standard scopes, singleton and
|
beans. (This initial setup is __not__ required for the standard scopes, `singleton` and
|
||||||
prototype.)
|
`prototype`.)
|
||||||
|
|
||||||
How you accomplish this initial setup depends on your particular Servlet environment..
|
How you accomplish this initial setup depends on your particular Servlet environment.
|
||||||
|
|
||||||
If you access scoped beans within Spring Web MVC, in effect, within a request that is
|
If you access scoped beans within Spring Web MVC, in effect, within a request that is
|
||||||
processed by the Spring `DispatcherServlet`, or `DispatcherPortlet`, then no special
|
processed by the Spring `DispatcherServlet` or `DispatcherPortlet`, then no special
|
||||||
setup is necessary: `DispatcherServlet` and `DispatcherPortlet` already expose all
|
setup is necessary: `DispatcherServlet` and `DispatcherPortlet` already expose all
|
||||||
relevant state.
|
relevant state.
|
||||||
|
|
||||||
If you use a Servlet 2.5 web container, with requests processed outside of Spring's
|
If you use a Servlet 2.5 web container, with requests processed outside of Spring's
|
||||||
DispatcherServlet (for example, when using JSF or Struts), you need to register the
|
`DispatcherServlet` (for example, when using JSF or Struts), you need to register the
|
||||||
`org.springframework.web.context.request.RequestContextListener` `ServletRequestListener`.
|
`org.springframework.web.context.request.RequestContextListener` `ServletRequestListener`.
|
||||||
For Servlet 3.0+, this can done programmatically via the `WebApplicationInitializer`
|
For Servlet 3.0+, this can be done programmatically via the `WebApplicationInitializer`
|
||||||
interface. Alternatively, or for older containers, add the following declaration to
|
interface. Alternatively, or for older containers, add the following declaration to
|
||||||
your web application's `web.xml` file:
|
your web application's `web.xml` file:
|
||||||
|
|
||||||
|
|
@ -2406,7 +2446,7 @@ your web application's `web.xml` file:
|
||||||
</web-app>
|
</web-app>
|
||||||
----
|
----
|
||||||
|
|
||||||
Alternatively, if there are issues with your listener setup, consider the provided
|
Alternatively, if there are issues with your listener setup, consider using Spring's
|
||||||
`RequestContextFilter`. The filter mapping depends on the surrounding web
|
`RequestContextFilter`. The filter mapping depends on the surrounding web
|
||||||
application configuration, so you have to change it as appropriate.
|
application configuration, so you have to change it as appropriate.
|
||||||
|
|
||||||
|
|
@ -2427,7 +2467,7 @@ application configuration, so you have to change it as appropriate.
|
||||||
</web-app>
|
</web-app>
|
||||||
----
|
----
|
||||||
|
|
||||||
`DispatcherServlet`, `RequestContextListener` and `RequestContextFilter` all do exactly
|
`DispatcherServlet`, `RequestContextListener`, and `RequestContextFilter` all do exactly
|
||||||
the same thing, namely bind the HTTP request object to the `Thread` that is servicing
|
the same thing, namely bind the HTTP request object to the `Thread` that is servicing
|
||||||
that request. This makes beans that are request- and session-scoped available further
|
that request. This makes beans that are request- and session-scoped available further
|
||||||
down the call chain.
|
down the call chain.
|
||||||
|
|
@ -2435,6 +2475,7 @@ down the call chain.
|
||||||
|
|
||||||
[[beans-factory-scopes-request]]
|
[[beans-factory-scopes-request]]
|
||||||
==== Request scope
|
==== Request scope
|
||||||
|
|
||||||
Consider the following bean definition:
|
Consider the following bean definition:
|
||||||
|
|
||||||
[source,xml,indent=0]
|
[source,xml,indent=0]
|
||||||
|
|
@ -2454,6 +2495,7 @@ bean that is scoped to the request is discarded.
|
||||||
|
|
||||||
[[beans-factory-scopes-session]]
|
[[beans-factory-scopes-session]]
|
||||||
==== Session scope
|
==== Session scope
|
||||||
|
|
||||||
Consider the following bean definition:
|
Consider the following bean definition:
|
||||||
|
|
||||||
[source,xml,indent=0]
|
[source,xml,indent=0]
|
||||||
|
|
@ -2475,6 +2517,7 @@ HTTP `Session` is eventually discarded, the bean that is scoped to that particul
|
||||||
|
|
||||||
[[beans-factory-scopes-global-session]]
|
[[beans-factory-scopes-global-session]]
|
||||||
==== Global session scope
|
==== Global session scope
|
||||||
|
|
||||||
Consider the following bean definition:
|
Consider the following bean definition:
|
||||||
|
|
||||||
[source,xml,indent=0]
|
[source,xml,indent=0]
|
||||||
|
|
@ -2497,6 +2540,7 @@ error is raised.
|
||||||
|
|
||||||
[[beans-factory-scopes-application]]
|
[[beans-factory-scopes-application]]
|
||||||
==== Application scope
|
==== Application scope
|
||||||
|
|
||||||
Consider the following bean definition:
|
Consider the following bean definition:
|
||||||
|
|
||||||
[source,xml,indent=0]
|
[source,xml,indent=0]
|
||||||
|
|
@ -2516,6 +2560,7 @@ and it is actually exposed and therefore visible as a `ServletContext` attribute
|
||||||
|
|
||||||
[[beans-factory-scopes-other-injection]]
|
[[beans-factory-scopes-other-injection]]
|
||||||
==== Scoped beans as dependencies
|
==== Scoped beans as dependencies
|
||||||
|
|
||||||
The Spring IoC container manages not only the instantiation of your objects (beans), but
|
The Spring IoC container manages not only the instantiation of your objects (beans), but
|
||||||
also the wiring up of collaborators (or dependencies). If you want to inject (for
|
also the wiring up of collaborators (or dependencies). If you want to inject (for
|
||||||
example) an HTTP request scoped bean into another bean, you must inject an AOP proxy in
|
example) an HTTP request scoped bean into another bean, you must inject an AOP proxy in
|
||||||
|
|
@ -2616,6 +2661,7 @@ Thus you need the following, correct and complete, configuration when injecting
|
||||||
|
|
||||||
[[beans-factory-scopes-other-injection-proxies]]
|
[[beans-factory-scopes-other-injection-proxies]]
|
||||||
===== Choosing the type of proxy to create
|
===== Choosing the type of proxy to create
|
||||||
|
|
||||||
By default, when the Spring container creates a proxy for a bean that is marked up with
|
By default, when the Spring container creates a proxy for a bean that is marked up with
|
||||||
the `<aop:scoped-proxy/>` element, __a CGLIB-based class proxy is created__.
|
the `<aop:scoped-proxy/>` element, __a CGLIB-based class proxy is created__.
|
||||||
|
|
||||||
|
|
@ -2653,6 +2699,7 @@ see <<aop-proxying>>.
|
||||||
|
|
||||||
[[beans-factory-scopes-custom]]
|
[[beans-factory-scopes-custom]]
|
||||||
=== Custom scopes
|
=== Custom scopes
|
||||||
|
|
||||||
The bean scoping mechanism is extensible; You can define your own
|
The bean scoping mechanism is extensible; You can define your own
|
||||||
scopes, or even redefine existing scopes, although the latter is considered bad practice
|
scopes, or even redefine existing scopes, although the latter is considered bad practice
|
||||||
and you __cannot__ override the built-in `singleton` and `prototype` scopes.
|
and you __cannot__ override the built-in `singleton` and `prototype` scopes.
|
||||||
|
|
@ -2660,6 +2707,7 @@ and you __cannot__ override the built-in `singleton` and `prototype` scopes.
|
||||||
|
|
||||||
[[beans-factory-scopes-custom-creating]]
|
[[beans-factory-scopes-custom-creating]]
|
||||||
==== Creating a custom scope
|
==== Creating a custom scope
|
||||||
|
|
||||||
To integrate your custom scope(s) into the Spring container, you need to implement the
|
To integrate your custom scope(s) into the Spring container, you need to implement the
|
||||||
`org.springframework.beans.factory.config.Scope` interface, which is described in this
|
`org.springframework.beans.factory.config.Scope` interface, which is described in this
|
||||||
section. For an idea of how to implement your own scopes, see the `Scope`
|
section. For an idea of how to implement your own scopes, see the `Scope`
|
||||||
|
|
@ -2715,6 +2763,7 @@ identifier can be the session identifier.
|
||||||
|
|
||||||
[[beans-factory-scopes-custom-using]]
|
[[beans-factory-scopes-custom-using]]
|
||||||
==== Using a custom scope
|
==== Using a custom scope
|
||||||
|
|
||||||
After you write and test one or more custom `Scope` implementations, you need to make
|
After you write and test one or more custom `Scope` implementations, you need to make
|
||||||
the Spring container aware of your new scope(s). The following method is the central
|
the Spring container aware of your new scope(s). The following method is the central
|
||||||
method to register a new `Scope` with the Spring container:
|
method to register a new `Scope` with the Spring container:
|
||||||
|
|
@ -2812,6 +2861,7 @@ bean itself that is scoped, not the object returned from `getObject()`.
|
||||||
|
|
||||||
[[beans-factory-lifecycle]]
|
[[beans-factory-lifecycle]]
|
||||||
=== Lifecycle callbacks
|
=== Lifecycle callbacks
|
||||||
|
|
||||||
To interact with the container's management of the bean lifecycle, you can implement the
|
To interact with the container's management of the bean lifecycle, you can implement the
|
||||||
Spring `InitializingBean` and `DisposableBean` interfaces. The container calls
|
Spring `InitializingBean` and `DisposableBean` interfaces. The container calls
|
||||||
`afterPropertiesSet()` for the former and `destroy()` for the latter to allow the bean
|
`afterPropertiesSet()` for the former and `destroy()` for the latter to allow the bean
|
||||||
|
|
@ -2844,6 +2894,7 @@ The lifecycle callback interfaces are described in this section.
|
||||||
|
|
||||||
[[beans-factory-lifecycle-initializingbean]]
|
[[beans-factory-lifecycle-initializingbean]]
|
||||||
==== Initialization callbacks
|
==== Initialization callbacks
|
||||||
|
|
||||||
The `org.springframework.beans.factory.InitializingBean` interface allows a bean to
|
The `org.springframework.beans.factory.InitializingBean` interface allows a bean to
|
||||||
perform initialization work after all necessary properties on the bean have been set by
|
perform initialization work after all necessary properties on the bean have been set by
|
||||||
the container. The `InitializingBean` interface specifies a single method:
|
the container. The `InitializingBean` interface specifies a single method:
|
||||||
|
|
@ -2905,6 +2956,7 @@ but does not couple the code to Spring.
|
||||||
|
|
||||||
[[beans-factory-lifecycle-disposablebean]]
|
[[beans-factory-lifecycle-disposablebean]]
|
||||||
==== Destruction callbacks
|
==== Destruction callbacks
|
||||||
|
|
||||||
Implementing the `org.springframework.beans.factory.DisposableBean` interface allows a
|
Implementing the `org.springframework.beans.factory.DisposableBean` interface allows a
|
||||||
bean to get a callback when the container containing it is destroyed. The
|
bean to get a callback when the container containing it is destroyed. The
|
||||||
`DisposableBean` interface specifies a single method:
|
`DisposableBean` interface specifies a single method:
|
||||||
|
|
@ -2977,6 +3029,7 @@ default behavior with Java config.
|
||||||
|
|
||||||
[[beans-factory-lifecycle-default-init-destroy-methods]]
|
[[beans-factory-lifecycle-default-init-destroy-methods]]
|
||||||
==== Default initialization and destroy methods
|
==== Default initialization and destroy methods
|
||||||
|
|
||||||
When you write initialization and destroy method callbacks that do not use the
|
When you write initialization and destroy method callbacks that do not use the
|
||||||
Spring-specific `InitializingBean` and `DisposableBean` callback interfaces, you
|
Spring-specific `InitializingBean` and `DisposableBean` callback interfaces, you
|
||||||
typically write methods with names such as `init()`, `initialize()`, `dispose()`, and so
|
typically write methods with names such as `init()`, `initialize()`, `dispose()`, and so
|
||||||
|
|
@ -3056,6 +3109,7 @@ interacts directly to the raw target bean.
|
||||||
|
|
||||||
[[beans-factory-lifecycle-combined-effects]]
|
[[beans-factory-lifecycle-combined-effects]]
|
||||||
==== Combining lifecycle mechanisms
|
==== Combining lifecycle mechanisms
|
||||||
|
|
||||||
As of Spring 2.5, you have three options for controlling bean lifecycle behavior: the
|
As of Spring 2.5, you have three options for controlling bean lifecycle behavior: the
|
||||||
<<beans-factory-lifecycle-initializingbean, `InitializingBean`>> and
|
<<beans-factory-lifecycle-initializingbean, `InitializingBean`>> and
|
||||||
<<beans-factory-lifecycle-disposablebean, `DisposableBean`>> callback interfaces; custom
|
<<beans-factory-lifecycle-disposablebean, `DisposableBean`>> callback interfaces; custom
|
||||||
|
|
@ -3088,6 +3142,7 @@ Destroy methods are called in the same order:
|
||||||
|
|
||||||
[[beans-factory-lifecycle-processor]]
|
[[beans-factory-lifecycle-processor]]
|
||||||
==== Startup and shutdown callbacks
|
==== Startup and shutdown callbacks
|
||||||
|
|
||||||
The `Lifecycle` interface defines the essential methods for any object that has its own
|
The `Lifecycle` interface defines the essential methods for any object that has its own
|
||||||
lifecycle requirements (e.g. starts and stops some background process):
|
lifecycle requirements (e.g. starts and stops some background process):
|
||||||
|
|
||||||
|
|
@ -3215,6 +3270,7 @@ above.
|
||||||
|
|
||||||
[[beans-factory-shutdown]]
|
[[beans-factory-shutdown]]
|
||||||
==== Shutting down the Spring IoC container gracefully in non-web applications
|
==== Shutting down the Spring IoC container gracefully in non-web applications
|
||||||
|
|
||||||
[NOTE]
|
[NOTE]
|
||||||
====
|
====
|
||||||
This section applies only to non-web applications. Spring's web-based
|
This section applies only to non-web applications. Spring's web-based
|
||||||
|
|
@ -4292,6 +4348,7 @@ any). These types must be 'wired up' explicitly via XML or using a Spring `@Bean
|
||||||
|
|
||||||
[[beans-autowired-annotation-primary]]
|
[[beans-autowired-annotation-primary]]
|
||||||
=== Fine-tuning annotation-based autowiring with @Primary
|
=== Fine-tuning annotation-based autowiring with @Primary
|
||||||
|
|
||||||
Because autowiring by type may lead to multiple candidates, it is often necessary to have
|
Because autowiring by type may lead to multiple candidates, it is often necessary to have
|
||||||
more control over the selection process. One way to accomplish this is with Spring's
|
more control over the selection process. One way to accomplish this is with Spring's
|
||||||
`@Primary` annotation. `@Primary` indicates that a particular bean should be given
|
`@Primary` annotation. `@Primary` indicates that a particular bean should be given
|
||||||
|
|
@ -4369,6 +4426,7 @@ The corresponding bean definitions appear as follows.
|
||||||
|
|
||||||
[[beans-autowired-annotation-qualifiers]]
|
[[beans-autowired-annotation-qualifiers]]
|
||||||
=== Fine-tuning annotation-based autowiring with qualifiers
|
=== Fine-tuning annotation-based autowiring with qualifiers
|
||||||
|
|
||||||
`@Primary` is an effective way to use autowiring by type with several instances when one
|
`@Primary` is an effective way to use autowiring by type with several instances when one
|
||||||
primary candidate can be determined. When more control over the selection process is
|
primary candidate can be determined. When more control over the selection process is
|
||||||
required, Spring's `@Qualifier` annotation can be used. You can associate qualifier values
|
required, Spring's `@Qualifier` annotation can be used. You can associate qualifier values
|
||||||
|
|
@ -4722,6 +4780,7 @@ the following example.
|
||||||
|
|
||||||
[[beans-generics-as-qualifiers]]
|
[[beans-generics-as-qualifiers]]
|
||||||
=== Using generics as autowiring qualifiers
|
=== Using generics as autowiring qualifiers
|
||||||
|
|
||||||
In addition to the `@Qualifier` annotation, it is also possible to use Java generic types
|
In addition to the `@Qualifier` annotation, it is also possible to use Java generic types
|
||||||
as an implicit form of qualification. For example, suppose you have the following
|
as an implicit form of qualification. For example, suppose you have the following
|
||||||
configuration:
|
configuration:
|
||||||
|
|
@ -4939,18 +4998,18 @@ For details about the effects of combining various lifecycle mechanisms, see
|
||||||
|
|
||||||
[[beans-classpath-scanning]]
|
[[beans-classpath-scanning]]
|
||||||
== Classpath scanning and managed components
|
== Classpath scanning and managed components
|
||||||
Most examples in this chapter use XML to specify the configuration metadata that
|
Most examples in this chapter use XML to specify the configuration metadata that produces
|
||||||
produces each `BeanDefinition` within the Spring container. The previous section
|
each `BeanDefinition` within the Spring container. The previous section
|
||||||
(<<beans-annotation-config>>) demonstrates how to provide a lot of the configuration
|
(<<beans-annotation-config>>) demonstrates how to provide a lot of the configuration
|
||||||
metadata through source-level annotations. Even in those examples, however, the "base"
|
metadata through source-level annotations. Even in those examples, however, the "base"
|
||||||
bean definitions are explicitly defined in the XML file, while the annotations only
|
bean definitions are explicitly defined in the XML file, while the annotations only drive
|
||||||
drive the dependency injection. This section describes an option for implicitly
|
the dependency injection. This section describes an option for implicitly detecting the
|
||||||
detecting the __candidate components__ by scanning the classpath. Candidate components
|
__candidate components__ by scanning the classpath. Candidate components are classes that
|
||||||
are classes that match against a filter criteria and have a corresponding bean
|
match against a filter criteria and have a corresponding bean definition registered with
|
||||||
definition registered with the container. This removes the need to use XML to perform
|
the container. This removes the need to use XML to perform bean registration; instead you
|
||||||
bean registration, instead you can use annotations (for example @Component), AspectJ
|
can use annotations (for example `@Component`), AspectJ type expressions, or your own
|
||||||
type expressions, or your own custom filter criteria to select which classes will have
|
custom filter criteria to select which classes will have bean definitions registered with
|
||||||
bean definitions registered with the container.
|
the container.
|
||||||
|
|
||||||
[NOTE]
|
[NOTE]
|
||||||
====
|
====
|
||||||
|
|
@ -4966,7 +5025,7 @@ than using the traditional XML files. Take a look at the `@Configuration`, `@Bea
|
||||||
=== @Component and further stereotype annotations
|
=== @Component and further stereotype annotations
|
||||||
|
|
||||||
The `@Repository` annotation is a marker for any class that fulfills the role or
|
The `@Repository` annotation is a marker for any class that fulfills the role or
|
||||||
__stereotype__ (also known as Data Access Object or DAO) of a repository. Among the uses
|
__stereotype__ of a repository (also known as Data Access Object or DAO). Among the uses
|
||||||
of this marker is the automatic translation of exceptions as described in
|
of this marker is the automatic translation of exceptions as described in
|
||||||
<<orm-exception-translation>>.
|
<<orm-exception-translation>>.
|
||||||
|
|
||||||
|
|
@ -4988,15 +5047,16 @@ supported as a marker for automatic exception translation in your persistence la
|
||||||
|
|
||||||
[[beans-meta-annotations]]
|
[[beans-meta-annotations]]
|
||||||
=== Meta-annotations
|
=== Meta-annotations
|
||||||
|
|
||||||
Many of the annotations provided by Spring can be used as "meta-annotations" in
|
Many of the annotations provided by Spring can be used as "meta-annotations" in
|
||||||
your own code. A meta-annotation is simply an annotation, that can be applied to another
|
your own code. A meta-annotation is simply an annotation that can be applied to another
|
||||||
annotation. For example, The `@Service` annotation mentioned above is meta-annotated with
|
annotation. For example, the `@Service` annotation mentioned above is meta-annotated with
|
||||||
with `@Component`:
|
with `@Component`:
|
||||||
|
|
||||||
[source,java,indent=0]
|
[source,java,indent=0]
|
||||||
[subs="verbatim,quotes"]
|
[subs="verbatim,quotes"]
|
||||||
----
|
----
|
||||||
@Target({ElementType.TYPE})
|
@Target(ElementType.TYPE)
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
@Documented
|
@Documented
|
||||||
**@Component** // Spring will see this and treat @Service in the same way as @Component
|
**@Component** // Spring will see this and treat @Service in the same way as @Component
|
||||||
|
|
@ -5011,11 +5071,11 @@ Meta-annotations can also be combined together to create __composed annotations_
|
||||||
example, the `@RestController` annotation from Spring MVC is __composed__ of
|
example, the `@RestController` annotation from Spring MVC is __composed__ of
|
||||||
`@Controller` and `@ResponseBody`.
|
`@Controller` and `@ResponseBody`.
|
||||||
|
|
||||||
With the exception of `value()`, meta-annotated types may redeclare attributes from the
|
With the exception of the `value` attribute, composed annotations may redeclare
|
||||||
source annotation to allow user customization. This can be particularly useful when you
|
attributes from meta-annotations to allow user customization. This can be particularly
|
||||||
want to only expose a subset of the source annotation attributes. For example, here is a
|
useful when you want to only expose a subset of the meta-annotation's attributes. For
|
||||||
custom `@Scope` annotation that defines `session` scope, but still allows customization
|
example, here is a custom `@Scope` annotation that hardcodes the scope name to `session`
|
||||||
of the `proxyMode`.
|
but still allows customization of the `proxyMode`.
|
||||||
|
|
||||||
|
|
||||||
[source,java,indent=0]
|
[source,java,indent=0]
|
||||||
|
|
@ -5037,6 +5097,7 @@ of the `proxyMode`.
|
||||||
|
|
||||||
[[beans-scanning-autodetection]]
|
[[beans-scanning-autodetection]]
|
||||||
=== Automatically detecting classes and registering bean definitions
|
=== Automatically detecting classes and registering bean definitions
|
||||||
|
|
||||||
Spring can automatically detect stereotyped classes and register corresponding
|
Spring can automatically detect stereotyped classes and register corresponding
|
||||||
++BeanDefinition++s with the `ApplicationContext`. For example, the following two classes
|
++BeanDefinition++s with the `ApplicationContext`. For example, the following two classes
|
||||||
are eligible for such autodetection:
|
are eligible for such autodetection:
|
||||||
|
|
@ -5140,6 +5201,7 @@ with a value of false.
|
||||||
|
|
||||||
[[beans-scanning-filters]]
|
[[beans-scanning-filters]]
|
||||||
=== Using filters to customize scanning
|
=== Using filters to customize scanning
|
||||||
|
|
||||||
By default, classes annotated with `@Component`, `@Repository`, `@Service`,
|
By default, classes annotated with `@Component`, `@Repository`, `@Service`,
|
||||||
`@Controller`, or a custom annotation that itself is annotated with `@Component` are the
|
`@Controller`, or a custom annotation that itself is annotated with `@Component` are the
|
||||||
only detected candidate components. However, you can modify and extend this behavior
|
only detected candidate components. However, you can modify and extend this behavior
|
||||||
|
|
@ -5217,6 +5279,7 @@ will in effect disable automatic detection of classes annotated with `@Component
|
||||||
|
|
||||||
[[beans-factorybeans-annotations]]
|
[[beans-factorybeans-annotations]]
|
||||||
=== Defining bean metadata within components
|
=== Defining bean metadata within components
|
||||||
|
|
||||||
Spring components can also contribute bean definition metadata to the container. You do
|
Spring components can also contribute bean definition metadata to the container. You do
|
||||||
this with the same `@Bean` annotation used to define bean metadata within `@Configuration`
|
this with the same `@Bean` annotation used to define bean metadata within `@Configuration`
|
||||||
annotated classes. Here is a simple example:
|
annotated classes. Here is a simple example:
|
||||||
|
|
@ -5347,6 +5410,7 @@ inheritance being possible through Java 8 default methods as of Spring 4.2.
|
||||||
|
|
||||||
[[beans-scanning-name-generator]]
|
[[beans-scanning-name-generator]]
|
||||||
=== Naming autodetected components
|
=== Naming autodetected components
|
||||||
|
|
||||||
When a component is autodetected as part of the scanning process, its bean name is
|
When a component is autodetected as part of the scanning process, its bean name is
|
||||||
generated by the `BeanNameGenerator` strategy known to that scanner. By default, any
|
generated by the `BeanNameGenerator` strategy known to that scanner. By default, any
|
||||||
Spring stereotype annotation ( `@Component`, `@Repository`, `@Service`, and
|
Spring stereotype annotation ( `@Component`, `@Repository`, `@Service`, and
|
||||||
|
|
@ -5412,6 +5476,7 @@ auto-generated names are adequate whenever the container is responsible for wiri
|
||||||
|
|
||||||
[[beans-scanning-scope-resolver]]
|
[[beans-scanning-scope-resolver]]
|
||||||
=== Providing a scope for autodetected components
|
=== Providing a scope for autodetected components
|
||||||
|
|
||||||
As with Spring-managed components in general, the default and most common scope for
|
As with Spring-managed components in general, the default and most common scope for
|
||||||
autodetected components is singleton. However, sometimes you need other scopes, which
|
autodetected components is singleton. However, sometimes you need other scopes, which
|
||||||
Spring 2.5 provides with a new `@Scope` annotation. Simply provide the name of the scope
|
Spring 2.5 provides with a new `@Scope` annotation. Simply provide the name of the scope
|
||||||
|
|
@ -5484,6 +5549,7 @@ the following configuration will result in standard JDK dynamic proxies:
|
||||||
|
|
||||||
[[beans-scanning-qualifiers]]
|
[[beans-scanning-qualifiers]]
|
||||||
=== Providing qualifier metadata with annotations
|
=== Providing qualifier metadata with annotations
|
||||||
|
|
||||||
The `@Qualifier` annotation is discussed in <<beans-autowired-annotation-qualifiers>>.
|
The `@Qualifier` annotation is discussed in <<beans-autowired-annotation-qualifiers>>.
|
||||||
The examples in that section demonstrate the use of the `@Qualifier` annotation and
|
The examples in that section demonstrate the use of the `@Qualifier` annotation and
|
||||||
custom qualifier annotations to provide fine-grained control when you resolve autowire
|
custom qualifier annotations to provide fine-grained control when you resolve autowire
|
||||||
|
|
@ -5680,6 +5746,7 @@ component-scanning in the exact same way as when using Spring annotations:
|
||||||
|
|
||||||
[[beans-standard-annotations-limitations]]
|
[[beans-standard-annotations-limitations]]
|
||||||
=== Limitations of the standard approach
|
=== Limitations of the standard approach
|
||||||
|
|
||||||
When working with standard annotations, it is important to know that some significant
|
When working with standard annotations, it is important to know that some significant
|
||||||
features are not available as shown in the table below:
|
features are not available as shown in the table below:
|
||||||
|
|
||||||
|
|
@ -5796,6 +5863,7 @@ Java-based configuration.
|
||||||
|
|
||||||
[[beans-java-instantiating-container]]
|
[[beans-java-instantiating-container]]
|
||||||
=== Instantiating the Spring container using AnnotationConfigApplicationContext
|
=== Instantiating the Spring container using AnnotationConfigApplicationContext
|
||||||
|
|
||||||
The sections below document Spring's `AnnotationConfigApplicationContext`, new in Spring
|
The sections below document Spring's `AnnotationConfigApplicationContext`, new in Spring
|
||||||
3.0. This versatile `ApplicationContext` implementation is capable of accepting not only
|
3.0. This versatile `ApplicationContext` implementation is capable of accepting not only
|
||||||
`@Configuration` classes as input, but also plain `@Component` classes and classes
|
`@Configuration` classes as input, but also plain `@Component` classes and classes
|
||||||
|
|
@ -5812,6 +5880,7 @@ used within those classes where necessary.
|
||||||
|
|
||||||
[[beans-java-instantiating-container-contstructor]]
|
[[beans-java-instantiating-container-contstructor]]
|
||||||
==== Simple construction
|
==== Simple construction
|
||||||
|
|
||||||
In much the same way that Spring XML files are used as input when instantiating a
|
In much the same way that Spring XML files are used as input when instantiating a
|
||||||
`ClassPathXmlApplicationContext`, `@Configuration` classes may be used as input when
|
`ClassPathXmlApplicationContext`, `@Configuration` classes may be used as input when
|
||||||
instantiating an `AnnotationConfigApplicationContext`. This allows for completely
|
instantiating an `AnnotationConfigApplicationContext`. This allows for completely
|
||||||
|
|
@ -6005,6 +6074,7 @@ You can use the `@Bean` annotation in a `@Configuration`-annotated or in a
|
||||||
|
|
||||||
[[beans-java-declaring-a-bean]]
|
[[beans-java-declaring-a-bean]]
|
||||||
==== Declaring a bean
|
==== Declaring a bean
|
||||||
|
|
||||||
To declare a bean, simply annotate a method with the `@Bean` annotation. You use this
|
To declare a bean, simply annotate a method with the `@Bean` annotation. You use this
|
||||||
method to register a bean definition within an `ApplicationContext` of the type
|
method to register a bean definition within an `ApplicationContext` of the type
|
||||||
specified as the method's return value. By default, the bean name will be the same as
|
specified as the method's return value. By default, the bean name will be the same as
|
||||||
|
|
@ -6046,6 +6116,7 @@ transferService -> com.acme.TransferServiceImpl
|
||||||
|
|
||||||
[[beans-java-dependencies]]
|
[[beans-java-dependencies]]
|
||||||
==== Bean dependencies
|
==== Bean dependencies
|
||||||
|
|
||||||
A `@Bean` annotated method can have an arbitrary number of parameters describing the
|
A `@Bean` annotated method can have an arbitrary number of parameters describing the
|
||||||
dependencies required to build that bean. For instance if our `TransferService`
|
dependencies required to build that bean. For instance if our `TransferService`
|
||||||
requires an `AccountRepository` we can materialize that dependency via a method
|
requires an `AccountRepository` we can materialize that dependency via a method
|
||||||
|
|
@ -6071,6 +6142,7 @@ injection, see <<beans-constructor-injection,the relevant section>> for more det
|
||||||
|
|
||||||
[[beans-java-lifecycle-callbacks]]
|
[[beans-java-lifecycle-callbacks]]
|
||||||
==== Receiving lifecycle callbacks
|
==== Receiving lifecycle callbacks
|
||||||
|
|
||||||
Any classes defined with the `@Bean` annotation support the regular lifecycle callbacks
|
Any classes defined with the `@Bean` annotation support the regular lifecycle callbacks
|
||||||
and can use the `@PostConstruct` and `@PreDestroy` annotations from JSR-250, see
|
and can use the `@PostConstruct` and `@PreDestroy` annotations from JSR-250, see
|
||||||
<<beans-postconstruct-and-predestroy-annotations,JSR-250 annotations>> for further
|
<<beans-postconstruct-and-predestroy-annotations,JSR-250 annotations>> for further
|
||||||
|
|
@ -6233,6 +6305,7 @@ link) to our `@Bean` using Java, it would look like the following:
|
||||||
|
|
||||||
[[beans-java-customizing-bean-naming]]
|
[[beans-java-customizing-bean-naming]]
|
||||||
==== Customizing bean naming
|
==== Customizing bean naming
|
||||||
|
|
||||||
By default, configuration classes use a `@Bean` method's name as the name of the
|
By default, configuration classes use a `@Bean` method's name as the name of the
|
||||||
resulting bean. This functionality can be overridden, however, with the `name` attribute.
|
resulting bean. This functionality can be overridden, however, with the `name` attribute.
|
||||||
|
|
||||||
|
|
@ -6253,6 +6326,7 @@ resulting bean. This functionality can be overridden, however, with the `name` a
|
||||||
|
|
||||||
[[beans-java-bean-aliasing]]
|
[[beans-java-bean-aliasing]]
|
||||||
==== Bean aliasing
|
==== Bean aliasing
|
||||||
|
|
||||||
As discussed in <<beans-beanname>>, it is sometimes desirable to give a single bean
|
As discussed in <<beans-beanname>>, it is sometimes desirable to give a single bean
|
||||||
multiple names, otherwise known as__bean aliasing__. The `name` attribute of the `@Bean`
|
multiple names, otherwise known as__bean aliasing__. The `name` attribute of the `@Bean`
|
||||||
annotation accepts a String array for this purpose.
|
annotation accepts a String array for this purpose.
|
||||||
|
|
@ -6274,6 +6348,7 @@ annotation accepts a String array for this purpose.
|
||||||
|
|
||||||
[[beans-java-bean-description]]
|
[[beans-java-bean-description]]
|
||||||
==== Bean description
|
==== Bean description
|
||||||
|
|
||||||
Sometimes it is helpful to provide a more detailed textual description of a bean. This can
|
Sometimes it is helpful to provide a more detailed textual description of a bean. This can
|
||||||
be particularly useful when beans are exposed (perhaps via JMX) for monitoring purposes.
|
be particularly useful when beans are exposed (perhaps via JMX) for monitoring purposes.
|
||||||
|
|
||||||
|
|
@ -6308,6 +6383,7 @@ inter-bean dependencies. See <<beans-java-basic-concepts>> for a general introdu
|
||||||
|
|
||||||
[[beans-java-injecting-dependencies]]
|
[[beans-java-injecting-dependencies]]
|
||||||
==== Injecting inter-bean dependencies
|
==== Injecting inter-bean dependencies
|
||||||
|
|
||||||
When ++@Bean++s have dependencies on one another, expressing that dependency is as simple
|
When ++@Bean++s have dependencies on one another, expressing that dependency is as simple
|
||||||
as having one bean method call another:
|
as having one bean method call another:
|
||||||
|
|
||||||
|
|
@ -6343,6 +6419,7 @@ using plain `@Component` classes.
|
||||||
|
|
||||||
[[beans-java-method-injection]]
|
[[beans-java-method-injection]]
|
||||||
==== Lookup method injection
|
==== Lookup method injection
|
||||||
|
|
||||||
As noted earlier, <<beans-factory-method-injection,lookup method injection>> is an
|
As noted earlier, <<beans-factory-method-injection,lookup method injection>> is an
|
||||||
advanced feature that you should use rarely. It is useful in cases where a
|
advanced feature that you should use rarely. It is useful in cases where a
|
||||||
singleton-scoped bean has a dependency on a prototype-scoped bean. Using Java for this
|
singleton-scoped bean has a dependency on a prototype-scoped bean. Using Java for this
|
||||||
|
|
@ -6396,6 +6473,7 @@ the abstract `createCommand()` method is overridden in such a way that it looks
|
||||||
|
|
||||||
[[beans-java-further-information-java-config]]
|
[[beans-java-further-information-java-config]]
|
||||||
==== Further information about how Java-based configuration works internally
|
==== Further information about how Java-based configuration works internally
|
||||||
|
|
||||||
The following example shows a `@Bean` annotated method being called twice:
|
The following example shows a `@Bean` annotated method being called twice:
|
||||||
|
|
||||||
[source,java,indent=0]
|
[source,java,indent=0]
|
||||||
|
|
@ -6730,10 +6808,11 @@ than the usual process of navigating interface-based code.
|
||||||
|
|
||||||
|
|
||||||
[[beans-java-conditional]]
|
[[beans-java-conditional]]
|
||||||
==== Conditionally including @Configuration classes or @Beans
|
==== Conditionally include @Configuration classes or @Bean methods
|
||||||
It is often useful to conditionally enable to disable a complete `@Configuration` class,
|
|
||||||
|
It is often useful to conditionally enable or disable a complete `@Configuration` class,
|
||||||
or even individual `@Bean` methods, based on some arbitrary system state. One common
|
or even individual `@Bean` methods, based on some arbitrary system state. One common
|
||||||
example of this it to use the `@Profile` annotation to active beans only when a specific
|
example of this is to use the `@Profile` annotation to activate beans only when a specific
|
||||||
profile has been enabled in the Spring `Environment` (see <<beans-definition-profiles>>
|
profile has been enabled in the Spring `Environment` (see <<beans-definition-profiles>>
|
||||||
for details).
|
for details).
|
||||||
|
|
||||||
|
|
@ -6773,6 +6852,7 @@ See the {javadoc-baseurl}/org/springframework/context/annotation/Conditional.htm
|
||||||
|
|
||||||
[[beans-java-combining]]
|
[[beans-java-combining]]
|
||||||
==== Combining Java and XML configuration
|
==== Combining Java and XML configuration
|
||||||
|
|
||||||
Spring's `@Configuration` class support does not aim to be a 100% complete replacement
|
Spring's `@Configuration` class support does not aim to be a 100% complete replacement
|
||||||
for Spring XML. Some facilities such as Spring XML namespaces remain an ideal way to
|
for Spring XML. Some facilities such as Spring XML namespaces remain an ideal way to
|
||||||
configure the container. In cases where XML is convenient or necessary, you have a
|
configure the container. In cases where XML is convenient or necessary, you have a
|
||||||
|
|
@ -6796,7 +6876,7 @@ Remember that `@Configuration` classes are ultimately just bean definitions in t
|
||||||
container. In this example, we create a `@Configuration` class named `AppConfig` and
|
container. In this example, we create a `@Configuration` class named `AppConfig` and
|
||||||
include it within `system-test-config.xml` as a `<bean/>` definition. Because
|
include it within `system-test-config.xml` as a `<bean/>` definition. Because
|
||||||
`<context:annotation-config/>` is switched on, the container will recognize the
|
`<context:annotation-config/>` is switched on, the container will recognize the
|
||||||
`@Configuration` annotation, and process the `@Bean` methods declared in `AppConfig`
|
`@Configuration` annotation and process the `@Bean` methods declared in `AppConfig`
|
||||||
properly.
|
properly.
|
||||||
|
|
||||||
[source,java,indent=0]
|
[source,java,indent=0]
|
||||||
|
|
@ -6821,10 +6901,11 @@ properly.
|
||||||
}
|
}
|
||||||
----
|
----
|
||||||
|
|
||||||
|
*system-test-config.xml*:
|
||||||
|
|
||||||
[source,xml,indent=0]
|
[source,xml,indent=0]
|
||||||
[subs="verbatim,quotes"]
|
[subs="verbatim,quotes"]
|
||||||
----
|
----
|
||||||
system-test-config.xml
|
|
||||||
<beans>
|
<beans>
|
||||||
<!-- enable processing of annotations such as @Autowired and @Configuration -->
|
<!-- enable processing of annotations such as @Autowired and @Configuration -->
|
||||||
<context:annotation-config/>
|
<context:annotation-config/>
|
||||||
|
|
@ -6840,10 +6921,11 @@ properly.
|
||||||
</beans>
|
</beans>
|
||||||
----
|
----
|
||||||
|
|
||||||
|
*jdbc.properties*:
|
||||||
|
|
||||||
[literal]
|
[literal]
|
||||||
[subs="verbatim,quotes"]
|
[subs="verbatim,quotes"]
|
||||||
----
|
----
|
||||||
jdbc.properties
|
|
||||||
jdbc.url=jdbc:hsqldb:hsql://localhost/xdb
|
jdbc.url=jdbc:hsqldb:hsql://localhost/xdb
|
||||||
jdbc.username=sa
|
jdbc.username=sa
|
||||||
jdbc.password=
|
jdbc.password=
|
||||||
|
|
@ -6861,11 +6943,11 @@ jdbc.password=
|
||||||
|
|
||||||
[NOTE]
|
[NOTE]
|
||||||
====
|
====
|
||||||
In `system-test-config.xml` above, the `AppConfig<bean/>` does not declare an `id`
|
In `system-test-config.xml` above, the `AppConfig` `<bean/>` does not declare an `id`
|
||||||
element. While it would be acceptable to do so, it is unnecessary given that no other
|
element. While it would be acceptable to do so, it is unnecessary given that no other
|
||||||
bean will ever refer to it, and it is unlikely that it will be explicitly fetched from
|
bean will ever refer to it, and it is unlikely that it will be explicitly fetched from
|
||||||
the container by name. Likewise with the `DataSource` bean - it is only ever autowired
|
the container by name. Likewise with the `DataSource` bean - it is only ever autowired
|
||||||
by type, so an explicit bean id is not strictly required.
|
by type, so an explicit bean `id` is not strictly required.
|
||||||
====
|
====
|
||||||
--
|
--
|
||||||
|
|
||||||
|
|
@ -6875,13 +6957,14 @@ Because `@Configuration` is meta-annotated with `@Component`, `@Configuration`-a
|
||||||
classes are automatically candidates for component scanning. Using the same scenario as
|
classes are automatically candidates for component scanning. Using the same scenario as
|
||||||
above, we can redefine `system-test-config.xml` to take advantage of component-scanning.
|
above, we can redefine `system-test-config.xml` to take advantage of component-scanning.
|
||||||
Note that in this case, we don't need to explicitly declare
|
Note that in this case, we don't need to explicitly declare
|
||||||
`<context:annotation-config/>`, because `<context:component-scan/>` enables all the same
|
`<context:annotation-config/>`, because `<context:component-scan/>` enables the same
|
||||||
functionality.
|
functionality.
|
||||||
|
|
||||||
|
*system-test-config.xml*:
|
||||||
|
|
||||||
[source,xml,indent=0]
|
[source,xml,indent=0]
|
||||||
[subs="verbatim,quotes"]
|
[subs="verbatim,quotes"]
|
||||||
----
|
----
|
||||||
system-test-config.xml
|
|
||||||
<beans>
|
<beans>
|
||||||
<!-- picks up and registers AppConfig as a bean definition -->
|
<!-- picks up and registers AppConfig as a bean definition -->
|
||||||
<context:component-scan base-package="com.acme"/>
|
<context:component-scan base-package="com.acme"/>
|
||||||
|
|
@ -7043,9 +7126,9 @@ this need.
|
||||||
==== @Profile
|
==== @Profile
|
||||||
|
|
||||||
The {javadoc-baseurl}/org/springframework/context/annotation/Profile.html[`@Profile`]
|
The {javadoc-baseurl}/org/springframework/context/annotation/Profile.html[`@Profile`]
|
||||||
annotation allows to indicate that a component is eligible for registration
|
annotation allows you to indicate that a component is eligible for registration
|
||||||
when one or more specified profiles are active. Using our example above, we
|
when one or more specified profiles are active. Using our example above, we
|
||||||
can rewrite the _dataSource_ configuration as follows:
|
can rewrite the `dataSource` configuration as follows:
|
||||||
|
|
||||||
[source,java,indent=0]
|
[source,java,indent=0]
|
||||||
[subs="verbatim,quotes"]
|
[subs="verbatim,quotes"]
|
||||||
|
|
@ -7081,9 +7164,9 @@ can rewrite the _dataSource_ configuration as follows:
|
||||||
----
|
----
|
||||||
|
|
||||||
|
|
||||||
`@Profile` can be used as a meta-annotation, for the purpose of composing
|
`@Profile` can be used as a <<beans-meta-annotations,meta-annotation>> for the purpose
|
||||||
custom stereotype annotations. The following example defines a `@Production`
|
of creating a custom _composed annotation_. The following example defines a custom
|
||||||
custom annotation that can be used as a drop-in replacement of
|
`@Production` annotation that can be used as a drop-in replacement for
|
||||||
`@Profile("production")`:
|
`@Profile("production")`:
|
||||||
|
|
||||||
[source,java,indent=0]
|
[source,java,indent=0]
|
||||||
|
|
@ -7096,8 +7179,8 @@ custom annotation that can be used as a drop-in replacement of
|
||||||
}
|
}
|
||||||
----
|
----
|
||||||
|
|
||||||
`@Profile` can also be specified at method-level to include only one particular
|
`@Profile` can also be declared at the method level to include only one particular bean
|
||||||
bean of a configuration class:
|
of a configuration class:
|
||||||
|
|
||||||
[source,java,indent=0]
|
[source,java,indent=0]
|
||||||
[subs="verbatim,quotes"]
|
[subs="verbatim,quotes"]
|
||||||
|
|
@ -7126,22 +7209,21 @@ bean of a configuration class:
|
||||||
|
|
||||||
[TIP]
|
[TIP]
|
||||||
====
|
====
|
||||||
If a `@Configuration` class is marked with `@Profile`, all of the `@Bean` methods
|
If a `@Configuration` class is marked with `@Profile`, all of the `@Bean` methods and
|
||||||
and `@Import` annotations associated with that class will be bypassed unless one
|
`@Import` annotations associated with that class will be bypassed unless one or more of
|
||||||
or more of the specified profiles are active. If a `@Component` or `@Configuration`
|
the specified profiles are active. If a `@Component` or `@Configuration` class is marked
|
||||||
class is marked with `@Profile({"p1", "p2"})`, that class will not be registered/
|
with `@Profile({"p1", "p2"})`, that class will not be registered/processed unless
|
||||||
processed unless profiles 'p1' and/or 'p2' have been activated. If a given profile
|
profiles 'p1' and/or 'p2' have been activated. If a given profile is prefixed with the
|
||||||
is prefixed with the NOT operator (`!`), the annotated element will be registered
|
NOT operator (`!`), the annotated element will be registered if the profile is **not**
|
||||||
if the profile is **not** active. e.g., for `@Profile({"p1", "!p2"})`, registration
|
active. For example, given `@Profile({"p1", "!p2"})`, registration will occur if profile
|
||||||
will occur if profile 'p1' is active or if profile 'p2' is not active.
|
'p1' is active or if profile 'p2' is not active.
|
||||||
====
|
====
|
||||||
|
|
||||||
[[beans-definition-profiles-xml]]
|
[[beans-definition-profiles-xml]]
|
||||||
=== XML Bean definition profiles
|
=== XML bean definition profiles
|
||||||
|
|
||||||
The XML counterpart is an update of the `beans` element that accepts a
|
The XML counterpart is the `profile` attribute of the `<beans>` element. Our sample
|
||||||
`profile` attribute. Our sample configuration above can be rewritten in two XML
|
configuration above can be rewritten in two XML files as follows:
|
||||||
files as follows:
|
|
||||||
|
|
||||||
[source,xml,indent=0]
|
[source,xml,indent=0]
|
||||||
[subs="verbatim,quotes"]
|
[subs="verbatim,quotes"]
|
||||||
|
|
@ -7203,15 +7285,16 @@ last ones in the file. This should help provide flexibility without incurring
|
||||||
clutter in the XML files.
|
clutter in the XML files.
|
||||||
|
|
||||||
[[beans-definition-profiles-enable]]
|
[[beans-definition-profiles-enable]]
|
||||||
==== Enabling a profile
|
==== Activating a profile
|
||||||
|
|
||||||
Now that we have updated our configuration, we still need to instruct which
|
Now that we have updated our configuration, we still need to instruct Spring which
|
||||||
profile is active. If we started our sample application right now, we would see
|
profile is active. If we started our sample application right now, we would see
|
||||||
a `NoSuchBeanDefinitionException` thrown, because the container could not find
|
a `NoSuchBeanDefinitionException` thrown, because the container could not find
|
||||||
the Spring bean named `dataSource`.
|
the Spring bean named `dataSource`.
|
||||||
|
|
||||||
Activating a profile can be done in several ways, but the most straightforward
|
Activating a profile can be done in several ways, but the most straightforward is to do
|
||||||
is to do it programmatically against the `ApplicationContext` API:
|
it programmatically against the `Environment` API which is available via an
|
||||||
|
`ApplicationContext`:
|
||||||
|
|
||||||
[source,java,indent=0]
|
[source,java,indent=0]
|
||||||
[subs="verbatim,quotes"]
|
[subs="verbatim,quotes"]
|
||||||
|
|
@ -7222,10 +7305,12 @@ is to do it programmatically against the `ApplicationContext` API:
|
||||||
ctx.refresh();
|
ctx.refresh();
|
||||||
----
|
----
|
||||||
|
|
||||||
In addition, profiles may also be activated declaratively through the `spring.profiles.active`
|
In addition, profiles may also be activated declaratively through the
|
||||||
property which may be specified through system environment variables, JVM system properties,
|
`spring.profiles.active` property which may be specified through system environment
|
||||||
servlet context parameters in `web.xml` or even as an entry in JNDI (see
|
variables, JVM system properties, servlet context parameters in `web.xml`, or even as an
|
||||||
<<beans-property-source-abstraction>>).
|
entry in JNDI (see <<beans-property-source-abstraction>>). In integration tests, active
|
||||||
|
profiles can be declared via the `@ActiveProfiles` annotation in the `spring-test` module
|
||||||
|
(see <<testcontext-ctx-management-env-profiles>>).
|
||||||
|
|
||||||
Note that profiles are not an "either-or" proposition; it is possible to activate multiple
|
Note that profiles are not an "either-or" proposition; it is possible to activate multiple
|
||||||
profiles at once. Programmatically, simply provide multiple profile names to the
|
profiles at once. Programmatically, simply provide multiple profile names to the
|
||||||
|
|
@ -7248,7 +7333,7 @@ Declaratively, `spring.profiles.active` may accept a comma-separated list of pro
|
||||||
[[beans-definition-profiles-default]]
|
[[beans-definition-profiles-default]]
|
||||||
==== Default profile
|
==== Default profile
|
||||||
|
|
||||||
The _default_ profile represents the profile that is enabled by default. Consider the
|
The _default_ profile represents the profile that is enabled by default. Consider the
|
||||||
following:
|
following:
|
||||||
|
|
||||||
[source,java,indent=0]
|
[source,java,indent=0]
|
||||||
|
|
@ -7272,13 +7357,13 @@ If no profile is active, the `dataSource` above will be created; this can be
|
||||||
seen as a way to provide a _default_ definition for one or more beans. If any
|
seen as a way to provide a _default_ definition for one or more beans. If any
|
||||||
profile is enabled, the _default_ profile will not apply.
|
profile is enabled, the _default_ profile will not apply.
|
||||||
|
|
||||||
The name of that default profile can be changed using `setDefaultProfiles` on
|
The name of the default profile can be changed using `setDefaultProfiles()` on
|
||||||
the `Environment` or declaratively using the `spring.profiles.default` property.
|
the `Environment` or declaratively using the `spring.profiles.default` property.
|
||||||
|
|
||||||
[[beans-property-source-abstraction]]
|
[[beans-property-source-abstraction]]
|
||||||
=== PropertySource Abstraction
|
=== PropertySource abstraction
|
||||||
|
|
||||||
Spring's Environment abstraction provides search operations over a configurable
|
Spring's `Environment` abstraction provides search operations over a configurable
|
||||||
hierarchy of property sources. To explain fully, consider the following:
|
hierarchy of property sources. To explain fully, consider the following:
|
||||||
|
|
||||||
[source,java,indent=0]
|
[source,java,indent=0]
|
||||||
|
|
@ -7695,6 +7780,7 @@ out the `ReloadableResourceBundleMessageSource` javadocs for details.
|
||||||
|
|
||||||
[[context-functionality-events]]
|
[[context-functionality-events]]
|
||||||
=== Standard and Custom Events
|
=== Standard and Custom Events
|
||||||
|
|
||||||
Event handling in the `ApplicationContext` is provided through the `ApplicationEvent`
|
Event handling in the `ApplicationContext` is provided through the `ApplicationEvent`
|
||||||
class and `ApplicationListener` interface. If a bean that implements the
|
class and `ApplicationListener` interface. If a bean that implements the
|
||||||
`ApplicationListener` interface is deployed into the context, every time an
|
`ApplicationListener` interface is deployed into the context, every time an
|
||||||
|
|
@ -8058,6 +8144,7 @@ an event.
|
||||||
|
|
||||||
[[context-functionality-resources]]
|
[[context-functionality-resources]]
|
||||||
=== Convenient access to low-level resources
|
=== Convenient access to low-level resources
|
||||||
|
|
||||||
For optimal usage and understanding of application contexts, users should generally
|
For optimal usage and understanding of application contexts, users should generally
|
||||||
familiarize themselves with Spring's `Resource` abstraction, as described in the chapter
|
familiarize themselves with Spring's `Resource` abstraction, as described in the chapter
|
||||||
<<resources>>.
|
<<resources>>.
|
||||||
|
|
@ -8124,6 +8211,7 @@ in any subdirectory of "WEB-INF".
|
||||||
|
|
||||||
[[context-deploy-rar]]
|
[[context-deploy-rar]]
|
||||||
=== Deploying a Spring ApplicationContext as a Java EE RAR file
|
=== Deploying a Spring ApplicationContext as a Java EE RAR file
|
||||||
|
|
||||||
It is possible to deploy a Spring ApplicationContext as a RAR file, encapsulating the
|
It is possible to deploy a Spring ApplicationContext as a RAR file, encapsulating the
|
||||||
context and all of its required bean classes and library JARs in a Java EE RAR deployment
|
context and all of its required bean classes and library JARs in a Java EE RAR deployment
|
||||||
unit. This is the equivalent of bootstrapping a standalone ApplicationContext, just hosted
|
unit. This is the equivalent of bootstrapping a standalone ApplicationContext, just hosted
|
||||||
|
|
@ -8269,6 +8357,7 @@ important functionality such as property placeholder replacement and AOP.
|
||||||
|
|
||||||
[[beans-servicelocator]]
|
[[beans-servicelocator]]
|
||||||
=== Glue code and the evil singleton
|
=== Glue code and the evil singleton
|
||||||
|
|
||||||
It is best to write most application code in a dependency-injection (DI) style, where
|
It is best to write most application code in a dependency-injection (DI) style, where
|
||||||
that code is served out of a Spring IoC container, has its own dependencies supplied by
|
that code is served out of a Spring IoC container, has its own dependencies supplied by
|
||||||
the container when it is created, and is completely unaware of the container. However,
|
the container when it is created, and is completely unaware of the container. However,
|
||||||
|
|
|
||||||
|
|
@ -96,7 +96,7 @@ then combine attributes from both annotations to create merged CORS configuratio
|
||||||
@RequestMapping("/account")
|
@RequestMapping("/account")
|
||||||
public class AccountController {
|
public class AccountController {
|
||||||
|
|
||||||
@CrossOrigin(origins = "http://domain2.com")
|
@CrossOrigin("http://domain2.com")
|
||||||
@RequestMapping("/{id}")
|
@RequestMapping("/{id}")
|
||||||
public Account retrieve(@PathVariable Long id) {
|
public Account retrieve(@PathVariable Long id) {
|
||||||
// ...
|
// ...
|
||||||
|
|
|
||||||
|
|
@ -627,7 +627,7 @@ to narrow the mapping.
|
||||||
|
|
||||||
|
|
||||||
[[mvc-ann-requestmapping-proxying]]
|
[[mvc-ann-requestmapping-proxying]]
|
||||||
==== ++@Controller++'s and AOP Proxying
|
==== @Controller and AOP Proxying
|
||||||
|
|
||||||
In some cases a controller may need to be decorated with an AOP proxy at runtime.
|
In some cases a controller may need to be decorated with an AOP proxy at runtime.
|
||||||
One example is if you choose to have `@Transactional` annotations directly on the
|
One example is if you choose to have `@Transactional` annotations directly on the
|
||||||
|
|
@ -1910,7 +1910,8 @@ which case they apply to matching controllers. This provides an alternative to u
|
||||||
|
|
||||||
|
|
||||||
[[mvc-ann-controller-advice]]
|
[[mvc-ann-controller-advice]]
|
||||||
==== Advising controllers with the `@ControllerAdvice` annotation
|
==== Advising controllers with @ControllerAdvice
|
||||||
|
|
||||||
The `@ControllerAdvice` annotation is a component annotation allowing implementation
|
The `@ControllerAdvice` annotation is a component annotation allowing implementation
|
||||||
classes to be auto-detected through classpath scanning. It is automatically enabled when
|
classes to be auto-detected through classpath scanning. It is automatically enabled when
|
||||||
using the MVC namespace or the MVC Java config.
|
using the MVC namespace or the MVC Java config.
|
||||||
|
|
@ -2518,11 +2519,6 @@ directly on `RequestMappingHandlerAdapter`.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
[[mvc-viewresolver]]
|
[[mvc-viewresolver]]
|
||||||
== Resolving views
|
== Resolving views
|
||||||
All MVC frameworks for web applications provide a way to address views. Spring provides
|
All MVC frameworks for web applications provide a way to address views. Spring provides
|
||||||
|
|
|
||||||
|
|
@ -1554,7 +1554,7 @@ the TCP connection is established is different from the host providing the
|
||||||
cloud-based STOMP service.
|
cloud-based STOMP service.
|
||||||
|
|
||||||
[[websocket-stomp-destination-separator]]
|
[[websocket-stomp-destination-separator]]
|
||||||
=== Using Dot as Separator in `@MessageMapping` Destinations
|
=== Using Dot as Separator in @MessageMapping Destinations
|
||||||
|
|
||||||
Although slash-separated path patterns are familiar to web developers, in messaging
|
Although slash-separated path patterns are familiar to web developers, in messaging
|
||||||
it is common to use a "." as the separator, for example in the names of topics, queues,
|
it is common to use a "." as the separator, for example in the names of topics, queues,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue