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
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -78,6 +78,13 @@ import org.springframework.stereotype.Component;
|
||||
@Component
|
||||
public @interface ControllerAdvice {
|
||||
|
||||
/**
|
||||
* Alias for {@link Component#value}.
|
||||
* @since 6.1
|
||||
*/
|
||||
@AliasFor(annotation = Component.class, attribute = "value")
|
||||
String name() default "";
|
||||
|
||||
/**
|
||||
* Alias for the {@link #basePackages} attribute.
|
||||
* <p>Allows for more concise annotation declarations — for example,
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -52,6 +52,13 @@ import org.springframework.core.annotation.AliasFor;
|
||||
@ResponseBody
|
||||
public @interface RestControllerAdvice {
|
||||
|
||||
/**
|
||||
* Alias for {@link ControllerAdvice#name}.
|
||||
* @since 6.1
|
||||
*/
|
||||
@AliasFor(annotation = ControllerAdvice.class)
|
||||
String name() default "";
|
||||
|
||||
/**
|
||||
* Alias for the {@link #basePackages} attribute.
|
||||
* <p>Allows for more concise annotation declarations — for example,
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright 2002-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.web.bind.annotation;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Integration tests that verify support for component scanning
|
||||
* {@link ControllerAdvice} and {@link RestControllerAdvice} beans.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 6.1
|
||||
*/
|
||||
class ComponentScannedControllerAdviceTests {
|
||||
|
||||
@Test
|
||||
void scannedAdviceHasCustomName() {
|
||||
String basePackage = getClass().getPackageName() + ".scanned";
|
||||
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(basePackage)) {
|
||||
assertThat(context.getBean("myControllerAdvice")).isNotNull();
|
||||
assertThat(context.getBean("myRestControllerAdvice")).isNotNull();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* Copyright 2002-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.web.bind.annotation.scanned;
|
||||
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
|
||||
/**
|
||||
* @author Sam Brannen
|
||||
* @since 6.1
|
||||
*/
|
||||
@ControllerAdvice(name = "myControllerAdvice", value = "org.my.pkg")
|
||||
class ScannedControllerAdvice {
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* Copyright 2002-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.web.bind.annotation.scanned;
|
||||
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
/**
|
||||
* @author Sam Brannen
|
||||
* @since 6.1
|
||||
*/
|
||||
@RestControllerAdvice(name = "myRestControllerAdvice", value = "org.my.pkg")
|
||||
class ScannedRestControllerAdvice {
|
||||
}
|
||||
Reference in New Issue
Block a user