Skip to content

Commit 3dad61f

Browse files
committed
Backported applicable beans chapter revisions
1 parent d73f91e commit 3dad61f

File tree

1 file changed

+45
-41
lines changed

1 file changed

+45
-41
lines changed

src/asciidoc/core-beans.adoc

Lines changed: 45 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2274,7 +2274,7 @@ the __scope__ of the objects created from a particular bean definition. This app
22742274
powerful and flexible in that you can __choose__ the scope of the objects you create
22752275
through configuration instead of having to bake in the scope of an object at the Java
22762276
class level. Beans can be defined to be deployed in one of a number of scopes: out of
2277-
the box, the Spring Framework supports five scopes, three of which are available only if
2277+
the box, the Spring Framework supports seven scopes, five of which are available only if
22782278
you use a web-aware `ApplicationContext`.
22792279

22802280
The following scopes are supported out of the box. You can also create
@@ -2301,14 +2301,18 @@ The following scopes are supported out of the box. You can also create
23012301
| Scopes a single bean definition to the lifecycle of an HTTP `Session`. Only valid in
23022302
the context of a web-aware Spring `ApplicationContext`.
23032303

2304-
| <<beans-factory-scopes-global-session,global session>>
2304+
| <<beans-factory-scopes-global-session,globalSession>>
23052305
| Scopes a single bean definition to the lifecycle of a global HTTP `Session`. Typically
2306-
only valid when used in a portlet context. Only valid in the context of a web-aware
2306+
only valid when used in a Portlet context. Only valid in the context of a web-aware
23072307
Spring `ApplicationContext`.
23082308

23092309
| <<beans-factory-scopes-application,application>>
23102310
| Scopes a single bean definition to the lifecycle of a `ServletContext`. Only valid in
23112311
the context of a web-aware Spring `ApplicationContext`.
2312+
2313+
| <<websocket-stomp-websocket-scope,websocket>>
2314+
| Scopes a single bean definition to the lifecycle of a `WebSocket`. Only valid in
2315+
the context of a web-aware Spring `ApplicationContext`.
23122316
|===
23132317

23142318
[NOTE]
@@ -2418,22 +2422,22 @@ runtime more than once, see <<beans-factory-method-injection>>
24182422

24192423

24202424
[[beans-factory-scopes-other]]
2421-
=== Request, session, and global session scopes
2425+
=== Request, session, global session, application, and WebSocket scopes
24222426

2423-
The `request`, `session`, and `global session` scopes are __only__ available if you use
2424-
a web-aware Spring `ApplicationContext` implementation (such as
2425-
`XmlWebApplicationContext`). If you use these scopes with regular Spring IoC containers
2426-
such as the `ClassPathXmlApplicationContext`, you get an `IllegalStateException`
2427-
complaining about an unknown bean scope.
2427+
The `request`, `session`, `globalSession`, `application`, and `websocket` scopes are
2428+
__only__ available if you use a web-aware Spring `ApplicationContext` implementation
2429+
(such as `XmlWebApplicationContext`). If you use these scopes with regular Spring IoC
2430+
containers such as the `ClassPathXmlApplicationContext`, an `IllegalStateException` will
2431+
be thrown complaining about an unknown bean scope.
24282432

24292433

24302434
[[beans-factory-scopes-other-web-configuration]]
24312435
==== Initial web configuration
24322436

2433-
To support the scoping of beans at the `request`, `session`, and `global session` levels
2434-
(web-scoped beans), some minor initial configuration is required before you define your
2435-
beans. (This initial setup is __not__ required for the standard scopes, `singleton` and
2436-
`prototype`.)
2437+
To support the scoping of beans at the `request`, `session`, `globalSession`,
2438+
`application`, and `websocket` levels (web-scoped beans), some minor initial
2439+
configuration is required before you define your beans. (This initial setup is __not__
2440+
required for the standard scopes, `singleton` and `prototype`.)
24372441

24382442
How you accomplish this initial setup depends on your particular Servlet environment.
24392443

@@ -2493,7 +2497,7 @@ down the call chain.
24932497
[[beans-factory-scopes-request]]
24942498
==== Request scope
24952499

2496-
Consider the following bean definition:
2500+
Consider the following XML configuration for a bean definition:
24972501

24982502
[source,xml,indent=0]
24992503
[subs="verbatim,quotes"]
@@ -2513,7 +2517,7 @@ bean that is scoped to the request is discarded.
25132517
[[beans-factory-scopes-session]]
25142518
==== Session scope
25152519

2516-
Consider the following bean definition:
2520+
Consider the following XML configuration for a bean definition:
25172521

25182522
[source,xml,indent=0]
25192523
[subs="verbatim,quotes"]
@@ -2543,22 +2547,22 @@ Consider the following bean definition:
25432547
<bean id="userPreferences" class="com.foo.UserPreferences" scope="globalSession"/>
25442548
----
25452549

2546-
The `global session` scope is similar to the standard HTTP `Session` scope
2550+
The `globalSession` scope is similar to the standard HTTP `Session` scope
25472551
(<<beans-factory-scopes-session,described above>>), and applies only in the context of
25482552
portlet-based web applications. The portlet specification defines the notion of a global
25492553
`Session` that is shared among all portlets that make up a single portlet web
2550-
application. Beans defined at the `global session` scope are scoped (or bound) to the
2554+
application. Beans defined at the `globalSession` scope are scoped (or bound) to the
25512555
lifetime of the global portlet `Session`.
25522556

25532557
If you write a standard Servlet-based web application and you define one or more beans
2554-
as having `global session` scope, the standard HTTP `Session` scope is used, and no
2558+
as having `globalSession` scope, the standard HTTP `Session` scope is used, and no
25552559
error is raised.
25562560

25572561

25582562
[[beans-factory-scopes-application]]
25592563
==== Application scope
25602564

2561-
Consider the following bean definition:
2565+
Consider the following XML configuration for a bean definition:
25622566

25632567
[source,xml,indent=0]
25642568
[subs="verbatim,quotes"]
@@ -2637,12 +2641,12 @@ understand the "why" as well as the "how" behind it.
26372641
----
26382642

26392643
To create such a proxy, you insert a child `<aop:scoped-proxy/>` element into a scoped
2640-
bean definition. See <<beans-factory-scopes-other-injection-proxies>> and
2641-
<<xsd-configuration>>.) Why do definitions of beans scoped at the `request`, `session`,
2642-
`globalSession` and custom-scope levels require the `<aop:scoped-proxy/>` element ?
2644+
bean definition (see <<beans-factory-scopes-other-injection-proxies>> and
2645+
<<xsd-configuration>>). Why do definitions of beans scoped at the `request`, `session`,
2646+
`globalSession` and custom-scope levels require the `<aop:scoped-proxy/>` element?
26432647
Let's examine the following singleton bean definition and contrast it with what you need
2644-
to define for the aforementioned scopes. (The following `userPreferences` bean
2645-
definition as it stands is __incomplete.)__
2648+
to define for the aforementioned scopes (note that the following `userPreferences` bean
2649+
definition as it stands is __incomplete__).
26462650

26472651
[source,xml,indent=0]
26482652
[subs="verbatim,quotes"]
@@ -3126,7 +3130,7 @@ You configure destroy method callbacks similarly (in XML, that is) by using the
31263130

31273131
Where existing bean classes already have callback methods that are named at variance
31283132
with the convention, you can override the default by specifying (in XML, that is) the
3129-
method name using the `init-method` and `destroy-method` attributes of the <bean/>
3133+
method name using the `init-method` and `destroy-method` attributes of the `<bean/>`
31303134
itself.
31313135

31323136
The Spring container guarantees that a configured initialization callback is called
@@ -3395,7 +3399,7 @@ a reference to the name defined in its associated object definition.
33953399
----
33963400
public interface BeanNameAware {
33973401
3398-
void setBeanName(string name) throws BeansException;
3402+
void setBeanName(String name) throws BeansException;
33993403
34003404
}
34013405
----
@@ -4227,18 +4231,18 @@ arguments:
42274231
}
42284232
----
42294233

4230-
You can apply `@Autowired` to constructors and fields:
4234+
You can apply `@Autowired` to fields as well and even mix it with constructors:
42314235

42324236
[source,java,indent=0]
42334237
[subs="verbatim,quotes"]
42344238
----
42354239
public class MovieRecommender {
42364240
4241+
private final CustomerPreferenceDao customerPreferenceDao;
4242+
42374243
@Autowired
42384244
private MovieCatalog movieCatalog;
42394245
4240-
private CustomerPreferenceDao customerPreferenceDao;
4241-
42424246
@Autowired
42434247
public MovieRecommender(CustomerPreferenceDao customerPreferenceDao) {
42444248
this.customerPreferenceDao = customerPreferenceDao;
@@ -4373,7 +4377,7 @@ automatically resolved, with no special setup necessary.
43734377

43744378
[NOTE]
43754379
====
4376-
`@Autowired`, `@Inject`, `@Resource`, and `@Value` annotations are handled by a Spring
4380+
`@Autowired`, `@Inject`, `@Resource`, and `@Value` annotations are handled by Spring
43774381
`BeanPostProcessor` implementations which in turn means that you __cannot__ apply these
43784382
annotations within your own `BeanPostProcessor` or `BeanFactoryPostProcessor` types (if
43794383
any). These types must be 'wired up' explicitly via XML or using a Spring `@Bean` method.
@@ -4573,8 +4577,8 @@ to the specific collection or map bean by unique name.
45734577
45744578
`@Autowired` applies to fields, constructors, and multi-argument methods, allowing for
45754579
narrowing through qualifier annotations at the parameter level. By contrast, `@Resource`
4576-
is supported only for fields and bean property setter methods with a single argument. As
4577-
a consequence, stick with qualifiers if your injection target is a constructor or a
4580+
is supported only for fields and bean property setter methods with a single argument.
4581+
As a consequence, stick with qualifiers if your injection target is a constructor or a
45784582
multi-argument method.
45794583
====
45804584

@@ -5324,9 +5328,9 @@ and the equivalent using XML
53245328
[NOTE]
53255329
====
53265330
You can also disable the default filters by setting `useDefaultFilters=false` on the annotation or
5327-
providing `use-default-filters="false"` as an attribute of the <component-scan/> element. This
5331+
providing `use-default-filters="false"` as an attribute of the `<component-scan/>` element. This
53285332
will in effect disable automatic detection of classes annotated with `@Component`, `@Repository`,
5329-
`@Service`, or `@Controller`.
5333+
`@Service`, `@Controller`, or `@Configuration`.
53305334
====
53315335

53325336

@@ -5474,14 +5478,14 @@ analogous to how the container selects between multiple `@Autowired` constructor
54745478

54755479
When a component is autodetected as part of the scanning process, its bean name is
54765480
generated by the `BeanNameGenerator` strategy known to that scanner. By default, any
5477-
Spring stereotype annotation ( `@Component`, `@Repository`, `@Service`, and
5478-
`@Controller`) that contains a `name` value will thereby provide that name to the
5481+
Spring stereotype annotation (`@Component`, `@Repository`, `@Service`, and
5482+
`@Controller`) that contains a _name_ `value` will thereby provide that name to the
54795483
corresponding bean definition.
54805484

5481-
If such an annotation contains no `name` value or for any other detected component (such
5485+
If such an annotation contains no _name_ `value` or for any other detected component (such
54825486
as those discovered by custom filters), the default bean name generator returns the
54835487
uncapitalized non-qualified class name. For example, if the following two components
5484-
were detected, the names would be myMovieLister and movieFinderImpl:
5488+
were detected, the names would be `myMovieLister` and `movieFinderImpl`:
54855489

54865490
[source,java,indent=0]
54875491
[subs="verbatim,quotes"]
@@ -5539,8 +5543,8 @@ auto-generated names are adequate whenever the container is responsible for wiri
55395543
=== Providing a scope for autodetected components
55405544

55415545
As with Spring-managed components in general, the default and most common scope for
5542-
autodetected components is singleton. However, sometimes you need other scopes, which
5543-
Spring 2.5 provides with a new `@Scope` annotation. Simply provide the name of the scope
5546+
autodetected components is `singleton`. However, sometimes you need a different scope
5547+
which can be specified via the `@Scope` annotation. Simply provide the name of the scope
55445548
within the annotation:
55455549

55465550
[source,java,indent=0]
@@ -5830,7 +5834,7 @@ Please use Spring's stereotype model for building custom component annotations.
58305834

58315835

58325836
[[beans-standard-annotations-limitations]]
5833-
=== Limitations of the standard approach
5837+
=== Limitations of JSR-330 standard annotations
58345838

58355839
When working with standard annotations, it is important to know that some significant
58365840
features are not available as shown in the table below:

0 commit comments

Comments
 (0)