diff --git a/spring-framework-reference/src/beans.xml b/spring-framework-reference/src/beans.xml index 0b6ddda8af5..48f4237fd01 100644 --- a/spring-framework-reference/src/beans.xml +++ b/spring-framework-reference/src/beans.xml @@ -2626,10 +2626,10 @@ public class ReplacementComputeValue implements MethodReplacer { session + linkend="beans-factory-scopes-session">session - Scopes a single bean definition to the lifecycle of a + Scopes a single bean definition to the lifecycle of an HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext. @@ -2991,18 +2991,18 @@ public class ReplacementComputeValue implements MethodReplacer {
Scoped beans as dependencies - Being able to define a bean scoped to a HTTP request or + Being able to define a bean scoped to an HTTP request or Session (or indeed a custom scope of your own devising) is all very well, but one of the main value-adds of the Spring IoC container is that it manages not only the instantiation of your objects (beans), but also the wiring up of collaborators (or - dependencies). If you want to inject a (for example) HTTP request + dependencies). If you want to inject (for example) an HTTP request scoped bean into another bean, you will need to inject an AOP proxy in place of the scoped bean. That is, you need to inject a proxy object - that exposes the same public interface as the scoped object, but that + that exposes the same public interface as the scoped object but that is smart enough to be able to retrieve the real, target object from - the relevant scope (for example a HTTP request) and delegate method + the relevant scope (for example an HTTP request) and delegate method calls onto the real object. @@ -3029,7 +3029,7 @@ public class ReplacementComputeValue implements MethodReplacer { http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> - <!-- a HTTP Session-scoped bean exposed as a proxy --> + <!-- an HTTP Session-scoped bean exposed as a proxy --> <bean id="userPreferences" class="com.foo.UserPreferences" scope="session"> @@ -3079,12 +3079,12 @@ public class ReplacementComputeValue implements MethodReplacer { (conceptually) only ever operate on the exact same 'userPreferences' object, that is the one that it was originally injected with. This is not what - you want when you inject a HTTP + you want when you inject an HTTP Session-scoped bean as a dependency into a collaborating object (typically). Rather, what we do want is a single 'userManager' object, and then, for the lifetime of - a HTTP Session, we want to see and use + an HTTP Session, we want to see and use a 'userPreferences' object that is specific to said HTTP Session. @@ -3200,7 +3200,7 @@ public class ReplacementComputeValue implements MethodReplacer { allowing them to be 'destroyed' if needed. The first method should return the object from the underlying - scope. The session scope implementation for example will return the + scope. The session scope implementation, for example, will return the session-scoped bean (and if it does not exist, return a new instance of the bean, after having bound it to the session for future reference). @@ -6321,11 +6321,10 @@ public class AppConfig { destroy-method, autowiring - and name. + and name. - You can use the @Bean annotation in a Configuraton-class or in a - Component-class. + You can use the @Bean annotation in + a Configuration-class or in a Component-class.
Declaring a bean @@ -6346,7 +6345,7 @@ public class AppConfig { return new TransferServiceImpl(); } -} +} For comparison sake, the configuration above is exactly equivalent to the following Spring XML: Specifying bean scope
- Using the <interfacename>@Scope</interfacename> - annotation + Using the <interfacename>@Scope</interfacename> annotation You can specify that your beans defined with the @Bean annotation should have a @@ -6476,18 +6474,18 @@ public class AppConfig { the Bean Scopes section. - The StandardScopes class provides string - constants for each of these four scopes. SINGLETON is the default, - and can be overridden by using the - @Scope annotation: The default scope is "singleton", but + this can be overridden by using the + @Scope annotation: +@Configuration public class MyConfiguration { @Bean - @Scope(StandardScopes.PROTOTYPE) + @Scope("prototype") public Encryptor encryptor() { // ... } -} +}
@@ -6507,9 +6505,9 @@ public class MyConfiguration { If we were to port the the XML reference documentation scoped proxy example (see link above) to our @Bean using Java, it would look like - the following: // a HTTP Session-scoped bean exposed as a proxy + the following: // an HTTP Session-scoped bean exposed as a proxy @Bean -@Scope(value = StandardScopes.SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS) +@Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS) public UserPreferences userPreferences() { return new UserPreferences(); } @@ -6548,10 +6546,10 @@ public Service userService() { Using Java-configuration support we can easily create a subclass of CommandManager where the abstract - createCommand() is overridden in such a way that it + createCommand() method is overridden in such a way that it 'looks up' a brand new (prototype) command object: @Bean -@Scope(StandardScopes.PROTOTYPE) +@Scope("prototype") public AsyncCommand asyncCommand() { AsyncCommand command = new AsyncCommand(); // inject dependencies here as required @@ -6644,12 +6642,12 @@ public class FactoryMethodComponent { return tb; } - @Bean @Scope(StandardScopes.PROTOTYPE) + @Bean @Scope("prototype") private TestBean privateInstance() { return new TestBean("privateInstance", i++); } - @Bean @Scope(value = StandardScopes.SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS) + @Bean @Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS) public TestBean requestScopedInstance() { return new TestBean("requestScopedInstance", 3); } @@ -6736,14 +6734,14 @@ public class MovieFinderImpl implements MovieFinder { As with Spring-managed components in general, the default and by far most common scope is 'singleton'. However, there are times when - other scopes are needed. Therefore Spring 2.5 introduces a new + other scopes are needed. Therefore Spring 2.5 introduced a new @Scope annotation as well. Simply provide the name of the scope within the annotation, such as: - @Scope(StandardScopes.PROTOTYPE) + @Scope("prototype") @Repository public class MovieFinderImpl implements MovieFinder { - // ... + // ... }