Skip to content

Allow component name to be specified in @ControllerAdvice [SPR-16566] #21108

Closed
@spring-projects-issues

Description

@spring-projects-issues

Peter Luttrell opened SPR-16566 and commented

Please consider adding a property to the @ControllerAdvice and @RestControllerAdvice annotations that provides a hint to SpringBoot naming just like is currently provided for @Controller and @RestController. This would enable users to have two identically named ControllerAdvice classes in different packages peacefully coexist.

For example, these two classes works nicely togather:

com.v1.SomeRestController:

@RestController
public class SomeRestController{
}

com.v2.SomeRestController:

@RestController("someRestController-v2")
public class SomeRestController{
}

So it'd be great if these would also work togather:

com.v1.SomeRestControllerAdvice:

@RestControllerAdvice(assignableTypes = {SomeRestController.class})
public class SomeRestControllerAdvice{
}

com.v2.SomeRestControllerAdvice:

@RestControllerAdvice(assignableTypes = {SomeRestController.class})
public class SomeRestControllerAdvice{
}

But the last class causes the following as of the version of Spring included with Spring Boot 2.0.0.RELEASE:

org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class  
        [omitted for brevity]
Caused by: org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'someRestControllerAdvice' for bean class [com.v2.SomeRestControllerAdvice] conflicts with existing, non-compatible bean definition of same name and class [com.v1.SomeRestControllerAdvice]
	at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.checkCandidate(ClassPathBeanDefinitionScanner.java:345) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.doScan(ClassPathBeanDefinitionScanner.java:283) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at org.springframework.context.annotation.ComponentScanAnnotationParser.parse(ComponentScanAnnotationParser.java:135) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:287) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:245) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:198) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:167) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	... 13 common frames omitted


Affects: 5.0.4

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions