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:
Sam Brannen
2023-08-27 17:38:51 +02:00
parent e239753a1b
commit 4e2d9252e5
7 changed files with 117 additions and 4 deletions

View File

@@ -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 &mdash; for example,

View File

@@ -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 &mdash; for example,