Skip to content

Commit 161ec15

Browse files
committed
Merge pull request #982 from sbrannen/SPR-13993
* SPR-13993: Introduce composed annotations for web scopes
2 parents df7b24b + b423596 commit 161ec15

File tree

5 files changed

+243
-49
lines changed

5 files changed

+243
-49
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2002-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.web.context.annotation;
18+
19+
import java.lang.annotation.Documented;
20+
import java.lang.annotation.Retention;
21+
import java.lang.annotation.Target;
22+
23+
import org.springframework.context.annotation.Scope;
24+
import org.springframework.context.annotation.ScopedProxyMode;
25+
import org.springframework.core.annotation.AliasFor;
26+
27+
import static java.lang.annotation.ElementType.METHOD;
28+
import static java.lang.annotation.ElementType.TYPE;
29+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
30+
import static org.springframework.web.context.WebApplicationContext.SCOPE_APPLICATION;
31+
32+
/**
33+
* {@code @ApplicationScope} is a specialization of {@link Scope @Scope} for a
34+
* component whose lifecycle is bound to the current web application.
35+
*
36+
* <p>Specifically, {@code @ApplicationScope} is a <em>composed annotation</em> that
37+
* acts as a shortcut for {@code @Scope("application")} with the default
38+
* {@link #proxyMode} set to {@link ScopedProxyMode#TARGET_CLASS TARGET_CLASS}.
39+
*
40+
* <p>{@code @ApplicationScope} may be used as a meta-annotation to create custom
41+
* composed annotations.
42+
*
43+
* @author Sam Brannen
44+
* @since 4.3
45+
* @see RequestScope
46+
* @see SessionScope
47+
* @see org.springframework.context.annotation.Scope
48+
* @see org.springframework.web.context.WebApplicationContext#SCOPE_APPLICATION
49+
* @see org.springframework.web.context.support.ServletContextScope
50+
* @see org.springframework.stereotype.Component
51+
* @see org.springframework.context.annotation.Bean
52+
*/
53+
@Scope(SCOPE_APPLICATION)
54+
@Target({ TYPE, METHOD })
55+
@Retention(RUNTIME)
56+
@Documented
57+
public @interface ApplicationScope {
58+
59+
/**
60+
* Alias for {@link Scope#proxyMode}.
61+
* <p>Defaults to {@link ScopedProxyMode#TARGET_CLASS}.
62+
*/
63+
@AliasFor(annotation = Scope.class)
64+
ScopedProxyMode proxyMode() default ScopedProxyMode.TARGET_CLASS;
65+
66+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2002-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.web.context.annotation;
18+
19+
import java.lang.annotation.Documented;
20+
import java.lang.annotation.Retention;
21+
import java.lang.annotation.Target;
22+
23+
import org.springframework.context.annotation.Scope;
24+
import org.springframework.context.annotation.ScopedProxyMode;
25+
import org.springframework.core.annotation.AliasFor;
26+
27+
import static java.lang.annotation.ElementType.METHOD;
28+
import static java.lang.annotation.ElementType.TYPE;
29+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
30+
import static org.springframework.web.context.WebApplicationContext.SCOPE_REQUEST;
31+
32+
/**
33+
* {@code @RequestScope} is a specialization of {@link Scope @Scope} for a
34+
* component whose lifecycle is bound to the current web request.
35+
*
36+
* <p>Specifically, {@code @RequestScope} is a <em>composed annotation</em> that
37+
* acts as a shortcut for {@code @Scope("request")} with the default
38+
* {@link #proxyMode} set to {@link ScopedProxyMode#TARGET_CLASS TARGET_CLASS}.
39+
*
40+
* <p>{@code @RequestScope} may be used as a meta-annotation to create custom
41+
* composed annotations.
42+
*
43+
* @author Sam Brannen
44+
* @since 4.3
45+
* @see SessionScope
46+
* @see ApplicationScope
47+
* @see org.springframework.context.annotation.Scope
48+
* @see org.springframework.web.context.WebApplicationContext#SCOPE_REQUEST
49+
* @see org.springframework.web.context.request.RequestScope
50+
* @see org.springframework.stereotype.Component
51+
* @see org.springframework.context.annotation.Bean
52+
*/
53+
@Scope(SCOPE_REQUEST)
54+
@Target({ TYPE, METHOD })
55+
@Retention(RUNTIME)
56+
@Documented
57+
public @interface RequestScope {
58+
59+
/**
60+
* Alias for {@link Scope#proxyMode}.
61+
* <p>Defaults to {@link ScopedProxyMode#TARGET_CLASS}.
62+
*/
63+
@AliasFor(annotation = Scope.class)
64+
ScopedProxyMode proxyMode() default ScopedProxyMode.TARGET_CLASS;
65+
66+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2002-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.web.context.annotation;
18+
19+
import java.lang.annotation.Documented;
20+
import java.lang.annotation.Retention;
21+
import java.lang.annotation.Target;
22+
23+
import org.springframework.context.annotation.Scope;
24+
import org.springframework.context.annotation.ScopedProxyMode;
25+
import org.springframework.core.annotation.AliasFor;
26+
27+
import static java.lang.annotation.ElementType.METHOD;
28+
import static java.lang.annotation.ElementType.TYPE;
29+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
30+
import static org.springframework.web.context.WebApplicationContext.SCOPE_SESSION;
31+
32+
/**
33+
* {@code @SessionScope} is a specialization of {@link Scope @Scope} for a
34+
* component whose lifecycle is bound to the current web session.
35+
*
36+
* <p>Specifically, {@code @SessionScope} is a <em>composed annotation</em> that
37+
* acts as a shortcut for {@code @Scope("session")} with the default
38+
* {@link #proxyMode} set to {@link ScopedProxyMode#TARGET_CLASS TARGET_CLASS}.
39+
*
40+
* <p>{@code @SessionScope} may be used as a meta-annotation to create custom
41+
* composed annotations.
42+
*
43+
* @author Sam Brannen
44+
* @since 4.3
45+
* @see RequestScope
46+
* @see ApplicationScope
47+
* @see org.springframework.context.annotation.Scope
48+
* @see org.springframework.web.context.WebApplicationContext#SCOPE_SESSION
49+
* @see org.springframework.web.context.request.SessionScope
50+
* @see org.springframework.stereotype.Component
51+
* @see org.springframework.context.annotation.Bean
52+
*/
53+
@Scope(SCOPE_SESSION)
54+
@Target({ TYPE, METHOD })
55+
@Retention(RUNTIME)
56+
@Documented
57+
public @interface SessionScope {
58+
59+
/**
60+
* Alias for {@link Scope#proxyMode}.
61+
* <p>Defaults to {@link ScopedProxyMode#TARGET_CLASS}.
62+
*/
63+
@AliasFor(annotation = Scope.class)
64+
ScopedProxyMode proxyMode() default ScopedProxyMode.TARGET_CLASS;
65+
66+
}

src/asciidoc/whats-new.adoc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -662,10 +662,11 @@ Spring 4.3 also improves the caching abstraction as follows:
662662
=== Web Improvements
663663

664664
* Built-in support for <<mvc-ann-requestmapping-head-options,HTTP HEAD and HTTP OPTIONS>>.
665+
* New `@RequestScope`, `@SessionScope`, and `@ApplicationScope` _composed annotations_ for web scopes.
665666
* New `@RestControllerAdvice` annotation with combined `@ControllerAdvice` with `@ResponseBody` semantics.
666-
* `@ResponseStatus` supported on the class level and inherited on all methods.
667+
* `@ResponseStatus` is now supported at the class level and inherited by all methods.
667668
* New `@SessionAttribute` annotation for access to session attributes (see <<mvc-ann-sessionattrib-global, example>>).
668-
* New `@RequestAttribute` annotation for access to session attributes (see <<mvc-ann-requestattrib, example>>).
669+
* New `@RequestAttribute` annotation for access to request attributes (see <<mvc-ann-requestattrib, example>>).
669670
* `@ModelAttribute` allows preventing data binding via `binding=false` attribute (see <<mvc-ann-modelattrib-method-args, reference>>).
670671
* `AsyncRestTemplate` supports request interception.
671672

0 commit comments

Comments
 (0)