Skip to content

Commit 4e2d925

Browse files
committed
Allow component name to be specified in @[Rest]ControllerAdvice
This commit builds on the recently added support for using @AliasFor to override the `value` attribute in `@Component, and allows a custom component name to be specified in both @ControllerAdvice and @RestControllerAdvice via new `name` attributes. See gh-31089 Closes gh-21108
1 parent e239753 commit 4e2d925

File tree

7 files changed

+117
-4
lines changed

7 files changed

+117
-4
lines changed

framework-docs/modules/ROOT/pages/core/beans/classpath-scanning.adoc

+1-1
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ As of Spring Framework 6.1, the name of the annotation attribute that is used to
675675
the bean name is no longer required to be `value`. Custom stereotype annotations can
676676
declare an attribute with a different name (such as `name`) and annotate that attribute
677677
with `@AliasFor(annotation = Component.class, attribute = "value")`. See the source code
678-
declaration of `Repository#value()` for a concrete example.
678+
declaration of `Repository#value()` and `ControllerAdvice#name()` for concrete examples.
679679
====
680680

681681
If such an annotation contains no name `value` or for any other detected component

spring-context/src/main/java/org/springframework/stereotype/Component.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@
3535
* <p>As of Spring Framework 6.1, custom component stereotype annotations should
3636
* use {@link org.springframework.core.annotation.AliasFor @AliasFor} to declare
3737
* an explicit alias for this annotation's {@link #value} attribute. See the
38-
* source code declaration of {@link Repository#value()} for a concrete example.
38+
* source code declaration of {@link Repository#value()} and
39+
* {@link org.springframework.web.bind.annotation.ControllerAdvice#name()
40+
* ControllerAdvice.name()} for concrete examples.
3941
*
4042
* @author Mark Fisher
4143
* @author Sam Brannen

spring-web/src/main/java/org/springframework/web/bind/annotation/ControllerAdvice.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -78,6 +78,13 @@
7878
@Component
7979
public @interface ControllerAdvice {
8080

81+
/**
82+
* Alias for {@link Component#value}.
83+
* @since 6.1
84+
*/
85+
@AliasFor(annotation = Component.class, attribute = "value")
86+
String name() default "";
87+
8188
/**
8289
* Alias for the {@link #basePackages} attribute.
8390
* <p>Allows for more concise annotation declarations &mdash; for example,

spring-web/src/main/java/org/springframework/web/bind/annotation/RestControllerAdvice.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -52,6 +52,13 @@
5252
@ResponseBody
5353
public @interface RestControllerAdvice {
5454

55+
/**
56+
* Alias for {@link ControllerAdvice#name}.
57+
* @since 6.1
58+
*/
59+
@AliasFor(annotation = ControllerAdvice.class)
60+
String name() default "";
61+
5562
/**
5663
* Alias for the {@link #basePackages} attribute.
5764
* <p>Allows for more concise annotation declarations &mdash; for example,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2002-2023 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+
* https://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.bind.annotation;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
22+
23+
import static org.assertj.core.api.Assertions.assertThat;
24+
25+
/**
26+
* Integration tests that verify support for component scanning
27+
* {@link ControllerAdvice} and {@link RestControllerAdvice} beans.
28+
*
29+
* @author Sam Brannen
30+
* @since 6.1
31+
*/
32+
class ComponentScannedControllerAdviceTests {
33+
34+
@Test
35+
void scannedAdviceHasCustomName() {
36+
String basePackage = getClass().getPackageName() + ".scanned";
37+
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(basePackage)) {
38+
assertThat(context.getBean("myControllerAdvice")).isNotNull();
39+
assertThat(context.getBean("myRestControllerAdvice")).isNotNull();
40+
}
41+
}
42+
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2002-2023 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+
* https://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.bind.annotation.scanned;
18+
19+
import org.springframework.web.bind.annotation.ControllerAdvice;
20+
21+
/**
22+
* @author Sam Brannen
23+
* @since 6.1
24+
*/
25+
@ControllerAdvice(name = "myControllerAdvice", value = "org.my.pkg")
26+
class ScannedControllerAdvice {
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2002-2023 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+
* https://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.bind.annotation.scanned;
18+
19+
import org.springframework.web.bind.annotation.RestControllerAdvice;
20+
21+
/**
22+
* @author Sam Brannen
23+
* @since 6.1
24+
*/
25+
@RestControllerAdvice(name = "myRestControllerAdvice", value = "org.my.pkg")
26+
class ScannedRestControllerAdvice {
27+
}

0 commit comments

Comments
 (0)